error while doing array with function

Nov 28, 2011 at 6:24pm
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?
Nov 28, 2011 at 6:27pm
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.
Nov 28, 2011 at 6:28pm
so what should i do in order to make it without any errors?
Nov 28, 2011 at 6:33pm
create min and max variables to pass to getExtremes and print them instead of the return value.
Nov 28, 2011 at 6:39pm
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?
Nov 28, 2011 at 6:50pm
It depends on what you want it to do. The code now compiles.
Nov 28, 2011 at 6:52pm
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...
Nov 28, 2011 at 7:00pm
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.
Nov 28, 2011 at 7:03pm
thanks for ur help !! (:
Topic archived. No new replies allowed.