Jump to content



Photo

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


  • Please log in to reply
7 replies to this topic

#1 higueraalfredo

higueraalfredo

    Neowinian

  • Joined: 24-April 05

Posted 19 November 2005 - 03:19

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



#2 ozzy76

ozzy76

    Neowinian

  • Joined: 25-November 02
  • Location: USA

Posted 19 November 2005 - 04:48

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


#3 Julius Caro

Julius Caro

    Neowinian Senior

  • Joined: 11-October 04

Posted 19 November 2005 - 10:03

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

#4 nikhil500

nikhil500

    Neowinian

  • Joined: 13-March 04

Posted 19 November 2005 - 13:32

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.

#5 +Xinok

Xinok

    Resident Reresident

  • Joined: 28-May 04
  • Location: Shikaka
  • OS: Windows 7 x64
  • Phone: Galaxy S3 (Wicked)

Posted 19 November 2005 - 13:43

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.

#6 nikhil500

nikhil500

    Neowinian

  • Joined: 13-March 04

Posted 19 November 2005 - 17:06

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.

#7 CdCViRus

CdCViRus

    Neowinian Senior

  • Joined: 27-July 02

Posted 20 November 2005 - 01:36

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


#8 OP higueraalfredo

higueraalfredo

    Neowinian

  • Joined: 24-April 05

Posted 20 November 2005 - 18:48

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.