10 Replies - 735 Views - Last Post: 07 May 2016 - 02:13 PM Rate Topic: -----

#1 JosephKerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-May 16

How to calculate the average of an array from a function?

Posted 07 May 2016 - 08:49 AM

Hello and thanks for taking the time to look and maybe answer my questions. I am currently learning the C language and I need help understanding a few things. First off, I want to figure out how to calculate the average of the grades entered by the user. I thought I could come to an understanding by using my notes and some online searching and came upon what you see below. I knew going ahead that the function wouldn't be correct however, I do not know where I went wrong and I thought maybe I could figure it out by looking at it.
So far I have tried changing the variable for the average to a float. I tried changing the average equation at the end to use type cast and divide grades(which should be total) by the count. When I did that I get the error “error: pointer value used where a floating point value was expected” and this is on line 80. I also have tried searching to learn about how to calculate an average in C and couldn't come across anything that useful. I appreciate any advice.
-J


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

 #define MAX_ARRAY_SIZE 30

int getLowest(int gL[], int gcnt){
    int lowest = 100, grade1;
    for(grade1 = 0; grade1 < gcnt; grade1++){
      if(gL[grade1] < lowest) {
        lowest = gL[grade1];
      }
   }
   return lowest;
}

int getHighest(int gH[], int gcnt2){
    int highest = 0, grade2;
    for(grade2 = 0; grade2 < gcnt2; grade2++) {
      if(gH[grade2] > highest) {
        highest = gH[grade2];
      }
    }
    return highest;
}

int avg(int gradeSum[], int gcnt3){
    int sum = 0, gradeAvg;
    for(gradeAvg = 0; sum < gcnt3; gradeAvg++){
        sum += gradeSum[gradeAvg];

    }

    return gradeAvg;
}

 int main(void){
     //Defines grades[MAX_ARRAY_SIZE], gradeCount, and grade as integer values.
     int grades[MAX_ARRAY_SIZE], gradeCount = 0, grade, low = getLowest(grades, gradeCount), high = getHighest(grades, gradeCount);
     int average = avg(grades, gradeCount);
     int *grAde;
     //Defines continueResponse as a character value.
     char continueResponse;
     // Allocates heap memory for our data.
     grAde = malloc(sizeof(int) * MAX_ARRAY_SIZE);
     if(grAde == 0) {
        exit(1);
     }
     //Prompts the user what the program is for and what to enter.
     printf("This program will ask you to input the grades of your class. \n");
     printf("\nEnter a max of %d grades.\n", MAX_ARRAY_SIZE);
     //The for loop makes the program loop until all grades have been entered and the user chooses to end the loop.
     for (grade = 0; grade < MAX_ARRAY_SIZE; grade++) {
        //Prompts the user to enter a grade 0-100.
        printf("Enter grade (0-100) [%02d/%02d]: ", grade + 1, MAX_ARRAY_SIZE);
        //User enters the grade.
        scanf("%d", &grades[grade]);
        printf("    Continue?  y/n: ");
        scanf(" %c", &continueResponse);
        gradeCount++;
        //If the user is finished and enters an n or an N then the program ends the loop.
        if(continueResponse == 'n' || continueResponse == 'N') {
            printf("\n //// END OF DATA ENTRY //// \n");
            {break;}
        }
     }

     //Shows the user how many grades were entered.
     printf("\n\nGrades entered: \n");
     //Loops the program to print out the grades entered.
     for(grade = 0; grade < gradeCount; grade++){
        printf("\t%d\n", grades[grade]);
     }

     low = getLowest(grades, gradeCount);
     printf("Most shit grade was: %d\n", low);
     high = getHighest(grades, gradeCount);
     printf("Most fucking excellent grade was: %d\n", high);

     printf("The average grade was: %d\n", average);

     free(grAde);


     return 0;
 } 


Is This A Good Question/Topic? 0
  • +

Replies To: How to calculate the average of an array from a function?

#2 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 721
  • View blog
  • Posts: 2,911
  • Joined: 13-June 14

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 08:57 AM

Remember that the average is the sum of the values divided by the number of values. Is that what your function is returning?
Was This Post Helpful? 0
  • +
  • -

#3 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 09:00 AM

In your avg function, where did you actually calculate an average?

And yes, you should probably calculate it as a float or a double, and change the function return type accordingly.

And in main, you shouldn't be calling the avg function before any input occurs.
Was This Post Helpful? 0
  • +
  • -

#4 JosephKerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-May 16

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 09:45 AM

View Postndc85430, on 07 May 2016 - 08:57 AM, said:

Remember that the average is the sum of the values divided by the number of values. Is that what your function is returning?

That is what I am trying to achieve. That was what I was able to put together after searching for how to calculate an average with an array.

View Postkaa, on 07 May 2016 - 09:00 AM, said:

In your avg function, where did you actually calculate an average?

And yes, you should probably calculate it as a float or a double, and change the function return type accordingly.

And in main, you shouldn't be calling the avg function before any input occurs.

I believed that to be sum += gradeSum[gradeAvg]; I see now that I was missing the proper calculation for average or perhaps needing more.
I am a bit stuck on what the proper return type would be however.

So far I added to the avg function
float avg(int gradeSum[], int gcnt3){
    int sum = 0, gradeAvg;
    for(gradeAvg = 0; sum < gcnt3; gradeAvg++){
        sum += gradeSum[gradeAvg];
        gradeAvg = (float) sum / gcnt3;

    }

    return gradeAvg;

and this below to where the program prints out the lowest and highest then average grades.
float average = avg(grades, gradeCount);


however for average the grade always comes up as 0.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,583
  • Joined: 08-August 08

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 09:58 AM

  • Why would you calculate gradeAvg more than once?
  • Integer math has no fractions, so 9999/10000 == 0.

Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 721
  • View blog
  • Posts: 2,911
  • Joined: 13-June 14

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 10:08 AM

Also, the variables you're using in your for loop header don't really make sense. Why would you use gradeAvg to index the array and use sum < gcnt3 as the condition to use to decide when to stop iterating? I assume gcnt3 is the number of elements in the array, but you should use a better name than that.
Was This Post Helpful? 0
  • +
  • -

#7 JosephKerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-May 16

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 10:25 AM

View PostCTphpnwb, on 07 May 2016 - 09:58 AM, said:

  • Why would you calculate gradeAvg more than once?
  • Integer math has no fractions, so 9999/10000 == 0.

Why? I would think it would be to get the average. But I don't know now. I am still trying to figure out

View Postndc85430, on 07 May 2016 - 10:08 AM, said:

Also, the variables you're using in your for loop header don't really make sense. Why would you use gradeAvg to index the array and use sum < gcnt3 as the condition to use to decide when to stop iterating? I assume gcnt3 is the number of elements in the array, but you should use a better name than that.

I used gradeAvg because I thought it was correct. As for the gcnt3 is the counter. I used gradeCount in main so I wanted to keep what made sense to me in the names.
Was This Post Helpful? 0
  • +
  • -

#8 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 10:51 AM

No, gcnt3 is the limit. It's the number of elements in the array, so in the loop it's used as the constant that stops the loop iteration.

(Actually, there's no need for separate gcnt, gcnt2 and gcnt3. Each of these is actually a local variable that exists only in the function where it's declared. You can use the same name, e.g., gcnt, in all 3 functions and the meaning will actually be clearer.)

If gradeAvg will be the average (which makes sense), you need a separate variable to serve as the loop index, or "counter". It is common to use simply i or idx as the variable name for this.
Was This Post Helpful? 0
  • +
  • -

#9 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 10:57 AM

Although you don't actually need a "gradeAvg" variable. You can simply return the result of the average calculation, e.g.:
float myfunc( //argument list...)
{
    // some code ...

   return ...; // code to calculate the return value ;
}

Hope that's clear...
Was This Post Helpful? 0
  • +
  • -

#10 JosephKerr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-May 16

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 01:20 PM

I appreciate all the help everyone. I just find this all a tad confusing since I obviously lack the proper understanding of math and how it should be written in code. I thought I was doing ok until I hit my first stump trying to calculate overtime and now this. My professor will show us what to do in about 10 minutes in a conference call, I just was hoping to be able to wrap my head around the concept and not just copy and paste. Seems I am more lost than I thought.
Was This Post Helpful? 0
  • +
  • -

#11 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: How to calculate the average of an array from a function?

Posted 07 May 2016 - 02:13 PM

It seemed like you were going in the right direction but then got confused.

Can you describe in words how to calculate an average?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1