9 Replies - 3592 Views - Last Post: 06 November 2012 - 07:47 AM Rate Topic: -----

#1 andyc222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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? 0
  • +

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

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 marty617  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 BLACK.DART  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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;
}







Was This Post Helpful? -2
  • +
  • -

#8 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 BLACK.DART  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • 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 !
Was This Post Helpful? -2
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1