Like Tree1Likes
  • 1 Post By laserlight

Calculating the average of an array entered by the user.

This is a discussion on Calculating the average of an array entered by the user. within the C++ Programming forums, part of the General Programming Boards category; Hello. Im having trouble calculating the average of an array. Ill get right to it and post my code. Code: ...

  1. #1
    blu
    blu is offline
    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
    20,968
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639

  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, 02: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, 09:40 AM
  5. calculating average
    By Sharlene in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2003, 02:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21