Reply to Simon's version 2 - tested this time
If you don't have an account, just leave the password field blank.
Okay here it goes... I've tested this as key-80.c and Dink tells me the right answer for the tests of 4, 9, 16, 25, 400, 10000... and I'm assuming 123456789
....
//Sqaure Root
void main( void )
{
int &mynumber;
int &guess = 3;
int &avg;
int ÷
int &chk1;
int &chk2;
int &sqroot;
int &countloop;
int &disploop;
freeze(1);
newtest:
&countloop = 0;
int &guess = 3;
//assign a value to the &mynumber variable
choice_start()
"Test 4"
"Test 9"
"Test 16"
"Test 25"
"Test 144"
"Test 400"
"Test 10000"
"Test 123456789"
choice_end();
if (&result == 1)
&mynumber = 4;
if (&result == 2)
&mynumber = 9;
if (&result == 3)
&mynumber = 16;
if (&result == 4)
&mynumber = 25;
if (&result == 5)
&mynumber = 144;
if (&result == 6)
&mynumber = 400;
if (&result == 7)
&mynumber = 10000;
if (&result == 8)
&mynumber = 123456789;
debug("Starting new test for &mynumber");
calroot:
debug("Calroot has started run &countloop + 1");
debug("Guess is &guess");
&div = &mynumber;
debug("divide is now &div which should be &mynumber");
&div / &guess;
debug("divide is now ÷");
&avg = &guess;
debug("average is now &avg which should be &guess");
&avg += ÷
debug("average is now &avg which should be &guess plus &div");
&avg / 2;
debug("average is now &avg ... old one divided by 2");
debug("guess is still &guess");
&countloop += 1;
debug("loop passed thru is &countloop");
//next line is just to see how many times it takes to get to an answer
say_stop_xy("`5GONE THRU LOOP &countloop TIMES", 10, 40);
//remove this line to speed up the calcualtions
// wait(1000);
if (&guess == &avg)
{
debug("We are here if &guess equals &avg");
goto getoutofloop;
} else
{
debug("We are here to figure out if guess and average are close");
debug("Guess is &guess, Average is &avg");
&chk1 = &guess;
&chk1 -= &avg;
if (&chk1 == 1)
goto getoutofloop;
&chk2 = &avg;
&chk2 -= &guess;
if (&chk2 == 1)
goto getoutofloop;
debug("If we are here, then the difference between guess and average is more than one");
debug("so now guess becomes the old average");
&guess = &avg;
&disploop = get_sprite_with_this_brain(8, 0);
sp_kill(&disploop, 1);
goto calroot;
}
getoutofloop:
debug("finished calroot loop");
&sqroot = &avg;
say_stop("`5The Sqaure Root of &mynumber is &sqroot", 1);
choice_start()
"Another Test"
"Leave"
choice_end();
if (&result == 1)
goto newtest;
unfreeze(1);
kill_this_task();
}
....
//Sqaure Root
void main( void )
{
int &mynumber;
int &guess = 3;
int &avg;
int ÷
int &chk1;
int &chk2;
int &sqroot;
int &countloop;
int &disploop;
freeze(1);
newtest:
&countloop = 0;
int &guess = 3;
//assign a value to the &mynumber variable
choice_start()
"Test 4"
"Test 9"
"Test 16"
"Test 25"
"Test 144"
"Test 400"
"Test 10000"
"Test 123456789"
choice_end();
if (&result == 1)
&mynumber = 4;
if (&result == 2)
&mynumber = 9;
if (&result == 3)
&mynumber = 16;
if (&result == 4)
&mynumber = 25;
if (&result == 5)
&mynumber = 144;
if (&result == 6)
&mynumber = 400;
if (&result == 7)
&mynumber = 10000;
if (&result == 8)
&mynumber = 123456789;
debug("Starting new test for &mynumber");
calroot:
debug("Calroot has started run &countloop + 1");
debug("Guess is &guess");
&div = &mynumber;
debug("divide is now &div which should be &mynumber");
&div / &guess;
debug("divide is now ÷");
&avg = &guess;
debug("average is now &avg which should be &guess");
&avg += ÷
debug("average is now &avg which should be &guess plus &div");
&avg / 2;
debug("average is now &avg ... old one divided by 2");
debug("guess is still &guess");
&countloop += 1;
debug("loop passed thru is &countloop");
//next line is just to see how many times it takes to get to an answer
say_stop_xy("`5GONE THRU LOOP &countloop TIMES", 10, 40);
//remove this line to speed up the calcualtions
// wait(1000);
if (&guess == &avg)
{
debug("We are here if &guess equals &avg");
goto getoutofloop;
} else
{
debug("We are here to figure out if guess and average are close");
debug("Guess is &guess, Average is &avg");
&chk1 = &guess;
&chk1 -= &avg;
if (&chk1 == 1)
goto getoutofloop;
&chk2 = &avg;
&chk2 -= &guess;
if (&chk2 == 1)
goto getoutofloop;
debug("If we are here, then the difference between guess and average is more than one");
debug("so now guess becomes the old average");
&guess = &avg;
&disploop = get_sprite_with_this_brain(8, 0);
sp_kill(&disploop, 1);
goto calroot;
}
getoutofloop:
debug("finished calroot loop");
&sqroot = &avg;
say_stop("`5The Sqaure Root of &mynumber is &sqroot", 1);
choice_start()
"Another Test"
"Leave"
choice_end();
if (&result == 1)
goto newtest;
unfreeze(1);
kill_this_task();
}