# using an array to calculate average c++

• (3 Pages)
• 1
• 2
• 3

## 42 Replies - 8720 Views - Last Post: 28 February 2013 - 02:09 AMRate 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=313397&amp;s=db37451a8636808f093b59293879d9e3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

# using an array to calculate average c++

Posted 24 February 2013 - 06:54 PM

I'm trying to do an assignment, but I don't really understand arrays or functions all the way. I've done the average before in c++ where the main function did all the calculations as variable ie sum = numbers and avg = sum/ n but for functions and arrays its a bit more confusing. I mostly get too few argument errors. In this case, the numbers are grades the user is entering, calculating the average once a grade of -1 is entered. I want a function called average to do the calculations, so once the main finishes getting the sum of the grades, it will pass the value of sum to the average fucntion, which will calculate the average and return the variable avg to the main function, where main will output both the avg variable (as defined in main) and the values of the array.

```
#include <iostream> // input and output
#include <stdlib.h> // pause screen after completion
using namespace std;

//average function

double average (double sum, int i, double avg)
{

avg = sum / i;

return average(avg);
}

// main function

int main()

{
double avg, sum = 0;

{

for( i = 0; i <= 10; i++)
{
cout<<"Enter Grade: "<< i + 1 <<endl;

} // end for

} // end while

average(sum,i);

avg = average(avg);

cout << "Average is: " << avg << endl;

system("pause"); //stop the screen

} //end main

```

Is This A Good Question/Topic? 0

## Replies To: using an array to calculate average c++

### #2 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 24 February 2013 - 07:20 PM

I changed the return part to

```
return average(avg,sum,i);

```

but I just want the variable sum to be passed so that main can output it as a result.

### #3 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: using an array to calculate average c++

Posted 24 February 2013 - 07:30 PM

``` double average (double sum, int i, double avg)
{

avg = sum / i;

return average(avg);
}

```

You need to review how functions work.

http://www.cplusplus...rial/functions/

This post has been edited by jjl: 24 February 2013 - 07:30 PM

### #4 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 24 February 2013 - 07:49 PM

when I do :

```
return average(avg,sum,i);

```

it doesn't give me an error. I if I only put avg in the return (), it gives an error of too few arguments. Am I doing something wrong?

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3331
• Posts: 11,783
• Joined: 08-August 08

## Re: using an array to calculate average c++

Posted 24 February 2013 - 08:17 PM

If avg is what you're calculating, then why pass it to the function? And why do you change the order of the parameters??

I would think you'd want to pass the array and the number of items in the array to the function and have the function return the average.

### #6 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: using an array to calculate average c++

Posted 24 February 2013 - 08:24 PM

Quote

it doesn't give me an error. I if I only put avg in the return (), it gives an error of too few arguments. Am I doing something wrong?

It is too hard to explain why that is completely wrong. Read the reference material that I posted earlier.

### #7 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: using an array to calculate average c++

Posted 24 February 2013 - 09:03 PM

```//average function

//changes below corrects the average () function
double average (double sum, int i)

{double avg=0;

avg = sum / i;

return avg;//delete the word average and ellipses

}
```

This post has been edited by buffalobill: 24 February 2013 - 09:05 PM

### #8 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 24 February 2013 - 09:49 PM

Made some changes and now get an 47 invalid conversion from `int*' to `int' initializing argument 1 of `int average(int)'

```

#include <iostream> // input and output
#include <stdlib.h> // pause screen after completion
using namespace std;

//average function

{

int i =0;
int avg = 0;
int sum = 0;

for( i = 0; i <= 9; i++)
{
} //end for

avg = sum / i;

return (avg);

}

// main function

int main()

{

{

cout<<"Enter Grade: "<< i + 1 <<endl;
} //end while

cout << "Average is: " << GradeAvg << endl;

system("pause"); //stop the screen

} //end main

```

### #9 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: using an array to calculate average c++

Posted 24 February 2013 - 09:58 PM

You need to pass grades as an array of integers, not a single integer.

```int average(int grades[]);

```

### #10 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 24 February 2013 - 10:37 PM

jjl, on 24 February 2013 - 09:58 PM, said:

You need to pass grades as an array of integers, not a single integer.

```int average(int grades[]);

```

makes sense. How do I pass an array of integers, instead of a single one? Would it be: "int average(int grades[i]"? Does the i mean all value if I do that in the loop? I've been following the array example

```
// arrays example
#include <iostream>
using namespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0

```

from http://www.cplusplus...utorial/arrays/

Where they have n, I call it i in mine.

In my for loop, should it be something like " sum = sum + grades[i]; ?

### #11 jjl

• Engineer

Reputation: 1121
• Posts: 4,648
• Joined: 09-June 09

## Re: using an array to calculate average c++

Posted 24 February 2013 - 11:17 PM

Quote

How do I pass an array of integers

By declaring the function to accept an array of integers
i.e.
```//accepts array of integers, and number of integers in array
{
int sum = 0;
for(int i=0; i<size; i++) {
//calculate sum
}
return //calculate average
}

int main() {

```

Quote

In my for loop, should it be something like " sum = sum + grades[i];

Exactly

### #12 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 25 February 2013 - 02:48 AM

Now my problem is that the size of the array cannot be greater than 10 total, and the user can input less than 10 grades in. It also fails to store the value of the entered grades in the appropriate array indexes, it just overwrites the first grade.

```
//average function

int average(int grades[], int size =9)
{

int avg = 0;
int sum = 0;

for( int i = 0; i = size; i++)
{
} //end for

avg = sum / size;

return (avg);

```

```
int main()

{

{

cout<<"Enter Grade: "<< i + 1 <<endl;
} //end while

```

### #13 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 25 February 2013 - 03:19 AM

forgot to add i++; in my 2nd code block in the loop

```
int main()

{
double avg;

{

cout<<"Enter Grade "<< i+1 <<": " ;
i++;

} //end while

```

The problem is not only is my sentinel value of -1 ignored, but it asks for grades up to grade #19

### #14 jimblumberg

Reputation: 4653
• Posts: 14,586
• Joined: 25-December 09

## Re: using an array to calculate average c++

Posted 25 February 2013 - 06:33 AM

Quote

The problem is not only is my sentinel value of -1 ignored

Your sentinel value is not ignored, it is not seen. You increment your index value before you test the value so you are testing an element that hasn't been initialized. You need to test the value before you increment the index.

Quote

Actually there is really nothing the program from asking for grades much higher than that. Where do you test to insure your index doesn't become larger than your array?

Jim

### #15 Jite25

Reputation: -2
• Posts: 82
• Joined: 24-February 13

## Re: using an array to calculate average c++

Posted 27 February 2013 - 04:51 AM

Changes:

```
#include <iostream> // input and output
#include <stdlib.h> // pause screen after completion
using namespace std;

//average function

{

int avg = 0;
int sum = 0;

for( int i = 0; i < size; i++)
{

} //end for

avg = sum / size;

return avg;

}

// main function

int main()

{

while(grades[i] != -1 && i < 10)
{

cout<<"Enter Grade "<< i+1 <<": " ;
i++;

} //end while

cout << "Average is: " << Gradeavg << endl;

system("pause"); //stop the screen

} //end main

```

Always gives me average = 0. Still does not see sentinel value of -1.

When I say [code/ while(grades[i] != -1 [/code]

I want it to mean
``` cout<<"Enter Grade "<< i+1 <<": " ;
```

Because to me, "grades[i]" is the stored grade the user entered. So if this were the 5th iteration of the loop (grade #5) grades[i] = grades[4] (number might be off) but I seem to have this concept wrong somehow.

• (3 Pages)
• 1
• 2
• 3

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }