Recent

Author Topic: Need help with a program i've been trying to make  (Read 2138 times)

guest58006

  • Guest
Need help with a program i've been trying to make
« on: October 04, 2015, 09:13:32 pm »
So I know this program IS simple, but I am still a beginner and it would be nice for someone to help me find the mistake and maybe even fix it myself so I should know how to avoid it in the future.
Basically what I'm trying to create is a simple program calculating how much money you will have after depositing your money for a certain period of time at a certain bank rate. The only condition it being that I cannot leave it after the sum doubled itself. I'm trying to do it as simple as possible, and also only use repeat until/while/for/etc.
Here's the program:
Code: Pascal  [Select][+][-]
  1. program bank;
  2. var
  3.   k,n : real;
  4.   a : integer;
  5. begin
  6. writeln ('Introduce the annual rate');
  7. readln (k);
  8. writeln ('Introduce the initial sum');
  9. readln (n);
  10. writeln ('Introduce the number of years');
  11. readln (a);
  12. while n <= 2*n do
  13. begin
  14. n:= n+(k*n)/100;
  15. a:= a+1;
  16. writeln (a, '  ',n:0:5);
  17. end;
  18. writeln ('Press ENTER to continue...');
  19. readln;
  20. end.

EDIT: Forgot to add that I always get error 205, and the program runs properly, but after adding all the values in, it goes through a massive amount of text and just stops.

balazsszekely

  • Guest
Re: Need help with a program i've been trying to make
« Reply #1 on: October 04, 2015, 09:29:50 pm »
You force your program into a runaway loop. The initial condition is n < 2*n then you increase n, so the condition is never met.
It's similar to:
Code: Pascal  [Select][+][-]
  1. while 1 < 2 do
  2. begin
  3.   //do something
  4. end;
Try this instead:
Code: Pascal  [Select][+][-]
  1.  m := 2*n;
  2.  while n <= m do
  3.  begin
  4.    //...
  5.  end;                
  6.  

guest58006

  • Guest
Re: Need help with a program i've been trying to make
« Reply #2 on: October 04, 2015, 09:51:24 pm »
Actually I HAD m in my program at the start, but thought that too many variables wouldn't be that great. Will try and then reply again.

guest58006

  • Guest
Re: Need help with a program i've been trying to make
« Reply #3 on: October 04, 2015, 09:55:04 pm »
You force your program into a runaway loop. The initial condition is n < 2*n then you increase n, so the condition is never met.
It's similar to:
Code: Pascal  [Select][+][-]
  1. while 1 < 2 do
  2. begin
  3.   //do something
  4. end;
Try this instead:
Code: Pascal  [Select][+][-]
  1.  m := 2*n;
  2.  while n <= m do
  3.  begin
  4.    //...
  5.  end;                
  6.  

OK, this has helped me. But as an answer I get the number AND the decimals. I thought 0:5 should've removed them? Am I doing something wrong? What is the actual way to remove the decimal part of a real number? (Sorry for these questions but we're going fast through all the subjects and sometimes I find it relatively hard to understand everything.

balazsszekely

  • Guest
Re: Need help with a program i've been trying to make
« Reply #4 on: October 04, 2015, 10:01:30 pm »
Quote
@aerowei
What is the actual way to remove the decimal part of a real number?
Use the function Trunc(http://www.freepascal.org/docs-html/rtl/system/trunc.html)

 

TinyPortal © 2005-2018