• 0

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


Question

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& max, int& min)
{
	if(x<=y && x<=z)
	{
  min=x;
  if(y<z)
  	max=z;
  else
  	max=y;
	}

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

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

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

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

#include <iostream>
using namespace std;

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

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

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

	for (int i=0;i<3;i++){
  if (number[i] <= small)
  	small = number[i];
  if (number[i] >= large)
  	large = number[i];
	}
  
	cout << small << " is the smallest number." << endl;
	cout << large << " is the largest number." << endl;

	system("pause");
	return 0;
}

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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& max, int& min)
{
     if(x<=y && x<=z)
     {
          min=x;
          if(y<z)
               max=z;
          else
               max=y;
     }

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

     else
     {
          min=z;
          if(x<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.

Link to comment
Share on other sites

  • 0

My stab at it:

#include <iostream>
using namespace std;

#define GetMin(a,b) ((a) < (b) ? (a) : (b))
#define GetMax(a,b) ((a) > (b) ? (a) : (b))

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

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

	return 0;
}

Edited: Updated to use Define macros.

Link to comment
Share on other sites

  • 0

Another attempt.....

void FindMaxMin(int x, int y, int z, int& max, int& min)
{
     if (x<=y)
     {
          if (x<=z)
          {
               min=x;
               if (y<=z)
                    max=z;
               else
                    max=y;
          }
          else
          {
               min=z;
               max=y;
          }
     }
     else if (x<=z)
     {
          max=z;
          min=y;
     }
     else
     {
          max=x;
          if(y<=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.

Link to comment
Share on other sites

  • 0

#define max(a,b) ((a) > (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& max, int& min)
{
      max = max3(x, y, z);
      min = min3(x, y, z);
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.