Could you tell me what's wrong of my answer?

Thread Solved
Reply

Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Could you tell me what's wrong of my answer?

 
0
  #1
Jul 5th, 2009
Dear Sir,

Please find the attached document for your reference. Could you tell me what is wrong of my answer?

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. program Project1;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7. SysUtils;
  8.  
  9. var
  10. balance, posNeg, positive, negative: integer;
  11.  
  12. begin
  13. randomize;
  14. writeln('Get average balance');
  15. balance:=random(10000);
  16. posNeg:=random(2);
  17. if posNeg=0 then
  18. begin
  19. balance:=balance*-1-30;
  20. end
  21. else
  22. begin
  23. writeln('Positive balance.');
  24. balance:=balance*1.03;
  25. end;
  26. writeln('Your balance is ', balance);
  27. readln;
  28. end.

Cheers,
Attached Files
File Type: doc Lab 4 Notesnewi.doc (164.5 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: Sub Xero is an unknown quantity at this point 
Solved Threads: 2
Sub Xero Sub Xero is offline Offline
Newbie Poster

Re: Could you tell me what's wrong of my answer?

 
0
  #2
Jul 5th, 2009
Your main problem is 'balance' should be a real type, as multiplying it by 1.03 will return a non integer value. Here's how I'd do it:

  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. var
  9. posNeg: integer;
  10. balance: Real; // balance must be a real type (supports decimal places)
  11.  
  12. begin
  13. try
  14. Randomize;
  15. Writeln('Get average balance');
  16. posNeg:=random(2);
  17. // Set posNeg to 1 or -1
  18. if posNeg = 0 then
  19. posNeg:=-1
  20. else
  21. posNeg:=1;
  22. // %.2f limits the float to 2 decimal places
  23. balance:=StrToFloat(Format('%.2f',[posNeg*(random+random(10000))]));
  24. Writeln('Average balance is '+FloatToStr(balance));
  25. if posNeg = -1 then
  26. begin
  27. Writeln('Negative balance.');
  28. balance:=balance-20;
  29. end else
  30. begin
  31. Writeln('Positive balance.');
  32. balance:=balance*1.03;
  33. end;
  34. // Format to 2 decimal places
  35. Writeln('Your new balance is ', Format('%.2f',[balance]));
  36. readln;
  37. except
  38. on E:Exception do
  39. Writeln(E.Classname, ': ', E.Message);
  40. end;
  41. end.

Hope that helps.

Sub Xero
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: Could you tell me what's wrong of my answer?

 
0
  #3
Jul 6th, 2009
Hi,

Thank you for your kind reply and answer. Can I do at the following way?

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. program Hoard_It;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7. SysUtils;
  8.  
  9. var
  10. balance: real;
  11. posNeg, positive, negative: integer;
  12.  
  13. begin
  14. randomize;
  15. writeln('Get average balance');
  16. balance:=random(10000);
  17. posNeg:=random(2);
  18. if posNeg=0 then
  19. begin
  20. balance:=balance*-1-30;
  21. end
  22. else
  23. begin
  24. writeln('Positive balance.');
  25. balance:=balance*1.03;
  26. end;
  27. writeln('Your balance is ', balance:6:2);
  28. readln;
  29. end.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: Could you tell me what's wrong of my answer?

 
0
  #4
Jul 6th, 2009
Originally Posted by Sub Xero View Post
Your main problem is 'balance' should be a real type, as multiplying it by 1.03 will return a non integer value. Here's how I'd do it:

  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. var
  9. posNeg: integer;
  10. balance: Real; // balance must be a real type (supports decimal places)
  11.  
  12. begin
  13. try
  14. Randomize;
  15. Writeln('Get average balance');
  16. posNeg:=random(2);
  17. // Set posNeg to 1 or -1
  18. if posNeg = 0 then
  19. posNeg:=-1
  20. else
  21. posNeg:=1;
  22. // %.2f limits the float to 2 decimal places
  23. balance:=StrToFloat(Format('%.2f',[posNeg*(random+random(10000))]));
  24. Writeln('Average balance is '+FloatToStr(balance));
  25. if posNeg = -1 then
  26. begin
  27. Writeln('Negative balance.');
  28. balance:=balance-20;
  29. end else
  30. begin
  31. Writeln('Positive balance.');
  32. balance:=balance*1.03;
  33. end;
  34. // Format to 2 decimal places
  35. Writeln('Your new balance is ', Format('%.2f',[balance]));
  36. readln;
  37. except
  38. on E:Exception do
  39. Writeln(E.Classname, ': ', E.Message);
  40. end;
  41. end.

Hope that helps.

Sub Xero
Hi,

Could you tell me what about the different between 'integer' and 'real'?

Cheers,

Tony
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Could you tell me what's wrong of my answer?

 
0
  #5
Jul 6th, 2009
another alternative
  1. {Little tutorial for real type}
  2. {real type is stores floating point numbers like 3.14 or 0.1234}
  3. {The generic type Real is equivalent to Double.
  4. Type:Real, Range:5.0 x 10^-324 .. 1.7 x 10^308
  5. Significant digits:15-16 Size in bytes:8}
  6. {if you want to divide 2 numbers then the result always real}
  7. {the same result when the first number is decimal and the second is real}
  8. {so if you work with real numbers then the result always real }
  9.  
  10.  
  11. Program get_the_average_ballance;
  12.  
  13. {$APPTYPE CONSOLE}
  14.  
  15. Uses
  16. SysUtils;
  17.  
  18. Var balance,interest:Real;
  19. posneg:Integer;
  20.  
  21. Begin {main}
  22. WriteLn('Get the average ballance...');
  23.  
  24. Randomize;
  25.  
  26. balance:=Random(2000); {get the random number}
  27.  
  28. posneg:=Random(2); {again}
  29.  
  30. If posneg = 0 Then Begin {if true then}
  31. {balance will be negative}
  32. balance:=balance*-1;
  33. {and substarct $20 from the money}
  34. balance:=balance-20;
  35. {alert the user for the changes}
  36. WriteLn('You lose $20,sorry');
  37. End
  38. Else Begin {in the other way}
  39. interest:=(balance/100)*3; {it is the same as balance*1.03 the result is real}
  40. balance:=balance+interest;{add the interest to the balance}
  41. WriteLn('You got 3%'); {alert the user}
  42. End;
  43. WriteLn('The balance is: ',balance:0:0);
  44. {the colon (:) after balance is the field widenes}
  45. {the first number after the colon is the number of numbers before the dot}
  46. {and the second number after the second colon is the number of numbers}
  47. {after the dot}
  48. ReadLn; {press the enter button}
  49. End.{end of main}
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: Sub Xero is an unknown quantity at this point 
Solved Threads: 2
Sub Xero Sub Xero is offline Offline
Newbie Poster

Re: Could you tell me what's wrong of my answer?

 
0
  #6
Jul 6th, 2009
I like this line:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. WriteLn('The balance is: ',balance:0:0);
Very neat.

Sub Xero
Last edited by Sub Xero; Jul 6th, 2009 at 2:32 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Could you tell me what's wrong of my answer?

 
0
  #7
Jul 6th, 2009
Originally Posted by Sub Xero View Post
I like this line:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. WriteLn('The balance is: ',balance:0:0);
Very neat.

Sub Xero
simple pascal syntax...but delphi accepts it too...
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC