# How to find average in an array of numbers using function.

Page 1 of 1

## 10 Replies - 12367 Views - Last Post: 01 September 2011 - 03:01 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=245503&amp;s=83f0cb932c35cab2787664c484d48664&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rdhc1330

Reputation: 1
• Posts: 130
• Joined: 01-August 11

# How to find average in an array of numbers using function.

Posted 30 August 2011 - 11:49 PM

Hey guys so I understand my question OK, I've been through the internet and done as much research as I can...
as far as I can see, everyone has been given hard-coded numbers to input where I have to read my integers in using a for loops and then sending them to the function to calculate the average. I'll show you what I've done so far and if you could lend me a hand with anything that would be awesome...

so function prototype should look something like this:
```void printCalc( int[] );

```

then to read the 10 integers into an array I have done this:

```int userInput;

for( int i = 1; i <= 10; i++ )
{
cout << "Please enter number " << i << " of 10: ";
cin >> userInput;
}

```

and then function call:
```printCalc( userInput[] );

```

and lastly the function itself:
```void printCalc( int arrayInput )
{
int sum = 0;
int average = 0;
int arrayInput;

for (int i = 0; i < 10; ++i)
{
sum+=arrayInput[i];
average = sum/10;
cout<<"Average:"<<average;
}
}

```

Any help much appreciated

Is This A Good Question/Topic? 0

## Replies To: How to find average in an array of numbers using function.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How to find average in an array of numbers using function.

Posted 30 August 2011 - 11:59 PM

Did you put the numbers you entered, into the array?

That's not how to average numbers. Sum them ALL up, and do NOT divide each by 10. Then, after the for loop is done, divide the sum by i or 10.

Your i variable should go from i=0; to i<10;

Learn to count from 0 not 1! All C style arrays begin with 0, and stop with one less than the number of elements in the array. NO <= ! in the testing part of the for loop:

for(i = 0; i < 10; i++), <====Good!
for(i = 1; i <= 10; i++), <===Stinks! Error.

This post has been edited by Adak: 31 August 2011 - 12:20 AM

### #3 rdhc1330

Reputation: 1
• Posts: 130
• Joined: 01-August 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 12:03 AM

Adak, on 30 August 2011 - 11:59 PM, said:

Did you put the numbers you entered, into the array?

how do you mean?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 12:06 AM

I mean something like:

```for(i=0;i<10;i++)
cin >> &array[i];

```

THEN call your average function or have the average block of code.

You CAN average numbers that way, but that's not the way you want to do things in code (in general).

In sequence, what you want is:
Get your input, and store it
make your computations on the input

Try to never do a bit of one, then a bit of the other, then a bit of the third, etc. Easy to get bugs that are to find, that way. Keep it simple, and go 1, 2, 3. Simple, efficient, relatively concise - that's good C, and that's straight from the creator of C, I assure you.

This post has been edited by Adak: 31 August 2011 - 12:24 AM

### #5 Ecstatic

Reputation: 2
• Posts: 42
• Joined: 20-March 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 02:30 AM

1. Input the digits in the array.
2. Call the function.
Then in the function:
3. Use a temporary variable to store the sum and sum all of the elements in the array like you usually would.
4. Divide the temporary variable with the number of the digits entered.
5. Return the average.

Hope it helps

This post has been edited by Ecstatic: 31 August 2011 - 02:30 AM

### #6 rdhc1330

Reputation: 1
• Posts: 130
• Joined: 01-August 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 02:14 PM

Adak, on 30 August 2011 - 11:59 PM, said:

Did you put the numbers you entered, into the array?

That's not how to average numbers. Sum them ALL up, and do NOT divide each by 10. Then, after the for loop is done, divide the sum by i or 10.

Your i variable should go from i=0; to i<10;

Learn to count from 0 not 1! All C style arrays begin with 0, and stop with one less than the number of elements in the array. NO <= ! in the testing part of the for loop:

for(i = 0; i < 10; i++), <====Good!
for(i = 1; i <= 10; i++), <===Stinks! Error.

Hey yeah thanks, I understand that but I need the counter to start at 1, when running the program it says "Please enter number << i << of 10: "

'i' starting at 1 and finishing at 10, where as 0 to 9 would look funny

### #7 jimblumberg

Reputation: 4734
• Posts: 14,757
• Joined: 25-December 09

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 02:27 PM

Quote

Hey yeah thanks, I understand that but I need the counter to start at 1, when running the program it says "Please enter number << i << of 10: "

Then you should print i + 1 not change you loop from 1 to 10. Arrays in C/C++ start at 0 and go to size - 1. Trying to force the array to start at 1 is a very bad habit and prone to errors.

Jim

### #8 rdhc1330

Reputation: 1
• Posts: 130
• Joined: 01-August 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 02:39 PM

jimblumberg, on 31 August 2011 - 02:27 PM, said:

Quote

Hey yeah thanks, I understand that but I need the counter to start at 1, when running the program it says "Please enter number << i << of 10: "

Then you should print i + 1 not change you loop from 1 to 10. Arrays in C/C++ start at 0 and go to size - 1. Trying to force the array to start at 1 is a very bad habit and prone to errors.

Jim

cheers Jim, that's sorted now

### #9 rdhc1330

Reputation: 1
• Posts: 130
• Joined: 01-August 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 10:09 PM

so far I'm up to here:

```#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int printAverage( int[], int );
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
const int MAX = 10;
int userInput[ MAX ];
int sum = 0;

for( int i = 0; i < MAX; i++ )
{
cout << "Please enter number " << i + 1 << " of 10: ";
cin >> userInput[ i ];
sum = sum + userInput[ i ];
}

int average = printAverage( userInput, MAX);

system("cls");
printAverage( sum, MAX );

getch();
return 0;
}
//------------------------------------------------------------------------------
int printAverage( int array[], int numElements )
{
int sum = 0;

for( int i = 0; i < numElements; i++)
{
sum += array[ i ];
}

return sum / numElements;
}
//------------------------------------------------------------------------------

```

now I'm not using pointers, only passing the array name and size of the array. I keep getting error:

25 initializing argument 1 of `int printAverage(int*, int)'

any help?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: How to find average in an array of numbers using function.

Posted 31 August 2011 - 11:06 PM

Delete line 25.

First, do the for loop, to get the sum added up.

Second, call printAverage, to get the average calculated and returned

Then clear the screen.

And print the output you want.

### #11 darek9576

• D.I.C Lover

Reputation: 202
• Posts: 1,710
• Joined: 13-March 10

## Re: How to find average in an array of numbers using function.

Posted 01 September 2011 - 03:01 AM

I think you should be using :

cin.get();