Forum > Games

BrainTrainer (Little maths practice game) How to do Division?

(1/3) > >>

Boleeman:
Found a tutorial on Utube by Devstructor and decided to make the Brain Trainer basic maths practice program.

I could do plus, minus and multiply but ...

How could  make a division operation for the Brain Trainer?

I have tried different approaches but nothing was working properly.

I tried making a numerator = number1*number2  and then doing numerator div number2 to check the answers but was getting weird results (probably division problem).


alpine:
The difference is that the division can give a fractional number unlike the others. And the zerodiv of course.


--- Quote ---I tried making a numerator = number1*number2  and then doing numerator div number2 to check the answers but was getting weird results (probably division problem).
--- End quote ---

Weird? What exactly is the problem?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.btnNextClick(Sender: TObject); var   Number1, Number2, NumberResult: Integer; begin   Number1:=StrtoInt(lblNumber1.Caption);  Number2:=StrtoInt(lblNumber2.Caption);   If lblOperation.Caption= '+' then  NumberResult:= Number1 + Number2  else if lblOperation.Caption= '-' then NumberResult:= Number1 - Number2  else if lblOperation.Caption= '*' then NumberResult:= Number1 * Number2  else if lblOperation.Caption= '/' then NumberResult:= Number1 div Number2;    {...}end; {...} procedure TForm1.GenerateFormula;var  RandomOP: Integer;  A, B: Integer;begin   A := Random(20);  B := Random(20);   RandomOp:=Random(4);   case RandomOp of    0: lblOperation.Caption:= '+';    1: lblOperation.Caption:= '-';    2: lblOperation.Caption:= '*';    3:      begin        lblOperation.Caption:= '/';        if B = 0 then          B := 1;        A := A * B;      end;  end;   lblNumber1.Caption:= InttoStr(A);  lblNumber2.Caption:= InttoStr(B);   pnlScore.Caption:='Score ' + InttoStr(Score);end; 

Fibonacci:
I had some free time so I wrote this while drinking my coffee :)


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a, b, o, r, i: integer; begin  randomize;   while true do begin    o := random(4); //0 + 1 - 2 * 3 /    //o := 3; //force division for testing     a := 1+random(10);    b := 1+random(10);     //if op is division    if o = 3 then begin      //allow divisor to be 1 only 1/3 of the times      if (b = 1) and not (random(3) = 0) then inc(b);      //use larger number      a := 1+random(20);      //choose a number that will be a natural number after division      while not (a mod b = 0) do inc(a);    end;     write('What is ');    write(a);    case o of      0: begin write(' + '); r := a + b; end;      1: begin write(' - '); r := a - b; end;      2: begin write(' * '); r := a * b; end;      3: begin write(' / '); r := a div b; end;    end;    write(b);    write(' = ');     readln(i);     if i = r then      writeln('Correct!')    else      writeln('Incorrect! Correct result is: ', r);     writeln;  end;end.

--- Quote ---What is 6 / 2 = 3
Correct!

What is 25 / 5 = 5
Correct!

What is 21 / 3 = 7
Correct!

What is 10 * 6 = 60
Correct!

What is 7 + 4 = 11
Correct!

What is 8 - 10 = -2
Correct!

What is 4 - 6 = 9
Incorrect! Correct result is: -2

What is 4 + 3 = 8
Incorrect! Correct result is: 7

What is 18 / 1 = 5
Incorrect! Correct result is: 18
--- End quote ---

Boleeman:
Thanks Fibonacci for your reply.

It works well, but I noticed that it had

6/6

and

70/10

How would you exclude dividing by the same number or dividing by 10?

I tried at line22

while not (a mod b = 0) and not (a=b) and (b<>10) do inc(a); 


Not really sure if that is correct? as I still got 8/8 after quite a long time.

Fibonacci:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  a, b, o, r, i: integer; begin  randomize;   while true do begin    o := random(4); //0 + 1 - 2 * 3 /    o := 3; //force division for testing     while true do begin      a := 1+random(10);      b := 1+random(10);      //if op is division      if o = 3 then begin        //allow divisor to be 1 only 1/3 of the times        if (b = 1) and not (random(3) = 0) then inc(b);        //use larger number        a := 1+random(20);        //choose a number that will be a natural number after division        while not (a mod b = 0) do inc(a);        //some more conditionals, choose new numbers if these happens        if a = b then continue;        if b = 10 then continue;      end;      //escape the loop      break;    end;     write('What is ');    write(a);    case o of      0: begin write(' + '); r := a + b; end;      1: begin write(' - '); r := a - b; end;      2: begin write(' * '); r := a * b; end;      3: begin write(' / '); r := a div b; end;    end;    write(b);    write(' = ');     readln(i);     if i = r then      writeln('Correct!')    else      writeln('Incorrect! Correct result is: ', r);     writeln;  end;end.

Navigation

[0] Message Index

[#] Next page

Go to full version