John said:
I've not been dealing with C too long and could do with a hand with
this;
I'm trying to design a program dealing with student loan repayment and
mainly how long its going to take me to pay back £12000, borrowed
£4000 a year. How will I write in the fractions of code for the 3.1%
apr, my salary threshold (£15000) when I start paying the money back
at £15 per week and just the general layout of the program?
Normally you would design top-down and write code bottom-up. This isn't
specific to C, and it isn't the only design methodology, but it is a good
way to start.
You need to input the amounts borrowed each year (I assume they charge
interest from day one), the amounts you are earning each month from now till
the loan is paid off, and the interest they charge on the loan.
You need to think how you are going to input this data - the best bet is
probably an ASCII input file, with the amounts borrowed at the top, and then
the amount you earn in the format 20,000/year 06/2004-08/2008.
If you use double precision floating point number throughout there shouldn't
be any problem with accuracy.
You need to calculate interest each month or year (depending on the rule)
using the formula
P (principal) = P * (1 + rate/100)
You need to calculate your repayment based on the rules, and your monthly
salary.
So write two functions.
double calcinterest(double P, double ratepercent)
double calcrepayment(double P, double salary)
Now you will need to put these two functions in a loop.
while(loan > 0)
{
/* calculate salary for this month from the input data */
salary = ?;
/* reduce loan by the repayment */
loan -= calcrepayment()
/* add the interest */
loan += calcinterest();
/* keep a counter of hown many months it took */
nmonths++;
}
You can test this loop with dummy salary / loan data.
Now we need to handle the input. This is always the most difficult part,
because we have to handle malformed data.
You know you won't work past 65-70, so we can have a static array of 50 * 12
months worth of salaries.
You then need to fill up the array with salary details in your input file,
checking for illegal entries. If you say that each line give salary, start
month, and end month, this shouldn't be impossible.
Write a function
int fillsalary(FILE *fp, double *months, int maxmonths)
return 0 on success, a negative number to indicate various types of failure.
Now we should pretty much have your program. Post code if you are stuck with
any of this.