HW ?...almost there....I think

Write a program with 3 functions:

void input(int vals[], const int MAX, int & size);

void maxmin(int vals[], int size, int& max, int & min);

void output(int vals[], int size);

In your main program, define the array as int values[MAXSIZE];

with const int MAXSIZE = 100;

Call input, then output, then call maxmin and display the max and min values.

Read the values into the array in the input function and display them in the output function. The maxmin function should “return” the largest and smallest values in the array. The input function should detect the end of input by reading the ctrl-z key combination from the keyboard.

Run the program for the values 1,2,3,4,5,6,7,8,9,10 as input.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
using namespace std;

void input(int vals[], const int MAX, int& size);
void maxmin(int vals[], int size, int& max, int& min);
void output(int vals[], int size);

void main()
{
	const int MAXSIZE = 100;
	int values[MAXSIZE];
	int size, max, min;

	input(values, MAXSIZE, size);
	maxmin(values, size, max, min);
	output(values, size);
}

void input(int vals[], const int MAX, int& size)
{
	size = 0;
	int value;
	cout << "enter values (ctrl-z to end): \n";
	while ((cin >> value) && (size < MAX))
	{
		vals[size] = value;
		size++;
	}
	return;
}

void maxmin(int vals[], int size, int& max, int& min)
{
	max = vals[0];
	min = vals[0];

	for (int i = 1; i < size; i++)
	{
		if (min > vals[i])
		{
			min = vals[i];
		}
		if (max < vals[i])
		{
			max = vals[i];
		}
	}


}

void output(int vals[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout<< vals[i] << " ";
	}
	cout<<endl;
	return;
}


My output is the numbers occupying the array 1,2,3,4,... No surprise there as I just inserted some code from another array and function problem we did.

What I don't understand is how am I supposed to pass the min and max values to the output function if I can't list them in my formal parameters in the declaration or as variable in the argument in the call. What am I missing???
First it should be int main() line 29 and 59 is not needed and does nothing.

What I don't understand is how am I supposed to pass the min and max values to the output function if I can't list them in my formal parameters in the declaration or as variable in the argument in the call. What am I missing???
Well for me there are two ways I would interpret it as
Call input, then output, then call maxmin and display the max and min values.
1) call input (in main), 2) call output (in main), 3) call maxmin (in main), 4) output min and max (in main) or 1) call input (in main), 2)call output (in main), 3) call maxmin (in output), 4) display min and max (in output) The latter you would have to create min and max inside of the output function instead of main function though.
Thanks I got it. Here's the code. See question down below.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <iostream>
using namespace std;

void input(int vals[], const int MAX, int& size);
void maxmin(int vals[], int size, int& max, int& min);
void output(int vals[], int size);

void main()
{
	const int MAXSIZE = 100;
	int values[MAXSIZE];
	int size, max, min;

	input(values, MAXSIZE, size);
	maxmin(values, size, max, min);
	output(values, size);

	cout << "The largest number is: " << max << " the smallest number is: " << min << endl;
}

void input(int vals[], const int MAX, int& size)
{
	size = 0;
	int value;
	cout << "enter values (ctrl-z to end): \n";
	while ((cin >> value) && (size < MAX))
	{
		vals[size] = value;
		size++;
	}
	return;
}

void maxmin(int vals[], int size, int& max, int& min)
{

	max = vals[0];
	min = vals[0];

	for (int i = 1; i < size; i++)
	{
		if (min > vals[i])
		{
			min = vals[i];
		}
		if (max < vals[i])
		{
			max = vals[i];
		}
	}


}

void output(int vals[], int size)
{
	

	for (int i = 0; i < size; i++)
	{
		cout << vals[i] << " ";
	}
	cout<<endl;
	return;
}


or 1) call input (in main), 2)call output (in main), 3) call maxmin (in output), 4) display min and max (in output) The latter you would have to create min and max inside of the output function instead of main function though.


To do this I would it look like this?

1
2
3
4
5
6
7
8
9
10
11
12
void output(int vals[], int size, int& max, int& min)
{
	maxmin(values, size, max, min);

	for (int i = 0; i < size; i++)
	{
		cout << vals[i] << " ";
	}
	cout<< " The max is: " << max << " The min is: " << min << endl;
	return;

}
You wouldn't pass max and min. You would actually declare them inside the function since you wouldn't be needing them in the main function if you did it that way. It would look like:

1
2
3
4
5
6
7
8
9
10
11
12
void output(int vals[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << vals[i] << " ";
	}

        int max = 0;
        int min = 0;
	maxmin(values, size, max, min);
	cout<< " The max is: " << max << " The min is: " << min << endl;
}
Thanks very much
Topic archived. No new replies allowed.