Need help finding mean with function.

I'm getting an error when I compile this.
[Error] expected primary-expression before ']' token.
Its Line 17

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using namespace std;
//prototypes
float Mean (int array,int size);

const size_t max_size = 100;

main()
{
	int array[max_size];
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while ((size < max_size) && (cin >> array[size]))
	{
		size++;
	}
	
	cout << "average: " << Mean(array[], size);
return 0;
}

Mean(int numbers[],int sizeOfArray)
{
	float sum = 0;
	float average = 0;
	for(int i = 0; i < sizeOfArray; i ++)
	{
		sum += numbers[i];
	}
	average = sum/sizeOfArray;
	return average;
}
Last edited on
Please check:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int Mean(int numbers[],int sizeOfArray)
{
	float sum = 0;
	float average = 0;
	for(int i = 0; i < sizeOfArray; i ++)
	{
		sum += numbers[i];
	}
	average = sum/sizeOfArray;
	return average;
}

int main()
{

    int array[10]; // can not define size of array by variable, It must be a const value or enum may work.
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while ((size < 10) && (cin >> array[size]))
	{
		size++;
	}
	
	cout << "average: " << Mean(array, size); // just array name will work no need to say array[]
    
    getch();
    return 1;
}
// can not define size of array by variable, It must be a const value or enum may work.


But i thought you could if it was a constant variable?
Now i'm getting the error:Line 16 [Error] invalid conversion from 'int*' to 'int' [-fpermissive]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
//prototyypes
float Mean (int array,int size);

main()
{
	int array[100];
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while ((size < 100) && (cin >> array[size]))
	{
		size++;
	}
	
	cout << "average: " << Mean(array, size);
return 0;
}

Mean(int numbers[],int sizeOfArray)
{
	float sum = 0;
	float average = 0;
	for(int i = 0; i < sizeOfArray; i ++)
	{
		sum +=(numbers[i]);
	}
	average = sum/sizeOfArray;
	return average;
}
Anybody?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
//prototyypes
float Mean(int arr[], int size); //make sure you declare prototypes correctly

int main() //all functions must have a return type
{
        const size_t max_size = 100;
	int arr[max_size]; //you CAN define an array with a const variable
        //you can also define an array with a non-const variable but then you have to dynamically allocate memory
	cout << "Enter integers('x' to stop)\n";
	size_t size = 0;
	while ((size < 100) && (cin >> arr[size]))
	{
		size++;
	}

	cout << "average: " << Mean(arr, size);
        
        cin.ignore(); //to pause the program
        cin.get();
	return 0;
}

float Mean(int numbers[], int sizeOfArray) //mean must return float
{
	float sum = 0;
	for (int i = 0; i < sizeOfArray; i++)
	{
		sum += (numbers[i]);
	}
	return (float)sum / sizeOfArray; //must cast sum to float, otherwise an integer division would occur
}
Last edited on
Topic archived. No new replies allowed.