Project 02
Home ] Up ]

 

//Project02.cpp
//Date 08/31/2001
//Author Us

//main program and a function to calculate max 2 of 3

#include <iostream.h>


void swap(int &p, int &q)
  {
  if (p != q)
    {
    int temp = p;
    p = q;
    q = temp; 
    }
  }


void swap(float &p, float &q)
  {
  if (p != q)
    {
    float temp = p;
    p = q;
    q = temp; 
    }
  }


void swap(double &p, double &q)
  {
  if (p != q)
    {
    double temp = p;
    p = q;
    q = temp; 
    }
  }


void max2(int a, int b, int c, int &x, int &y)
  {
  for (int i=1; i<=2; i++)
    {
    if (a < b)
      swap(a, b);
    if (b < c)
      swap(b, c);
    }

  x = a;
  y = b;
  }


void max2(int a, int b, int c, int d, int &x, int &y)
  {
  for (int i=1; i<=3; i++)
    {
    if (a < b)
      swap(a, b);
    if (b < c)
      swap(b, c);
    if (c < d)
      swap(c, d);
    }

  x = a;
  y = b;
  }


void max2(float a, float b, float c, float &x, float &y)
  {
  for (int i=1; i<=2; i++)
    {
    if (a < b)
      swap(a, b);
    if (b < c)
      swap(b, c);
    }

  x = a;
  y = b;
  }


void max2(double a, double b, double c, double &x, double &y)
  {
  for (int i=1; i<=2; i++)
    {
    if (a < b)
      swap(a, b);
    if (b < c)
      swap(b, c);
    }

  x = a;
  y = b;
  }


void main(void)
  {
  int x, y;

  max2(1, 2, 3, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(1, 2, 3, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(3, 1, 2, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(3, 2, 1, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(2, 1, 3, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(2, 3, 1, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  float xf, yf;

  max2(1.5, 3.5, 2.5, xf, yf);
  cout << "max2(1.5, 3.5, 2.5) = " << xf << ' ' << yf << endl;

  max2(1, 2, 3, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(3, 1, 2, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(3, 2, 1, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(2, 1, 3, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;

  max2(2, 3, 1, x, y);
  cout << "max2(1, 2, 3) = " << x << ' ' << y << endl;


  max2(1, 2, 3, 4, x, y);
  cout << "max2(1, 2, 3, 4) = " << x << ' ' << y << endl;

  }