# Using a function to calculate the average of an array

Page 1 of 1

## 9 Replies - 3778 Views - Last Post: 06 November 2012 - 07:47 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=298929&amp;s=a52b9d8686987eef01e0a1ddfa695da2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 andyc222

Reputation: 1
• Posts: 4
• Joined: 06-November 12

# Using a function to calculate the average of an array

Posted 06 November 2012 - 03:56 AM

I am trying to use a function to calculate the average value of an array. I am very new to coding. My error says that I am missing a 'function header' but I know that there is probably a more significant problem with my code. It simply won't compile. Any help would be much appreciated.

```#include <iostream>
using namespace std;

double average (int a[], int n);

int main()
{
int a[5] = {7, 12, 14, 21, 1};
cout << "The average is " << average (a, 5);

}

double average (int a[], int n);
{
double sum = 0;
for (int i = 0; i < n; ++i)
{
sum+ = a[i];
average = sum/n;
}
}

```

Is This A Good Question/Topic? 1

## Replies To: Using a function to calculate the average of an array

### #2 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 04:01 AM

See this:
```sum+ = a[i];
```
Does it look like it's correct? Did you mean += and not + =?
Also you don't return anything from your function - returning a result of the calculation would probably be a good idea.

If your code doesn't compile, you should post complete list of warnings/errors.

This post has been edited by Xupicor: 06 November 2012 - 04:02 AM

### #3 Aphex19

• Born again Pastafarian.

Reputation: 618
• Posts: 1,873
• Joined: 02-August 09

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 04:04 AM

There are quite a few errors in your code. Try to compile it (using g++ for example), and look at the line where the errors are indicated. The first major error is on line 13, although the compiler is picking it up as a different error on line 14.

Quote

main.cpp:14:1: error: expected unqualified-id before '{' token

This post has been edited by Aphex19: 06 November 2012 - 04:04 AM

### #4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 04:04 AM

Your average function needs to return a double value. In your code, it doesn't seem to return anything.And the function is having a semicolon on line 13.

And one more thing, you seem to be using a function name as a variable in your line 19. You probably need to change that.

regards,
Raghav

This post has been edited by raghav.naganathan: 06 November 2012 - 04:05 AM

### #5 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 04:06 AM

Ah, right, there's a stray semicolon where function body should start. Good catch.

### #6 marty617

Reputation: 8
• Posts: 33
• Joined: 14-October 12

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 06:14 AM

also average = sum/n; (or whatever that become after you correct the other problems) should be outside of the loop because the first 0 through n-2 times it executes it is just eating cpu with no effect outside the loop. But then don't forget to handle n==0 correctly.

This post has been edited by marty617: 06 November 2012 - 06:15 AM

### #7 BLACK.DART

Reputation: -3
• Posts: 4
• Joined: 06-November 12

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 06:19 AM

try this...

```#include <iostream.h>

average (int a[], int n);

int main()
{
int a[5] = {7, 12, 14, 21, 1};
cout << "The average is " << average (a, 5);

}
average (int a[], int n)
{
double sum = 0;
for (int i = 0; i < n; ++i)
{
sum = sum + a[i];

}
int  average1 = sum/n;
}

```

### #8 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 07:00 AM

@up - your code has some problems.
Line 1 - you want <iostream> in C++, not <iostream.h>.
Line 3 & 11 - I think you forgot to define return type for your function.
Line 13 - you sum up integers, you don't need double as a sum
Line 19 - you do however want float or double for an average; and you want to cast one of operands of division operator, so it won't be integer division.

### #9 BLACK.DART

Reputation: -3
• Posts: 4
• Joined: 06-November 12

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 07:19 AM

yo ! i m runng this in Turbo C++ 4.5...it works fine.
and if i dont mention any return type in a function it means void and acts like a normal called function.
the "query"er should brush up rest of the details !

### #10 jimblumberg

Reputation: 5419
• Posts: 16,896
• Joined: 25-December 09

## Re: Using a function to calculate the average of an array

Posted 06 November 2012 - 07:47 AM

Quote

yo ! i m runng this in Turbo C++ 4.5...it works fine.

Then I suggest you move into this century by finding new compiler. Try using a modern standard compliant compiler and see what happens.

Quote

and if i dont mention any return type in a function it means void and acts like a normal called function.

Actually when you don't provide any return type the compiler will assume you are returning an int, not a void.

I really think you should brush up on the language and come into the modern world. Your compiler seems to match your skills so maybe you should " brush up rest of the details".

Jim