Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Dividing

delphi

  • Please log in to reply
2 replies to this topic

#1 Sparky

Sparky

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 23 September 2008 - 04:24 AM

Hi im new at this forum.
I've been donig delphi for almost a year now, but i still have problems in my programs as i am not a professional. My latest problem is that i have to created a program that divides two random numbers. The problem is that the answer must be a whole number and not a decimal one. THe second number must also divide exactely in to the first one.
For example: 10 / 5 = 2 and not 10 / 3 = 3.333333
The first number has also got to be greater than the second number.

This is my code and it exicutes it when a button is pressed, it is incomplete, but my main aim is to get the answer as above.
procedure divide;
var iAdd1, iAdd2, iresult : integer;
begin
Randomize;
iAdd1 := Random(20) + 1;
iAdd2 := Random(20) + 1;
iResult := iAdd1 div iAdd2;
end ;

procedure TfrmPrimary.BitBtnDivideClick(Sender: TObject);
var
rAdd1, rAdd2, ireal, rResult : Real;
begin
Randomize;
rAdd1 := Random(20) + 1;
rAdd2 := Random(20) + 1;
rResult := rAdd1 / rAdd2;
if not (rResult In [1..20]) then
divide;
Inputstr := InputBox('Addition [ / ]', IntToStr(iAdd1) +  '/' + IntToStr(iAdd2) + ' - Always put in a Answer','0');
  If StrToInt(Inputstr) = iResult then
  inc(iRight,1) else
  inc(iWrong,1);
    If StrToInt(Inputstr) = iResult then
    lblRightDivide.Caption := 'Number of Correct Answers: ' + IntToStr(iRight)  else
    lblWrongDivide.Caption := 'Number of Incorrect Answers: ' + IntToStr(iWrong);
end;

The code my look a mess, but could anyone please help
Thanks

Edited by WingedPanther, 23 September 2008 - 09:09 AM.
add code tags

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 23 September 2008 - 09:11 AM

It looks like you may be approaching this backwards. To get two numbers that divide evenly, instead find to random numbers and multiply them, use one number and the product as your "random numbers". Also, use round/trunc to convert to an integer and store the result in an integer.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Sparky

Sparky

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 24 September 2008 - 08:38 AM

Thanks it works well
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download