Function passed an array

Hi everybody, I am attempting to write a program " that has a function that is passed an array of int and its size, and with that it prints out only the numbers that are above average. "

I have included my code so far, but I am only getting one value as output, and that value seems to be completely off. I keep trying, but I am really stuck. Any help would be awesome. Thank you.

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
#include <iostream>
using namespace std;

int average(int values[],int size);

int main(){
	int size;
	int values[] = {1,2,3,4,5,6};
	cout << "Please input the size of the array" << endl;
	cin >> size;
	int output = average(values, size);
	if(values[size]>output){
		cout << "The values above average are: " << output << endl;
	}

	return 0;
}

int average(int values[],int size){
	float temp=0.0;
	for(int i=0;i<size;i++){
		temp += values[i];
	}
	float end=temp/size;
	
	return end;
}



EDIT: added the floats in the average() function. But there is still a value problem.
Last edited on
I am only getting one value as output, and that value seems to be completely off
Yes, size is out of bounds.

Put the if statement on line 12 inside a loop (like on line 21) and replace size with i. That's it
Topic archived. No new replies allowed.