Recent

Author Topic: BrainTrainer (Little maths practice game) How to do Division?  (Read 1604 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 650
BrainTrainer (Little maths practice game) How to do Division?
« on: January 25, 2024, 04:03:14 am »
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).


« Last Edit: January 25, 2024, 07:46:00 am by Boleeman »

alpine

  • Hero Member
  • *****
  • Posts: 1248
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #1 on: January 25, 2024, 07:58:07 am »
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).

Weird? What exactly is the problem?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnNextClick(Sender: TObject);
  2.  var
  3.    Number1, Number2, NumberResult: Integer;
  4.  
  5. begin
  6.  
  7.   Number1:=StrtoInt(lblNumber1.Caption);
  8.   Number2:=StrtoInt(lblNumber2.Caption);
  9.  
  10.   If lblOperation.Caption= '+' then  NumberResult:= Number1 + Number2
  11.   else if lblOperation.Caption= '-' then NumberResult:= Number1 - Number2
  12.   else if lblOperation.Caption= '*' then NumberResult:= Number1 * Number2
  13.   else if lblOperation.Caption= '/' then NumberResult:= Number1 div Number2;  
  14.  
  15.  {...}
  16. end;
  17.  
  18. {...}
  19.  
  20. procedure TForm1.GenerateFormula;
  21. var
  22.   RandomOP: Integer;
  23.   A, B: Integer;
  24. begin
  25.  
  26.   A := Random(20);
  27.   B := Random(20);
  28.  
  29.   RandomOp:=Random(4);
  30.  
  31.   case RandomOp of
  32.     0: lblOperation.Caption:= '+';
  33.     1: lblOperation.Caption:= '-';
  34.     2: lblOperation.Caption:= '*';
  35.     3:
  36.       begin
  37.         lblOperation.Caption:= '/';
  38.         if B = 0 then
  39.           B := 1;
  40.         A := A * B;
  41.       end;
  42.   end;
  43.  
  44.   lblNumber1.Caption:= InttoStr(A);
  45.   lblNumber2.Caption:= InttoStr(B);
  46.  
  47.   pnlScore.Caption:='Score ' + InttoStr(Score);
  48. end;
  49.  
« Last Edit: January 25, 2024, 08:54:36 am by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #2 on: January 25, 2024, 08:21:14 am »
I had some free time so I wrote this while drinking my coffee :)

Code: Pascal  [Select][+][-]
  1. var
  2.   a, b, o, r, i: integer;
  3.  
  4. begin
  5.   randomize;
  6.  
  7.   while true do begin
  8.     o := random(4); //0 + 1 - 2 * 3 /
  9.     //o := 3; //force division for testing
  10.  
  11.     a := 1+random(10);
  12.     b := 1+random(10);
  13.  
  14.     //if op is division
  15.     if o = 3 then begin
  16.       //allow divisor to be 1 only 1/3 of the times
  17.       if (b = 1) and not (random(3) = 0) then inc(b);
  18.       //use larger number
  19.       a := 1+random(20);
  20.       //choose a number that will be a natural number after division
  21.       while not (a mod b = 0) do inc(a);
  22.     end;
  23.  
  24.     write('What is ');
  25.     write(a);
  26.     case o of
  27.       0: begin write(' + '); r := a + b; end;
  28.       1: begin write(' - '); r := a - b; end;
  29.       2: begin write(' * '); r := a * b; end;
  30.       3: begin write(' / '); r := a div b; end;
  31.     end;
  32.     write(b);
  33.     write(' = ');
  34.  
  35.     readln(i);
  36.  
  37.     if i = r then
  38.       writeln('Correct!')
  39.     else
  40.       writeln('Incorrect! Correct result is: ', r);
  41.  
  42.     writeln;
  43.   end;
  44. 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

Boleeman

  • Hero Member
  • *****
  • Posts: 650
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #3 on: January 25, 2024, 10:13:52 am »
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.
« Last Edit: January 25, 2024, 10:20:21 am by Boleeman »

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #4 on: January 25, 2024, 10:22:36 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   a, b, o, r, i: integer;
  3.  
  4. begin
  5.   randomize;
  6.  
  7.   while true do begin
  8.     o := random(4); //0 + 1 - 2 * 3 /
  9.     o := 3; //force division for testing
  10.  
  11.     while true do begin
  12.       a := 1+random(10);
  13.       b := 1+random(10);
  14.       //if op is division
  15.       if o = 3 then begin
  16.         //allow divisor to be 1 only 1/3 of the times
  17.         if (b = 1) and not (random(3) = 0) then inc(b);
  18.         //use larger number
  19.         a := 1+random(20);
  20.         //choose a number that will be a natural number after division
  21.         while not (a mod b = 0) do inc(a);
  22.         //some more conditionals, choose new numbers if these happens
  23.         if a = b then continue;
  24.         if b = 10 then continue;
  25.       end;
  26.       //escape the loop
  27.       break;
  28.     end;
  29.  
  30.     write('What is ');
  31.     write(a);
  32.     case o of
  33.       0: begin write(' + '); r := a + b; end;
  34.       1: begin write(' - '); r := a - b; end;
  35.       2: begin write(' * '); r := a * b; end;
  36.       3: begin write(' / '); r := a div b; end;
  37.     end;
  38.     write(b);
  39.     write(' = ');
  40.  
  41.     readln(i);
  42.  
  43.     if i = r then
  44.       writeln('Correct!')
  45.     else
  46.       writeln('Incorrect! Correct result is: ', r);
  47.  
  48.     writeln;
  49.   end;
  50. end.

alpine

  • Hero Member
  • *****
  • Posts: 1248
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #5 on: January 25, 2024, 10:24:16 am »
while not (a mod b = 0) and not (a=b) and (b<>10) do inc(a); 
Not really sure if that is correct?
It is wrong. Additional conditions (for unwanted values) must be OR-ed. But then the (b<>10) is over b, which is not modified into the while body. Infinite loop.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Hero Member
  • *****
  • Posts: 650
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #6 on: January 25, 2024, 10:49:23 am »
With Fibonacci's 2nd code:
I keep getting just division happening.



Alpine, are you saying       

while not (a mod b = 0) or (a=b) or (b=10) do inc(a);

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #7 on: January 25, 2024, 10:51:46 am »
With Fibonacci's 2nd code:
I keep getting just division happening.

Yes, because its forced for testing purposes

Little improv

Code: Pascal  [Select][+][-]
  1. var
  2.   a, b, o, r, i: integer;
  3.  
  4. begin
  5.   randomize;
  6.  
  7.   while true do begin
  8.     o := random(4); //0 + 1 - 2 * 3 /
  9.     //o := 3; //force division for testing
  10.  
  11.     while true do begin
  12.       a := 1+random(10);
  13.       b := 1+random(10);
  14.       //if op is division
  15.       if o = 3 then begin
  16.         //allow divisor to be 1 only 1/3 of the times
  17.         if (b = 1) and not (random(3) = 0) then inc(b);
  18.         //use larger number
  19.         a := 1+random(20);
  20.         //choose a number that will be a natural number after division
  21.         if not (a mod b = 0) then a := b*(1+random(5));
  22.         //some more conditionals, choose new numbers if these happens
  23.         if a = b then continue;
  24.         if b = 10 then continue;
  25.       end;
  26.       //escape the loop
  27.       break;
  28.     end;
  29.  
  30.     write('What is ');
  31.     write(a);
  32.     case o of
  33.       0: begin write(' + '); r := a + b; end;
  34.       1: begin write(' - '); r := a - b; end;
  35.       2: begin write(' * '); r := a * b; end;
  36.       3: begin write(' / '); r := a div b; end;
  37.     end;
  38.     write(b);
  39.     write(' = ');
  40.  
  41.     readln(i);
  42.  
  43.     if i = r then
  44.       writeln('Correct!')
  45.     else
  46.       writeln('Incorrect! Correct result is: ', r);
  47.  
  48.     writeln;
  49.   end;
  50. end.

alpine

  • Hero Member
  • *****
  • Posts: 1248
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #8 on: January 25, 2024, 11:00:32 am »
Alpine, are you saying       

while not (a mod b = 0) or (a=b) or (b=10) do inc(a);
Yes, and I'm saying that when b=10, changing the a won't help.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Boleeman

  • Hero Member
  • *****
  • Posts: 650
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #9 on: January 25, 2024, 11:10:10 am »
Fibonacci, code for division works better now. I also added
        if a = b then continue;
        if b = 1 then continue;
        if b = 10 then continue;



alpine, OK "changing the a won't help".
I was going up the wrong path alright.

Boleeman

  • Hero Member
  • *****
  • Posts: 650
Re: BrainTrainer (Little maths practice game) How to do Division?
« Reply #10 on: January 25, 2024, 11:15:45 am »
I was reading different threads that suggested when doing  / to make the data type float.
Yet Fibonacci was able to do it with just integers. That's why I was getting confused.

For division
        //choose a number that will be a natural number after division
        if not (a mod b = 0) then a := b*(1+random(5));
was the important bit.

I did something similar in VB6 years ago, but forgot how I did it.

Thanks Fibonacci and Alpine.
« Last Edit: January 25, 2024, 11:19:44 am by Boleeman »

 

TinyPortal © 2005-2018