Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C - calculate avg of an array - what am I doing wrong?

    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.

    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:

    Code:
    #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++)
    printf("%4d",values[i]);
     
    /* Calculating average */
    for(i = 0; i < 8; i++)
    if(count > 0)
    ave = sum / count;
    else
    {
        ave = 0;
        printf("Invalid amount of values")
    }
    
    printf("\nThe average of the ten numbers are: %d\n", ave);
     
    system("pause");
    return 0;
    
    }

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Did you have a specific question about the assignment or your code?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Integral; 12-23-2011 at 01:01 AM. Reason: Forgot to mention...


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •