View Full Version : C - calculate avg of an array - what am I doing wrong?

12-14-2011, 02:36 AM
I am very new to programming.
I have been looking to many examples for help with a program I need to write, but I am not sure I am getting it. :confused:

What I have to do:

Write a C program to callculate an average of an array of numbers and produce the following output:
1. All the numbers in the array
2. The sum of all the numbers in the array
3. The count of all the numbers in the array
4. The average of all the numbers in the array

Double-space after lines 1, 2, and 3
Define a 1 dimensional integer array of 8 elements and use scanf to read in 8 integers of your choice.
Use a double alternative IF block to calculate avg. of the array and to check for division by 0.
Use for loops to process the array elements and to output numbers

What I have so far:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
/* Declaring */
int count;
int values[8];
int ave, i;
int sum = 0;

/* Prompting for values to fill array 'values' */
printf("Enter 8 values: \n");
for (i = 0; i < 8; i++)
scanf("%d", &values[i]);

/* Outputting values */
printf("Your numbers are: \n");
for(i = 0; i < 8; i++)

/* Calculating average */
for(i = 0; i < 8; i++)
if(count > 0)
ave = sum / count;
ave = 0;
printf("Invalid amount of values")

printf("\nThe average of the ten numbers are: %d\n", ave);

return 0;


12-14-2011, 06:07 AM
Did you have a specific question about the assignment or your code?

12-23-2011, 12:52 AM
Well...I can see a few problems with your program here. Firstly, I am assuming your question is in the title of the topic: What exactly you are doing wrong concerning calculating the average of an array.

Right when take your code into my IDE and try to compile it gave me a warning; specifically the fact that your "count" variable was uninitialized. This can be a concerning problem when calculating your average since you do need it in the denominator of your calculation.

The calculation you have set up is: ave = sum/count which seems to be correct, but if count is uninitialized then you're going to get an incorrect/impossible answer.

My advice to you is to set your "count" variable to "count = 8". Since you are only allowing the user to enter 8 numbers, the calculation is always going to have a denominator of 8. This should get it to work properly. If I am misunderstanding your problem since you did not specify in the actual post of your topic, I apologize.

Edit: Also, I suggest that you declare your "ave" as a double. Calculating the average of something will usually result in a decimal, however you are trying obtain an integer result. This will give you an answer that improperly rounds. You also need to calculate the "sum" variable before you can calculate the average, your original code doesn't do anything with the "sum" variable except for when you declare it at the top as being "= to 0". Hence, why all of your answers for the average are becoming "0". 0 divided by anything will always be 0.