943,829 Members | Top Members by Rank

Ad:
Jul 5th, 2009
0

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

Expand Post »
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, 9 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 5th, 2009
0

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

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:

delphi Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Sub Xero is offline Offline
7 posts
since Jul 2009
Jul 6th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 6th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Sub Xero ...
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:

delphi Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 6th, 2009
0

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

another alternative
delphi Syntax (Toggle Plain Text)
  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}
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 6th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Sub Xero is offline Offline
7 posts
since Jul 2009
Jul 6th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Sub Xero ...
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...
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: May I ask you a question on Delphi?
Next Thread in Pascal and Delphi Forum Timeline: restricting decimal places on floating point values





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC