Thread: got my compound interest calc working but it has an array error after completing!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    got my compound interest calc working but it has an array error after completing!

    I wrote a simple program to compound interest over 25 years. interest compounded monthly. here is the code. everything works great it prints out all information but freezes after making all calcs. anyone able to see were I have strayed?
    Code:
    /* Program 5.2 from PTRTUT10.HTM   6/13/97 */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct database {   /*defines struct that will be 1 year and contain 12 months*/
           float month[12];
           };
           int main(){
               struct database year[25];     /*forms 25 structures which will be equal to 25 year I want to track*/
               struct database *point[25];   /*forms pointers that I havent used yet but want around in case I decide to create more functions*/
               
               int x,y; /*x and y will be used for counters in the loops*/
               float interest,lastvalue, principal;  /* The main variables for our program*/
                    for(x=1;x<=25;x++){ /*assigns points to corresponding structures again for future use*/
                                       point[x]=&year[x];
                                       }
            for(x=1;x<=25;x++){ /*loop for clearing all variables and setting to zero will move to a seperate function once things are operating properly*/
                                  for(y=1;y<=12;y++){
                                  year[x].month[y]=0;
                                  printf("year %d month %d value %f \n",x,y,year[x].month[y]);
                                  }
                                  }
    system ("cls");                              
    printf("please enter what your principal is \n");
    scanf("%f",&principal);
    printf("What is your interest rate?");
    scanf("%f",&interest);
    interest=(interest/100) + 1;
    lastvalue=principal;
    for(x=1;x<=25;x++){
                                  for(y=1;y<=12;y++){ /*main loop to compound the interest*/
                                  year[x].month[y]=lastvalue*interest;
                                  lastvalue=year[x].month[y];
                                  printf("year %d month %d value %f \n",x,y,year[x].month[y]);
                                  }
                                  }
               getchar();  
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Arrays start at zero, not one. This means a for loop over 25 items should look like this:
    Code:
    for (x=0; x < 25; x++)
    Same deal for 12 items obviously.

    To get furthur help, please first fix up the indentation of that code so that it isn't going left and right all over the place. Replace tabs with a fixed number of spaces before posting. Reading the FAQ or wiki about indentation might help you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    And same with months. Last slot in arrau declared as months[12], is months[11] (first is months[0])

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help working with an array
    By DCMann2 in forum C Programming
    Replies: 25
    Last Post: 10-28-2008, 11:39 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Dynamic array not working
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2003, 11:03 PM
  5. Working with a large array
    By Eber Kain in forum C++ Programming
    Replies: 18
    Last Post: 09-28-2002, 06:04 PM