Thread: Help finding average?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Help finding average?

    Ok, I realize I just asked for help, but I am so confused right now. I have been working for the past hour on another problem (which is just an add on to the last one I posted about), and I know I am SOMEWHERE on the right track, but I just need a tad bit of guidance? My professor didn't give me much to go off of to find the average using arrays/loops, except the following model function:

    Code:
    double find_average(int * array, int size)
    {
       int sum = 0;
       int i;
       for(i = 0; i < size; i++) 
      { sum += array[i]; } /* running total */
      return sum/ sizeof(array)/sizeof(*array);
    }
    I need to find the average of all of the scores. This is what I have, and after is the errors I am getting.

    Code:
    # include <stdio.h>
    
    /*prototypes*/
    double find_average(double *score[], int size);
    
    int main()
    {
            double score[5];
            int i;
            double average;
            int size;
    
            size=5;
            score[0]= 81.4;
            score[1]= 75.3;
            score[2]= 90.0;
            score[3]= 96.9;
            score[4]= 85.6;
    
            printf( "The grades are:" );
    
            for (i=0; i < sizeof(score)/sizeof(*score); i++)
            {
                    printf(" %.1lf", score[i]);
            }
            printf("\n");
    
            printf("The average is: ");
            average = find_average(*score, size);
            printf("/n");
    
            return 0;
    }
    
    
    /*methods*/
    double find_average(double *score[], int size)
    {
       int sum = 0;
     int i;
       for(i = 0; i < size; i++)
      { sum += score[i]; }
      return sum / (sizeof(score)/sizeof(*score));
    }
    Errors:
    hw10test2.c: In function āmainā:
    hw10test2.c:29: error: incompatible type for argument 1 of āfind_averageā
    hw10test2.c:4: note: expected ādouble **ā but argument is of type ādoubleā
    hw10test2.c: In function āfind_averageā:
    hw10test2.c:42: warning: assignment makes integer from pointer without a cast

    Once again, any help would be greatly appreciated! I hate asking for help, but I just can't figure this out for some reason. I have almost nothing to go off of (no instruction related to this specifically, just some vague instructions on passing techniques).

    Thank you!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    double find_average(int * array, int size)
    {
       int sum = 0;
       int i;
       for(i = 0; i < size; i++) 
      { sum += array[i]; } /* running total */
      return sum/ sizeof(array)/sizeof(*array);
    }
    You are passing in the size, what is up with this?

    Code:
    return sum/ sizeof(array)/sizeof(*array);
    To pass a pointer to a type you use the address-of operator, &, not the dereference operator, *.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    I am honestly not sure. I have only that model to go off of. In class, he said, "Oh, and here are a few models you can refer to later", and that was it. And, according to what was given, I thought that was what I was supposed to do? Is there any resource that could explain this a little more clearly? I honestly don't know what I am doing. The code you quoted is the model I was given. That is his work (the model), not mine. In my code, I was just trying to work with what I was given.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: The reason you pass the size to the function is that sizeof() will NOT give you the correct size of the array.

    Array names decay into pointers when passed to functions; pointers have a constant size not based on the number of items in the array.

    Tim S.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Referring to your first code sample in message 1...

    Line 7 should be...
    Code:
    return sum / size;

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thank you guys!!! You put me on the right track. I got it!

    Code:
     
    # include <stdio.h>
    
    /*prototypes*/
    double find_average(double *array, double size);
    
    int main()
    {
            double score[5];
            int i;
            double size;
            double average;
    
            size=5;
            score[0]= 81.4;
            score[1]= 75.3;
            score[2]= 90.0;
            score[3]= 96.9;
            score[4]= 85.6;
    
            printf( "The grades are:" );
    
            for (i=0; i < sizeof(score)/sizeof(*score); i++)
            {
                    printf(" %.1lf", score[i]);
            }
            printf("\n");
    
            printf("The average is: ");
            average = find_average(score, size);
            printf("%.2lf", average);
            printf("\n");
    
            return 0;
    }
    
    
    /*methods*/
    double find_average(double *array, double size)
    {
       double sum = 0;
       int i;
       for(i = 0; i < size; i++)
      { sum += array[i]; }
      return sum / size;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding the highest average grade
    By Rockie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 11:55 AM
  2. finding highest average
    By Vontrapp in forum C Programming
    Replies: 6
    Last Post: 06-07-2010, 06:38 AM
  3. finding the max/min/average
    By niponki in forum C Programming
    Replies: 2
    Last Post: 08-04-2005, 10:44 AM
  4. Finding Average of floats
    By sonict in forum C++ Programming
    Replies: 8
    Last Post: 12-01-2002, 08:52 PM
  5. Finding an Average
    By kidguru in forum C Programming
    Replies: 4
    Last Post: 02-24-2002, 06:25 PM