Algorithm Code

From BenningtonWiki
Jump to: navigation, search

Factorial

#include "gbaio.h"

int main(void) {

	int n, x;

	start_main();

	n = 7;  // the number to work with

	x = 1;
	while (n > 1) {
		x = x * n;
		n--;
	}

	draw_number(0, 0, x);		

infinite_loop: goto infinite_loop;

}

High / Low

#include "gbaio.h"

int main(void) {

	int n, i, x, y;
	int high, low, count;
	
	start_main();
	
	high = 100;
	low = 0;
	x = 0;
	y = 0;
	n = rand(low, high); 
	i = high/2;
	count = 1;
	
	while (i != n) {
		if (n < i) {
			high = i;	
		} else {
			low = i;	
		}
		
		i = low + ((high - low)/2);
		count++;
		
		draw_number(x, y, high);
		draw_number(x, (y + 10), low);
		draw_number(x, (y + 20), i);
                y = y + 40;

                if (y > 160) {
        	        y = 0;
        	        x = 25;
                }
	}

	draw_number(70, 0, count);
	draw_number(70, 20, i);
		
infinite_loop: goto infinite_loop;

}

Division

#include "gbaio.h"

int main(void) {

	int a, b, count;

	start_main();

	a = 789;	// a divided by b
	b = 35;

	count = 0;
	while (a >= b) {
		a = a - b;
		count++;
	}

	draw_number(0, 0, count);
	draw_number(0, 10, a);

infinite_loop: goto infinite_loop;

}