Sign in to follow this  
Followers 0
higueraalfredo

[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& 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;
	}
}

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
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 this post


Link to post
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& 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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#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);
}

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.