2
\$\begingroup\$

Lets say we need to write an algorithm for giving money to the bank. We need to know interest rate, money which we put every year on our account, and amount of money we want to get. F.e int. rate is 1%, money we put every year is 1000 (or whatever) and we want to get 10000 (not more). It'd look like this:

  1. MoneyOnAcc=1000*1.01
  2. MoneyOnAcc=(MoneyOnAcc+1000)*1.01
  3. Until MoneyOnAcc reaches 10000 (we want to print amount what was before 10000 - 9462.21)

and so on. Here is my code in Pascal.

program whilel;

var interestRate:real;
    moneyEveryYear:real;
    accMoneyOut:real;
    totalAmount:real;
    accMoneyControl:real;
begin
readln(moneyEveryYear);
readln(interestRate);
readln(totalAmount);

while accMoneyOut<totalAmount do begin
    accMoneyControl:=(accMoneyOut+moneyEveryYear)*((interestRate/100)+1);

    if accMoneyControl>totalAmount then break;

    accMoneyOut:=(accMoneyOut+moneyEveryYear)*((interestRate/100)+1);



end;
writeln(accMoneyOut:6:2);
readln();
end.

How can I improve this algorithm? Maybe some better logic will help.

\$\endgroup\$
  • \$\begingroup\$ Where did you find the docs for your use of WriteLn(GAccMoneyOut:6:2)? \$\endgroup\$ – cpicanco Nov 12 '15 at 22:52
  • \$\begingroup\$ It was in a book :-)) \$\endgroup\$ – Flash Nov 14 '15 at 10:54
  • \$\begingroup\$ Would love to know which book :) \$\endgroup\$ – cpicanco Nov 14 '15 at 19:53
  • \$\begingroup\$ I'm from Slovakia so it's a Slovak book. Why are you asking ?? Is there something wrong? :) \$\endgroup\$ – Flash Nov 14 '15 at 22:26
  • \$\begingroup\$ I am genuinely curious about it! I didn't know it was possible in this straightforward way. \$\endgroup\$ – cpicanco Nov 16 '15 at 21:03
1
\$\begingroup\$
  • Care about indentation. Pascal is usually written with 2 space indentation.

  • Repeat is a posttest loop, and your code needs to run at least once, so why not a repeat..until loop. You can save a begin..end block, as well.

  • you should explain to your self the inputs, unite measures and all the info you need to properly insert the requested variables.

  • in pascal, prefixing your own variables is good practice for complex code (not your case). Take a look at this answer. I choose to prefix our global variables with 'G'; Also, functions and procedures goes with CapitalLetters.

  • Avoid repetition, I choose to use GAccMoneyOut:=GMoneyControl; instead of repeating the code to raise the money.

  • Care about the user, you should use readkey and not readln at the end. ReadLn increases the chance of getting lost.

Putting all together:

program whilel;

uses Crt;

var
  GInterestRate,
  GMoneyEveryYear,
  GTotalAmount,
  GMoneyControl,
  GAccMoneyOut:real;

begin
  WriteLn('Enter the porcentage interest rate');
  ReadLn(GInterestRate);

  WriteLn('Enter the money you will put every year into the account');
  ReadLn(GMoneyEveryYear);

  WriteLn('Enter the money you want to get');
  ReadLn(GTotalAmount);

  GAccMoneyOut:=0;
  GMoneyControl:=0;
  repeat
    GMoneyControl:=(GAccMoneyOut+GMoneyEveryYear)*((GInterestRate/100)+1);
    if GMoneyControl>GTotalAmount then
      Break
    else
      GAccMoneyOut:=GMoneyControl;
  until GAccMoneyOut=GTotalAmount;

  WriteLn('Result is:');
  WriteLn(GAccMoneyOut:6:2);

  WriteLn('Press any key to exit...'); 
  Readkey;
end.
\$\endgroup\$
  • \$\begingroup\$ I'm using 4 space indentation from Python ,good to know :) That variable declaration at the beginning means that all of the variables are real? Thanks :-)) \$\endgroup\$ – Flash Nov 14 '15 at 10:56
  • \$\begingroup\$ Yes, all are real. \$\endgroup\$ – cpicanco Nov 14 '15 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.