void, display min. & max.

Hoe do I find min and max using void and display?

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

int main()
{
	int x, y, z;
	int total;
	float average;

	//Read data into x, y, z
	void ReadData(int & x, int & y, int & z); {
		{
			cout << "Enter three integer numbers: ";
			cin >> x >> y >> z;
		}

	//Comput total of x, y, z
	int total;
	int ComputSum(int a, int b, int c);
	total = ComputeSum(x, y, z);
	int ComputeSum(int x, int y, int z);
		{  x + y + z; } }

	//Compute average of x, y, z
	void ComputeAverage(int x, int y, int z);
	{  float average = (x + y + z);
	//Display total and average
	void Display(int total, float average);
	{
		cout << fixed << showpoint << setprecision(2);
		cout << "Total= " << x + y + z << endl;
		cout << "Average= " << average / 3 << endl;
	}
	}

	int max, min;
	int FindMaxMin(int x, int y, int z, int max, int min);
	{
		cout << "the max and min values of " << x << "," << y << "," << " and " << z << "are " << max << " and " << min; 
	}

	//Terminate the program
	system("pause");
	return 0;
}
Not sure what you mean by "using void and display." I would compare x, y and z in your FindMaxMin function, simply by using some if statements. Comparing x to y, then x to z, then y to z. Tinker with the arithmetic, but it's pretty easy to check. Then just save the result and output using the statement you already have.

You could also use AND, which may eliminate an if statement.
Last edited on
I'm not sure how to find min and max in a void statement
Ok so I got something like this I guess but I'm getting 0's as max and min ?

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

int main()
{
	int x, y, z;
	int total;
	float average;

	//Read data into x, y, z
	void ReadData(int & x, int & y, int & z); {
		{
			cout << "Enter three integer numbers: ";
			cin >> x >> y >> z;
		}
	//Comput total of x, y, z
	int total;
	int ComputSum(int a, int b, int c);

	//Compute average of x, y, z
	void ComputeAverage(int x, int y, int z);
	{  float average = (x + y + z);
	//Display total and average
	void Display(int total, float average);
	{
		cout << fixed << showpoint << setprecision(2);
		cout << "Total= " << x + y + z << endl;
		cout << "Average= " << average / 3 << endl;
	}
	}
void FindMaxMin(int x, int y, int z, int max, int min);
	int max=100,num=0,min=0; 
	for (int i=0; num!=1; i++)
	{
		if(num==-1)break; 
		if (num>min)
			min=num;
		if (num<max)
			max=num;
		cout << "the max and min values of " << x << ", " << y << "," << " and " << z 
			 << " are " << max << " and " << min; 
		cin.ignore(80, 'n');	
	}

	//Terminate the program
	system("pause");
	return 0;
}
}
Last edited on
closed account (D80DSL3A)
Umm... You can't define functions inside of other functions like that.
Have you tried compiling this code yet?

Judging from the parameter list for FindMaxMin I assume you're meant to pass min and max by reference, then find the values within the function.
ya I compiled and it gave me this:

1
2
3
4
5
6
Enter three integer numbers: 5 3 8
Total= 16
Average= 5.33
the max and min values of 5,3, and 8 are 0 and 0

I just used if else statements for each one, took forever but works though.
closed account (D80DSL3A)
My bad. Those are just function declarations, not definitions. I see it's ok for them to be there.
closed account (D80DSL3A)
May I suggest something more like this?
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
#include<iostream>
#include <iomanip>
using namespace std;

void ReadData(int & x, int & y, int & z)
{
    cout << "Enter three integer numbers: ";
    cin >> x >> y >> z;
}
//Comput total of x, y, z
int ComputSum(int a, int b, int c){ return a + b + c; }
//Compute average of x, y, z
float ComputAverage(int a, int b, int c){ return (a + b + c )/3.0f; }
	//Display total and average
void Display(int total, float average)
{
    cout << fixed << showpoint << setprecision(2);
    cout << "Total= " << total << endl;
    cout << "Average= " << average << endl;
}

void findMinMax( int x, int y, int z, int& min, int& max )
{
    min = max = x;
    if( y < min ) min = y;
    if( z < min ) min = z;
    if( y > max ) max = y;
    if( z > max ) max = z;
}

int main()
{
    int x, y, z;
    ReadData( x, y, z);
    Display( ComputSum( x, y, z ), ComputAverage( x, y, z ) );
    int min, max;
    findMinMax( x, y, z, min, max );
    cout << "the max and min values of " << x << ", " << y << "," << " and " << z
			 << " are " << max << " and " << min << endl;

    //Terminate the program
	system("pause");
    return 0;
}
Topic archived. No new replies allowed.