As part of an assignment, I am to create a function to complete the program....I am very new to arrays and would like a little help, code as follows

// NAME:
// ASSIGNMENT:  pj70101 - ComputeAverage Function
// COURSE:

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

//=========================================================
// put your function prototype here - MUST be one function.
//=========================================================
const int MAX_ARRAY_SIZE = 15;

double computeAverage(double x[],double y[], double z[], const int SIZE);



int main()
{//DO NOT CHANGE ANYTHING IN THIS FUNCTION.
    double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
    double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
    double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

    cout << fixed << showpoint << setprecision(2);

    double avg1 = computeAverage (x, sizeof (x) / sizeof (x[0]) );
    double avg2 = computeAverage (y, sizeof (y) / sizeof (y[0]));

    cout << "Average of numbers in array x = " <<setw(7) << avg1 << '\n';
    cout << "Average of numbers in array y = " <<setw(7) << avg2 << '\n';
    cout << "Average of numbers in array z = " <<setw(7)
         << computeAverage (z, sizeof (z) / sizeof (z[0])) << '\n';
    return 0;
 }

//====================================================================
// put your function definition here - MUST be one function definition
//====================================================================
double computeAverage(int Array[], const int SIZE) 
{
	int  sum =0;
	double avg;

	sum = ComputeAverage(Array, SIZE);
	if( SIZE > 0 )
		avg = sum / SIZE;
	else
		avg = 0;
	return avg;
}

I currently get the error c2660 the function does not take 2 arguments for line 24....any help?

Your parameters of your prototype don't match the parameters of your function definition. You don't need the x,y,z that's going to be passed in,the function doesn't care about that, you need something like line 39.

However, unlike 39 since it's a prototype you can omit the Array word, but leave in the int []). The prototype prepares the compiler for what's to come later so you need to tell it my function computeAverage has parameters of double and const int in that order. So you've confused it by giving it a prototype with 4 parameters rather than 2.

Edited 6 Years Ago by jonsca: n/a

Thanks, I corrected that...now when I run the program I get a stack overflow.....any ideas?

// NAME:
// ASSIGNMENT:  pj70101 - ComputeAverage Function
// COURSE:

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

//=========================================================
// put your function prototype here - MUST be one function.
//=========================================================
const int MAX_ARRAY_SIZE = 15;

double computeAverage(double[], const int SIZE);



int main()
{//DO NOT CHANGE ANYTHING IN THIS FUNCTION.
    double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
    double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
    double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

    cout << fixed << showpoint << setprecision(2);

    double avg1 = computeAverage (x, sizeof (x) / sizeof (x[0]) );
    double avg2 = computeAverage (y, sizeof (y) / sizeof (y[0]));

    cout << "Average of numbers in array x = " <<setw(7) << avg1 << '\n';
    cout << "Average of numbers in array y = " <<setw(7) << avg2 << '\n';
    cout << "Average of numbers in array z = " <<setw(7)
         << computeAverage (z, sizeof (z) / sizeof (z[0])) << '\n';
    return 0;
 }

//====================================================================
// put your function definition here - MUST be one function definition
//====================================================================
double computeAverage(double Array[], const int SIZE) 
{
	double  sum = 0;
	double avg;

	sum = computeAverage(Array, SIZE);
	if( SIZE > 0 )
		avg = sum / SIZE;
	else
		avg = 0;
	return avg;
}

You're calling computeAverage recursively from itself on line 44. It's going to keep going and going because there's not base case (stopping condition) for the recursion. I don't think you meant to do that anyway. Maybe you meant it to be a computeSum helper function because what you have there doesn't make sense.

Thanks you have been very helpfull so far.....I have added this to my function definition

double computeAverage(double Array[], const double SIZE) 
{
	double  sum = 0;
	double avg;

	sum = computeSum(Array, SIZE);
	if( SIZE > 0 )
		avg = sum / SIZE;
	else
		avg = 0;
	return avg;
}
double computeSum(int Array[], const double SIZE) 
{
	double sum =0;

	for( double index = 0; index < SIZE; index++ )
		sum += Array[ index ];
	return sum;
}

however now get error C2108: subscript is not of integral type for the line "su, += Array[index};

This article has been dead for over six months. Start a new discussion instead.