Thread: Calculating the average of an array entered by the user.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    Calculating the average of an array entered by the user.

    Hello. Im having trouble calculating the average of an array. Ill get right to it and post my code.
    Code:
    int main(void)
    {
        float *list,average = 0, sum = 0;
        int sortnum, j, arrayinput;
        printf("Enter the number of data points to average: ");
        scanf("%d",&sortnum);
        list = malloc(sortnum * sizeof(int));
        
        for (j = 0; j < sortnum; j++)
            scanf("%f", list + j);
        
        printf("You entered the following:\n");
        
        for (j = 0; j < sortnum; j++)
            printf("%f\n", *(list + j));
        
        free(list);
        
        sum  = *list + sortnum;
        average = sum/sortnum;
        printf("Average %d", average);
    
    
    
        return 0;
    
    }
    When I run the program everything works fine, except the average is always 0. Any help will be appreciated.
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or C++?

    Anyway, I note that you access *list after free(list), which is wrong. In terms of algorithm, you don't actually sum the elements of the array, so your calculation is off.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I think if you take a long, hard look at this line, you'll see where your error is:
    sum = *list + sortnum;
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2010, 03:07 PM
  2. calculating average
    By coolca in forum C Programming
    Replies: 1
    Last Post: 10-11-2006, 05:55 PM
  3. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM
  4. Calculating an average
    By Andy123 in forum C Programming
    Replies: 11
    Last Post: 03-02-2006, 10:40 AM
  5. calculating average
    By Sharlene in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2003, 03:10 PM