Thread: adding contents of an array

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    adding contents of an array

    I have this program i'm working on and i'm suppose to input some grade averages and then calculate the avg what I'm trying to do first is just add all the GPAs but it won't work can someone tell me why heres the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void CalculateAvg(void);
    
    int i;
    float iGpa[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    
    int main()
    {
    	int iChoice = 0;
    
    	while (iChoice != 3)
    	{
    		printf("\n\tGPA Calculator\n");
    		printf("1\tCalculate Average\n");
    		printf("2\tEnter new GPA\n");
    		printf("3\tQuit\n");
    
    		printf("\n\n\tEnter a choice: ");
    		scanf("%d",&iChoice);
    
    		if(iChoice == 1)
    		{
    			system("cls");
    			CalculateAvg();
    		}
    
    
    
    		if(iChoice == 2)
    		{
    			printf("\n\tGPA Calculator\n");
    			printf("\tEnter a GPA: ");
    			scanf("%f",&iGpa[i]);
    		}
    
    		
    	}
    }
    void CalculateAvg()
    {
    	float x;
    	int i;
    	for (i = 0; i <= 30; i++)
    		x += iGpa[i];
    	printf("The average is %f\n",x);
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Probably because you didn't initialize x, so it contains garbage values.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Initialize x doesn't do the trick it comes out the same what it does is print the last record I entered

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to initialize x, for sure, to 0.0

    Then you need to divide x after you've added up the grades into it, by the number of grades in iGpa. After all, it's still calculating an average.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    but it won't add up the grades the code is wrong or something it prints out the last GPA record I put in there not them all added up that's what I don't get

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    if I can't add them then I can't divide them

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    That's because you keep overwriting the previous values. The variable i (the global one) never changes from 0.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And here's the answer to that:

    scanf("%f",&iGpa[i]);

    You want i++ in there, not just i, and you need to initialize it to 0, also.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    hey thanks guys I ran the code it works now here it is
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void CalculateAvg(void);
    
    int iNum = 0;
    float iGpa[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    int main()
    {
    	int i = 0;
    	int iChoice = 0;
    
    	printf("\n\tGPA Calculator\n");
    	printf("\tHow many records to enter: ");
    	scanf("%d",&iNum);
    
    
    	while (iChoice != 3)
    	{
    		printf("\n\tGPA Calculator\n");
    		printf("1\tCalculate Average\n");
    		printf("2\tEnter new GPA\n");
    		printf("3\tQuit\n");
    
    		printf("\n\n\tEnter a choice: ");
    		scanf("%d",&iChoice);
    
    		if(iChoice == 1)
    		{
    			system("cls");
    			CalculateAvg();
    		}
    
    
    
    		if(iChoice == 2)
    		{
    			printf("\n\tGPA Calculator\n");
    			printf("\tEnter a GPA: ");
    			scanf("%f",&iGpa[i++]);
    		}
    
    		
    	}
    }
    void CalculateAvg()
    {
    	float x = 0;
    	int i;
    	for (i = 0; i <= iNum; i++)
    		x += iGpa[i];
    	printf("The average is %f\n",x/iNum);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding up the rows of a 2D array
    By youngvito in forum C Programming
    Replies: 31
    Last Post: 06-11-2009, 01:06 PM
  2. printing contents of array vertically
    By richdb in forum C Programming
    Replies: 16
    Last Post: 03-10-2006, 01:12 AM
  3. Getting the contents of edit boxes into an array
    By earth_angel in forum Windows Programming
    Replies: 3
    Last Post: 07-05-2005, 12:17 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM