Followers 0

# [C++] Find Minimum and Maximum of 3 Numbers

## 8 posts in this topic

Is there a better way to find the minimum and maximum of three numbers?

This is what I have for the function right now

```void FindMaxMin(int x, int y, int z, int&amp; max, int&amp; min)
{
if(x&lt;=y &amp;&amp; x&lt;=z)
{
min=x;
if(y&lt;z)
max=z;
else
max=y;
}

if (y&lt;=x &amp;&amp; y&lt;=z)
{
min=y;
if(x&lt;z)
max=z;
else
max=x;
}

if (z&lt;=x &amp;&amp; z&lt;=y)
{
min=z;
if(x&lt;y)
max=y;
else
max=x;
}
}
```

##### Share on other sites

I used an array. Here's the whole program.

```#include &lt;iostream&gt;
using namespace std;

int main()
{
int temp,small,large;
int number[3];

for (int a=0;a&lt;3;a++){
system("cls");
cout &lt;&lt; "Enter a number: " &lt;&lt; endl;
cin &gt;&gt; temp;
number[a] = temp;
}

small = number[0];
large = number[0];

for (int i=0;i&lt;3;i++){
if (number[i] &lt;= small)
small = number[i];
if (number[i] &gt;= large)
large = number[i];
}

cout &lt;&lt; small &lt;&lt; " is the smallest number." &lt;&lt; endl;
cout &lt;&lt; large &lt;&lt; " is the largest number." &lt;&lt; endl;

system("pause");
return 0;
}
```

##### Share on other sites

Yup I did something similar a while ago in my C course.

Basically, you have a certain amount of numbers, and two variables, max and min for example.

let's say the first number is min and the last one is max, or whatever :p You basically make a loop, and compare the current max / min with all the numbers. If there is any number smaller or larger, then set the new max to that number.

Or something like that :p

##### Share on other sites

This one would be better than yours. Have not compiled it, so it may have some syntax errors.

```
void FindMaxMin(int x, int y, int z, int&amp; max, int&amp; min)
{
if(x&lt;=y &amp;&amp; x&lt;=z)
{
min=x;
if(y&lt;z)
max=z;
else
max=y;
}

else if (y&lt;=z)
{
min=y;
if(x&lt;z)
max=z;
else
max=x;
}

else
{
min=z;
if(x&lt;y)
max=y;
else
max=x;
}
}

```

I have a feeling that it can be improved much more. Will post if I come up with something.

##### Share on other sites

My stab at it:

```#include &lt;iostream&gt;
using namespace std;

#define GetMin(a,b) ((a) &lt; (b) ? (a) : (b))
#define GetMax(a,b) ((a) &gt; (b) ? (a) : (b))

int main(){
int array[] = {3, 5, 7};
int min, max; min = max = array[0];

for(int i = 1; i &lt; sizeof(array)/sizeof(array[0]); i++){
?min = GetMin(min, array[i]);
?max = GetMax(max, array[i]);
}

return 0;
}```

Edited: Updated to use Define macros.

##### Share on other sites

Another attempt.....

```void FindMaxMin(int x, int y, int z, int&amp; max, int&amp; min)
{
if (x&lt;=y)
{
if (x&lt;=z)
{
min=x;
if (y&lt;=z)
max=z;
else
max=y;
}
else
{
min=z;
max=y;
}
}
else if (x&lt;=z)
{
max=z;
min=y;
}
else
{
max=x;
if(y&lt;=z)
min=y;
else
min=z;
}
}
```

I guess this is the best I can do. The program will perform a maximum of 3 comparisons and 2 assignments.

##### Share on other sites

```#define max(a,b) ((a) &gt; (b) ? (a) : (b))
#define max3(a,b,c) (max((a), max((b),(c))))
```

same for min

then, your function will look like this:

```void FindMaxMin(int x, int y, int z, int&amp; max, int&amp; min)
{
max = max3(x, y, z);
min = min3(x, y, z);
}
```

##### Share on other sites

Well it seems like this is done with if statements so I'll stick with mine and I haven't been taught about that #define thing so I don't want to use it.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account