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

Page 1 of 1

## 10 Replies - 793 Views - Last Post: 07 May 2016 - 02:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=392998&amp;s=cd8a11505b5412fe5c4a7886fdbec218&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JosephKerr

• New D.I.C Head

Reputation: 0
• 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;
if(gL[grade1] < lowest) {
}
}
return lowest;
}

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

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

}

}

int main(void){
//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.
//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.
printf("    Continue?  y/n: ");
scanf(" %c", &continueResponse);
//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.
//Loops the program to print out the grades entered.
}

printf("Most shit grade was: %d\n", low);
printf("Most fucking excellent grade was: %d\n", high);

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

return 0;
}
```

Is This A Good Question/Topic? 0

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

### #2 ndc85430

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

Reputation: 797
• Posts: 3,256
• 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?

### #3 kaa

Reputation: 225
• 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.

### #4 JosephKerr

• New D.I.C Head

Reputation: 0
• 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

ndc85430, 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.

kaa, 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++){
gradeAvg = (float) sum / gcnt3;

}

```

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.

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• 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.

### #6 ndc85430

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

Reputation: 797
• Posts: 3,256
• 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.

### #7 JosephKerr

• New D.I.C Head

Reputation: 0
• 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

CTphpnwb, 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

ndc85430, 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.

### #8 kaa

Reputation: 225
• 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.

### #9 kaa

Reputation: 225
• 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...

### #10 JosephKerr

• New D.I.C Head

Reputation: 0
• 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.

### #11 kaa

Reputation: 225
• 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?