| | |
Could you tell me what's wrong of my answer?
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 93
Reputation:
Solved Threads: 0
Dear Sir,
Please find the attached document for your reference. Could you tell me what is wrong of my answer?
Cheers,
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)
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var balance, posNeg, positive, negative: integer; begin randomize; writeln('Get average balance'); balance:=random(10000); posNeg:=random(2); if posNeg=0 then begin balance:=balance*-1-30; end else begin writeln('Positive balance.'); balance:=balance*1.03; end; writeln('Your balance is ', balance); readln; end.
Cheers,
•
•
Join Date: Jul 2009
Posts: 7
Reputation:
Solved Threads: 2
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:
Hope that helps.
Sub Xero
delphi Syntax (Toggle Plain Text)
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var posNeg: integer; balance: Real; // balance must be a real type (supports decimal places) begin try Randomize; Writeln('Get average balance'); posNeg:=random(2); // Set posNeg to 1 or -1 if posNeg = 0 then posNeg:=-1 else posNeg:=1; // %.2f limits the float to 2 decimal places balance:=StrToFloat(Format('%.2f',[posNeg*(random+random(10000))])); Writeln('Average balance is '+FloatToStr(balance)); if posNeg = -1 then begin Writeln('Negative balance.'); balance:=balance-20; end else begin Writeln('Positive balance.'); balance:=balance*1.03; end; // Format to 2 decimal places Writeln('Your new balance is ', Format('%.2f',[balance])); readln; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.
Hope that helps.
Sub Xero
•
•
Join Date: Feb 2009
Posts: 93
Reputation:
Solved Threads: 0
Hi,
Thank you for your kind reply and answer. Can I do at the following way?
Thank you for your kind reply and answer. Can I do at the following way?
Pascal and Delphi Syntax (Toggle Plain Text)
program Hoard_It; {$APPTYPE CONSOLE} uses SysUtils; var balance: real; posNeg, positive, negative: integer; begin randomize; writeln('Get average balance'); balance:=random(10000); posNeg:=random(2); if posNeg=0 then begin balance:=balance*-1-30; end else begin writeln('Positive balance.'); balance:=balance*1.03; end; writeln('Your balance is ', balance:6:2); readln; end.
•
•
Join Date: Feb 2009
Posts: 93
Reputation:
Solved Threads: 0
•
•
•
•
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)
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var posNeg: integer; balance: Real; // balance must be a real type (supports decimal places) begin try Randomize; Writeln('Get average balance'); posNeg:=random(2); // Set posNeg to 1 or -1 if posNeg = 0 then posNeg:=-1 else posNeg:=1; // %.2f limits the float to 2 decimal places balance:=StrToFloat(Format('%.2f',[posNeg*(random+random(10000))])); Writeln('Average balance is '+FloatToStr(balance)); if posNeg = -1 then begin Writeln('Negative balance.'); balance:=balance-20; end else begin Writeln('Positive balance.'); balance:=balance*1.03; end; // Format to 2 decimal places Writeln('Your new balance is ', Format('%.2f',[balance])); readln; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.
Hope that helps.
Sub Xero
Could you tell me what about the different between 'integer' and 'real'?
Cheers,
Tony
another alternative 

delphi Syntax (Toggle Plain Text)
{Little tutorial for real type} {real type is stores floating point numbers like 3.14 or 0.1234} {The generic type Real is equivalent to Double. Type:Real, Range:5.0 x 10^-324 .. 1.7 x 10^308 Significant digits:15-16 Size in bytes:8} {if you want to divide 2 numbers then the result always real} {the same result when the first number is decimal and the second is real} {so if you work with real numbers then the result always real } Program get_the_average_ballance; {$APPTYPE CONSOLE} Uses SysUtils; Var balance,interest:Real; posneg:Integer; Begin {main} WriteLn('Get the average ballance...'); Randomize; balance:=Random(2000); {get the random number} posneg:=Random(2); {again} If posneg = 0 Then Begin {if true then} {balance will be negative} balance:=balance*-1; {and substarct $20 from the money} balance:=balance-20; {alert the user for the changes} WriteLn('You lose $20,sorry'); End Else Begin {in the other way} interest:=(balance/100)*3; {it is the same as balance*1.03 the result is real} balance:=balance+interest;{add the interest to the balance} WriteLn('You got 3%'); {alert the user} End; WriteLn('The balance is: ',balance:0:0); {the colon (:) after balance is the field widenes} {the first number after the colon is the number of numbers before the dot} {and the second number after the second colon is the number of numbers} {after the dot} ReadLn; {press the enter button} 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...
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...
•
•
Join Date: Jul 2009
Posts: 7
Reputation:
Solved Threads: 2
I like this line:
Very neat.
Sub Xero
Pascal and Delphi Syntax (Toggle Plain Text)
WriteLn('The balance is: ',balance:0:0);
Sub Xero
Last edited by Sub Xero; Jul 6th, 2009 at 2:32 am.
•
•
•
•
I like this line:
Very neat.Pascal and Delphi Syntax (Toggle Plain Text)
WriteLn('The balance is: ',balance:0:0);
Sub Xero
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...
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...
![]() |
Similar Threads
- what is wrong with the check? (JavaScript / DHTML / AJAX)
- Getting wrong answer while returning address (but right if I return value) (C)
- Karatsuba Wrong Answer After 6 Decimal digits (C++)
- seql 2000 Query returns wrong results (MS SQL)
- Could you tell me what is wrong of my answer? (Pascal and Delphi)
- Double variable type, unexpected answer (C++)
- Can't figure out where my ACM sollution gives wrong answer. (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: May I ask you a question on Delphi?
- Next Thread: restricting decimal places on floating point values
| Thread Tools | Search this Thread |





