error while doing array with function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
void getExtremes(float& min, float& max, float a[], int n)
{
	min = max = a[0];
	for (int i=1; i<n; i++)
		if (a[i] < min) 
			min = a[i];
		else if (a[i] > max) 
			max = a[i];
}

int main()
{
	const int n = 8;
	float a[] = {1, 2, 3, 4, 5, 6, 7, 8};
	cout << getExtremes(min,max,a,n);
	return 0;
}


what is the problem with this code?
getExtremes has a void return value so you can't output it with to cout.
You try to pass min and max but they are not declared.
so what should i do in order to make it without any errors?
create min and max variables to pass to getExtremes and print them instead of the return value.
let's say i change it to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
float getExtremes(float& min, float& max, float a[], int n){
	min = max = a[0];
	for (int i=1; i<n; i++)
		if (a[i] < min) 
			min = a[i];
		else if (a[i] > max) 
			max = a[i];
	return 0;
}

int main(){
	int n = 8;
	float min,max;
	float a[] = {1, 2, 3, 4, 5, 6, 7, 8};
	cout<<getExtremes(min,max,a,n);
	return 0;
}

so now what is the problem?
It depends on what you want it to do. The code now compiles.
ok...let say...i didn't use the void..so the code should be...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
float getExtremes(float& min, float& max, float a[], int n){
	min = max = a[0];
	for (int i=1; i<n; i++)
		if (a[i] < min) 
			min = a[i];
		else if (a[i] > max) 
			max = a[i];
	return 0;
}

int main(){
	int n = 8;
	float min,max;
	float a[] = {1, 2, 3, 4, 5, 6, 7, 8};
	getExtremes(min,max,a,n);
	cout << min << endl;
	cout << max << endl;
	return 0;
}

now if i use void...so how am i suppose to change the cout to print...
what!? your code is working .. If you are never going to use the return value of getExtremes you can change it to void. the code should still work.
thanks for ur help !! (:
Topic archived. No new replies allowed.