v***@gmail.com
2017-08-12 23:02:38 UTC
So here's the thing. I'm not good at coding. Never really will be. Programming in C is not for me. However, for some reason, I'm in a C class and it's mandatory. Don't know why.
This is my assignment.
Loan Calculator Program
Write a program that lets the user enter the principal, annual interest rate, and number of years duration of a loan. The user can then choose to find out what the monthly payment will be and/or print out a loan repayment table. The user should be able to change the values for the principal, annual interest rate, and number of years duration without re-running the program.
A basic menu for this program would look like this:
Big Bank Loan Calculator
1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table
0. Quit
Sample: If the user entered $10000 for the principal, 9% for the annual interest rate, and 5 years for the duration of the loan, the monthly payment should be $207.58, and the beginning of the repayment table should look like this:
Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
2 9867.42 207.58 74.01 133.58 9733.84
...
12 8485.64 207.58 63.64 143.94 8341.70
Math information you will need:
Here is the formula for computing the loan payment:
payment = principal x rate ________
1 - ( (1+rate) to the power (-m) ) )
Notes:
* rate is the monthly interest rate. For example, a 9% annual rate is 0.75% monthly, which is expressed as 0.0075 when converted to a decimal representation
* m is the number of months; -m is the negative of that number
* there is a pow() function in the math.h library that you can use for computing exponents
* for a $10000 loan over 5 years at 9% (the example above), you'd end up computing: 10000 * 0.0075 / ( 1 - pow(1.0075, -60))
WARNING: You will get a division by zero error if the user sets the interest rate to 0%. Make sure you restrict the annual interest rate to values > 0.
interest paid in a given month = old balance * monthly interest rate
principal paid = payment - interest paid
new balance = old balance - principal paid
NOTE from real life: The formulas given above are for LOANS. In Canada, mortgage payments are calculated slightly differently (interest is calculated "semi-annually, not in advance" -- ask your banker what this means), so don't be alarmed if the calculation given by this program doesn't exactly match your bank's quote on a mortgage payment. (It should match the bank's quote on a regular loan.)
Marks:
The project will be marked out of 25. 17 marks will be for the items listed below. The remaining 8 marks are specific to the project topic and described with each topic.
1. (5 marks) Your code should be clear and easy-to-read. This includes proper indentation, meaningful variable names, ample comments, appropriate use of functions, and good use of constants (#define's). You should have only one statement per line, and there should be blank lines between different sections of your code.
2. (5 marks) Your program should be user friendly. The output should be neatly displayed with accurate spelling. The text should not scroll off the screen before the user has a chance to read it. There should be a clear easy-to-use menu. Input from the user should be checked. The program should work properly when the user gives valid input, and provide useful error messages when the data is invalid.
3. (7 marks) You should make good use of the C programming language. You should have an appropriate variety of data types. Somewhere in your program you should demonstrate that you can use pointers. You should put your function prototypes into a .h file. You must not use global (extern) variables. To communicate variable values between functions, pass them as arguments or return values.
Menu:
2 marks
- Your menu should look very much like the one shown above. If you wish to use letters instead of numbers to label the choices or to centre rather than left-justify the menu, that's fine.
- Your menu should be in a separate function, probably called menu(). menu() should display the menu, get the response from the user, make sure the response is valid, and return the response to the calling function. You may write your menu() function so that it is called like this: choice = menu(); or write it so that it is called like this: menu(&choice); The latter format gives you a chance to demonstrate your ability to user pointers!
Display:
3 marks
- The loan repayment table above was significantly shortened. Yours should include ALL months (right to the end of the repayment period) and should pause after every 12 months so that the user has a chance to read the information before it scrolls off the screen.
Tough Part:
3 marks
- There are a number of ways this program could be improved. Please choose ONE of the following
- Make the math smarter: The calculations of the payment and monthly interest will end of with fractions of pennies. Although it's easy enough to get printf() to DISPLAY only two decimal places, it's a little trickier to get the actual STORED value to have only two decimal places. You can see the round off problem in the second line of the sample repayment table above: The interest and the principal SHOULD add up to the payment, but they're actually over by one cent. The way to solve this is to round to the nearest penny when you compute the monthly payment, and again each time you compute the monthly interest. Here's how to round a value to the nearest penny: payment = (long) (payment*100 + 0.5)/100.0; Be very careful about the placement of the brackets or it won't work! Once you have all these things correct to the penny, you'll notice that the last month won't usually leave a balance of exactly 0.00. Add to or subtract from the last payment amount so that the balance comes to exactly 0.00. (This is what the bank does in real life!)
- Allow the user to print the loan repayment table: When the user asks to display the repayment table, ask whether the information should be displayed on the screen or prepared for printing. Printing directly to the printer works slightly differently on different computers, so rather than actually print to printer, ask the user for a file name to use, and write to that file. The user can later load the file into an editor (e.g., Notepad) and print it from there. Note that you should NOT pause after every 12 entries when you are writing to a file. At the beginning or end of the file, write out what the principal, interest rate, and repayment period were so that if the user has several printouts s/he can tell what each one means.
- Make the menu smarter: Revise the menu so that it looks like this:
Big Bank Loan Calculator
1. Update principal (currently $10000.00)
2. Update annual interest rate (currently 9.00%)
3. Update duration of loan (currently not set)
4. Calculate loan payment
5. Display loan repayment table
0. Quit
To get the menu to display the various values, you will have to pass them to the menu() function. You could use a value of -1 to represent a value that hasn't been set yet since -1 is not a valid choice for any of the values. Your call to menu() will now look something like this: choice = menu(principal, rate, years);
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// list of functions
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
int main(void)
{
int choice;
void count(void);
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");
getchar();
return 0;
}
void count(void)
{
float principal; // principal is the loan amount
while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_b(void)
{
float rate; // rate is the interest amount
while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_c(void)
{
int days; // amount of days int the loan
while(true)
{
printf( "Please enter term of the loan in days: " );
scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_d(void)
{
// loan payment
}
void count_e(void)
{
int columns;
int rows;
for(rows= 1; rows <=6; row++)
{
for(columns= 1; columns <=4; column++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}
I honestly feel like I'm failing big time here...
This is my assignment.
Loan Calculator Program
Write a program that lets the user enter the principal, annual interest rate, and number of years duration of a loan. The user can then choose to find out what the monthly payment will be and/or print out a loan repayment table. The user should be able to change the values for the principal, annual interest rate, and number of years duration without re-running the program.
A basic menu for this program would look like this:
Big Bank Loan Calculator
1. Enter principal
2. Enter annual interest rate
3. Enter duration of loan
4. Calculate loan payment
5. Display loan repayment table
0. Quit
Sample: If the user entered $10000 for the principal, 9% for the annual interest rate, and 5 years for the duration of the loan, the monthly payment should be $207.58, and the beginning of the repayment table should look like this:
Month Old Balance Payment Interest Principal New Balance
1 10000.00 207.58 75.00 132.58 9867.42
2 9867.42 207.58 74.01 133.58 9733.84
...
12 8485.64 207.58 63.64 143.94 8341.70
Math information you will need:
Here is the formula for computing the loan payment:
payment = principal x rate ________
1 - ( (1+rate) to the power (-m) ) )
Notes:
* rate is the monthly interest rate. For example, a 9% annual rate is 0.75% monthly, which is expressed as 0.0075 when converted to a decimal representation
* m is the number of months; -m is the negative of that number
* there is a pow() function in the math.h library that you can use for computing exponents
* for a $10000 loan over 5 years at 9% (the example above), you'd end up computing: 10000 * 0.0075 / ( 1 - pow(1.0075, -60))
WARNING: You will get a division by zero error if the user sets the interest rate to 0%. Make sure you restrict the annual interest rate to values > 0.
interest paid in a given month = old balance * monthly interest rate
principal paid = payment - interest paid
new balance = old balance - principal paid
NOTE from real life: The formulas given above are for LOANS. In Canada, mortgage payments are calculated slightly differently (interest is calculated "semi-annually, not in advance" -- ask your banker what this means), so don't be alarmed if the calculation given by this program doesn't exactly match your bank's quote on a mortgage payment. (It should match the bank's quote on a regular loan.)
Marks:
The project will be marked out of 25. 17 marks will be for the items listed below. The remaining 8 marks are specific to the project topic and described with each topic.
1. (5 marks) Your code should be clear and easy-to-read. This includes proper indentation, meaningful variable names, ample comments, appropriate use of functions, and good use of constants (#define's). You should have only one statement per line, and there should be blank lines between different sections of your code.
2. (5 marks) Your program should be user friendly. The output should be neatly displayed with accurate spelling. The text should not scroll off the screen before the user has a chance to read it. There should be a clear easy-to-use menu. Input from the user should be checked. The program should work properly when the user gives valid input, and provide useful error messages when the data is invalid.
3. (7 marks) You should make good use of the C programming language. You should have an appropriate variety of data types. Somewhere in your program you should demonstrate that you can use pointers. You should put your function prototypes into a .h file. You must not use global (extern) variables. To communicate variable values between functions, pass them as arguments or return values.
Menu:
2 marks
- Your menu should look very much like the one shown above. If you wish to use letters instead of numbers to label the choices or to centre rather than left-justify the menu, that's fine.
- Your menu should be in a separate function, probably called menu(). menu() should display the menu, get the response from the user, make sure the response is valid, and return the response to the calling function. You may write your menu() function so that it is called like this: choice = menu(); or write it so that it is called like this: menu(&choice); The latter format gives you a chance to demonstrate your ability to user pointers!
Display:
3 marks
- The loan repayment table above was significantly shortened. Yours should include ALL months (right to the end of the repayment period) and should pause after every 12 months so that the user has a chance to read the information before it scrolls off the screen.
Tough Part:
3 marks
- There are a number of ways this program could be improved. Please choose ONE of the following
- Make the math smarter: The calculations of the payment and monthly interest will end of with fractions of pennies. Although it's easy enough to get printf() to DISPLAY only two decimal places, it's a little trickier to get the actual STORED value to have only two decimal places. You can see the round off problem in the second line of the sample repayment table above: The interest and the principal SHOULD add up to the payment, but they're actually over by one cent. The way to solve this is to round to the nearest penny when you compute the monthly payment, and again each time you compute the monthly interest. Here's how to round a value to the nearest penny: payment = (long) (payment*100 + 0.5)/100.0; Be very careful about the placement of the brackets or it won't work! Once you have all these things correct to the penny, you'll notice that the last month won't usually leave a balance of exactly 0.00. Add to or subtract from the last payment amount so that the balance comes to exactly 0.00. (This is what the bank does in real life!)
- Allow the user to print the loan repayment table: When the user asks to display the repayment table, ask whether the information should be displayed on the screen or prepared for printing. Printing directly to the printer works slightly differently on different computers, so rather than actually print to printer, ask the user for a file name to use, and write to that file. The user can later load the file into an editor (e.g., Notepad) and print it from there. Note that you should NOT pause after every 12 entries when you are writing to a file. At the beginning or end of the file, write out what the principal, interest rate, and repayment period were so that if the user has several printouts s/he can tell what each one means.
- Make the menu smarter: Revise the menu so that it looks like this:
Big Bank Loan Calculator
1. Update principal (currently $10000.00)
2. Update annual interest rate (currently 9.00%)
3. Update duration of loan (currently not set)
4. Calculate loan payment
5. Display loan repayment table
0. Quit
To get the menu to display the various values, you will have to pass them to the menu() function. You could use a value of -1 to represent a value that hasn't been set yet since -1 is not a valid choice for any of the values. Your call to menu() will now look something like this: choice = menu(principal, rate, years);
and this is the code I've come up with (keep in mind I DO NOT KNOW what I'm doing really, I understand to a certain point and that's it)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// list of functions
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
void count_b(void);
void count_c(void);
void count_d(void);
void count_e(void);
int main(void)
{
int choice;
void count(void);
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : count();
break;
case 'b' : count_b();
break;
case 'c' : count_c();
break;
case 'd' : count_d();
break;
case 'e' : count_e();
break;
default : printf("Program error!\n");
break;
}
fflush(stdin);
}
printf("Bye.\n");
getchar();
return 0;
}
void count(void)
{
float principal; // principal is the loan amount
while(true)
{
printf( "Please enter the loan principal: " );
scanf("%f", &principal );
if ( principal == -1 )
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_b(void)
{
float rate; // rate is the interest amount
while(true)
{
printf("Please enter interest rate: ");
scanf("%f", &rate );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_c(void)
{
int days; // amount of days int the loan
while(true)
{
printf( "Please enter term of the loan in days: " );
scanf("%d", &days );
while (getchar() != '\n')
continue;
{
return 0;
}
}
}
void count_d(void)
{
// loan payment
}
void count_e(void)
{
int columns;
int rows;
for(rows= 1; rows <=6; row++)
{
for(columns= 1; columns <=4; column++)
{
printf("Month Old Balance Payment Interest Principal New Balance", columns);
}
printf("\n");
}
return 0;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice: \n");
printf("a. Enter principal b. Enter annual interest rate\n");
printf("c. Enter duration of loan d. Calculate loan payment\n");
printf("e. Display loan repayment table q. Quit\n");
ch = get_first();
while ( ( ch < 'a' || ch > 'e') && ch != 'q')
{
printf("Please enter a selection or q (to quit): ");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar(); // read next character
while (getchar() != '\n')
continue; // skip rest of line
return ch;
}
I honestly feel like I'm failing big time here...