10 Replies - 6956 Views - Last Post: 01 September 2011 - 03:01 AM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

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

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

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

Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

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

View PostAdak, on 30 August 2011 - 11:59 PM, said:

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


how do you mean?
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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
output your results, as needed.

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

Was This Post Helpful? 0
  • +
  • -

#5 Ecstatic  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 1
  • +
  • -

#6 rdhc1330  Icon User is offline

  • D.I.C Head

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

View PostAdak, 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 :)
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 rdhc1330  Icon User is offline

  • D.I.C Head

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

View Postjimblumberg, 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
Was This Post Helpful? 0
  • +
  • -

#9 rdhc1330  Icon User is offline

  • D.I.C Head

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

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

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

#11 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • 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();

instead of:

getch();

since its C++ but i might be wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1