• C

C Program Problemo

Hello,

I am fairly new to C and have been working on this code for a few days now, trying to achieve a program that calculates a mortgage with compound interest.

I decided to play around with arrays instead of trying to display the results in a table, and they seem to be displaying my results as I want them to be.

However, I have played around far too much with the formulas/variables I made and just end up getting crazy results.  I'm aiming to display one year of values at a time (12 lines), with three columns: amount to pay, amount paid and interest charged.  The amount to pay should constantly be going down, amount paid going up originally then down as the 'to pay' goes down (and less interest should be charged because the amount to pay is less) and the interest charged should go down as the amount to pay goes down.  I think that makes sense...sorry, you can tell how confused I've got!

If the mortgage is taken over several years, I tried to make it so that it would display a year at a time when '1' was pressed...which did work...but now it seems to drop out of the program.

Could you please have a look at my code and point me in the right direction?  Many thanks!

#include <stdio.h>
//Only IO is required
void main ()

{
      int amount, borrowed, payment, months, j, k, l=0, years, exit, mon;
      float interest, topay[12], amountpaid[12], interestpaid,monthlypay,rollinterest,totalint[12];
//Declaring all variables, characters, integers, floats, arrays
      
      printf("Welcome to the Mortgage Calculator \n\r\n\r");
//Welcome message
      do
      {      
      printf("\n\r\n\r Please enter the amount you would like to borrow: \n\r (This amount should be greater than or equal to 1000) \n\r\n\r ");
      scanf("%d", & amount);
      }
            while(amount <= 999);
            
      printf("\n\r\n\r Over how many years do you wish to pay this amount: \n\r\n\r");
      scanf("%d", & years);
      
      do
      {
      printf("\n\r\n\r Please enter the applicable interest rate: \n\r");
      scanf("%f", & interest);
      }
            while(interest >= 10);

      do
      {
      printf("\n\r\n\r How much will your initial payment be: \n\r\n\r ");
      scanf("%d", & payment);
      }
            while(payment >= amount);
            

      printf("\t\t\t Mortgage Calculator \n\r");
      printf("\t\t\t ******************* \n\n\r");
      printf("Month   Amount to pay    Amount paid  Interest Charged \n\r\n\r");

      for(l = 0; l<12;l++)
      {
            borrowed = amount-payment;
            topay[j] = borrowed - amountpaid[k];
            months = years * 12;
            rollinterest = (topay[j]/100)*interest;
            monthlypay = topay[j] / months;
            amountpaid[k] = monthlypay+rollinterest;
      
            totalint[l]=rollinterest;
            mon=mon++;
            printf("\n %d \t %f \t %f \t %f",mon,topay[j],amountpaid[k],totalint[l]);
      }
                  
      printf("\n\r\n\r You have paid %f this year \n",amountpaid[k]);
      printf("\n You have %f of your mortgage left to pay \n",topay[j]);
      printf("\n The total interest you have been charged this year is %f \n",totalint[l]);
      
      printf("\nPlease enter 1 to continue! \n\r");
      scanf("%d",&exit);
}
sarahthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
Hi sarahth,

OK! Problems:

1.      for(l = 0; l<12;l++)
     {
          borrowed = amount-payment;
          topay[j] = borrowed - amountpaid[k];

You use j and k before setting then to anything. Not sure what you intended here. I suspect you mean 'l' but I'm not sure.

I'll keep looking but this is certainly a major problem. Post the fixed code as soon as you sort it.

Paul
0
PaulCaswellCommented:
Hi sarahth,

Looking at the code further, I think I can see where you went awry. You only need one loop variable, thats the month number. So get rid of all the others and just use mon!

In fact, you dont need any arrays at all since each months values are calculated from the previous months' so I'd suggest you make each of the arrays just single values, or am I wrong?

Anyway, have a play around with my suggestions and post what you've got and we'll look at it again. Or if you get stuck, do the same! Good luck!

Paul
0
sarahthAuthor Commented:
for(l = 0; l<12;l++)

I did do this for j and k, as this was in one of my textbook examples on how to set up arrays...but they didnt really seem to be doing anything.  I have chopped this code around so much and it was pretty messed up in the first place!

Thanks Paul.
0
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

PaulCaswellCommented:
Hi sarahth,

Try using 'l' all the way through and see what happens. That would be a good start! Post the whole code then and tell us what is still wrong.

We have to treat homework/coursework questions a bit differently than usual to ensure you get the help you need so forgive us if we seem to be trying to get you to do the work ;-)

What do you expect to happen at the end. You suggest that it should go back and do another year. Is that what you want?

Paul
0
sarahthAuthor Commented:
I understand :)

I substituted everything for l's and now the amount to pay column deducts the initial payment off the total amount accurately and just repeats the value over and over.  The amount paid section definitely has something wrong and also repeats the same value, and the interest stays the same throughout.

I did get the program to display the yearly results page by page (and this should happen until the mortgage runs out) but mine just ran forever without dropping out.  Now I'm not sure what I've done to stop it doing that!

hmm...

#include <stdio.h>
//Only IO is required
void main ()

{
     int amount, borrowed, payment, months, l=0, years, exit, mon;
     float interest, topay[12], amountpaid[12], interestpaid,monthlypay,rollinterest,totalint[12];
//Declaring all variables, characters, integers, floats, arrays
     
     printf("Welcome to the Mortgage Calculator \n\r\n\r");
//Welcome message
     do
     {    
     printf("\n\r\n\r Please enter the amount you would like to borrow: \n\r (This amount should be greater than or equal to 1000) \n\r\n\r ");
     scanf("%d", & amount);
     }
          while(amount <= 999);
         
     printf("\n\r\n\r Over how many years do you wish to pay this amount: \n\r\n\r");
     scanf("%d", & years);
     
     do
     {
     printf("\n\r\n\r Please enter the applicable interest rate: \n\r");
     scanf("%f", & interest);
     }
          while(interest >= 10);

     do
     {
     printf("\n\r\n\r How much will your initial payment be: \n\r\n\r ");
     scanf("%d", & payment);
     }
          while(payment >= amount);
         

     printf("\t\t\t Mortgage Calculator \n\r");
     printf("\t\t\t ******************* \n\n\r");
     printf("Month   Amount to pay    Amount paid  Interest Charged \n\r\n\r");

     for(l = 0; l<12;l++)
     {
          borrowed = amount-payment;
          topay[l] = borrowed - amountpaid[l];
          months = years * 12;
          rollinterest = (topay[l]/100)*interest;
          monthlypay = topay[l] / months;
          amountpaid[l] = monthlypay+rollinterest;
     
          totalint[l]=rollinterest;
          mon=mon++;
          printf("\n %d \t %f \t %f \t %f",mon,topay[l],amountpaid[l],totalint[l]);
     }
               
     printf("\n\r\n\r You have paid %f this year \n",amountpaid[l]);
     printf("\n You have %f of your mortgage left to pay \n",topay[l]);
     printf("\n The total interest you have been charged this year is %f \n",totalint[l]);
     
     printf("\nPlease enter 1 to continue! \n\r");
     scanf("%d",&exit);
}
0
PaulCaswellCommented:
Hi sarahth,

I'm at work now so I cant help too much right now but here's a few pointers:

>>and just repeats the value over and over
That problem is here.

          topay[l] = borrowed - amountpaid[l];

You are calculating topay from the original 'borrowed' (which obviously doesnt change) and amountpaid[i] which you havent set yet.

try moving the line:

         amountpaid[l] = monthlypay+rollinterest;

up to above this line as this is the line that calculates amountpaid[l].

Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hiteshgupta1Commented:
U need to initialize the variables
variables like topay[],amountpaid[],rollinterests,monthlypay are dependent on each other
but u haven't intialized them
i guess ,value for amountpaid[] will be zero intitallly
so u can set it to zero before that for loop........
i..e.
for(l = 0; l<12;l++)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.