/*************************************************/ //moreSort.h //R. A. Hillyard //October 15, 2001 //Interface file gives function prototypes //with per and post conditions /*************************************************/ void sort3(int& a, int& b, int& c); //per condition a, b, and c have been given integer values //post condition a, b, and c will be sorted from lowest to highest
void sort3(double& a, double& b, double& c); //per condition a, b, and c have been given double values //post condition a, b, and c will be sorted from lowest to highest
void swap(int& a, int& b); //per condition a and b have been given integer values //post condition the values of a and b will be interchanged
void swap(double& a, double& b); //per condition a and b have been given double values //post condition the values of a and b will be interchanged
/*************************************************/
/*************************************************/ //moreSortFunctions.cpp //R. A. Hillyard //October 15, 2001 //Implementation file gives function definitions /*************************************************/
#include "moreSort.h"
/*****************************************************************/ //sort3 - overloaded function that sorts three integers from smallest to largest /*****************************************************************/ void sort3(int& a, int& b, int& c) { if(a > b) { swap(a,b); } if(a > c) { swap(a,c); } if(b > c) { swap(b,c); } }//end sort3 /*****************************************************************/ //swap - overloaded function that interchanges the value of two integers /*****************************************************************/ void swap(int& a, int& b) { int temp = a; a = b; b = temp; }//end swap /*****************************************************************/ //sort3 - overloaded function that sorts three doubles from smallest to largest /*****************************************************************/ void sort3(double& a, double& b, double& c) { if(a > b) { swap(a,b); } if(a > c) { swap(a,c); } if(b > c) { swap(b,c); } }//end sort3 /*****************************************************************/ //swap - overloaded function that interchanges the value of two doubles /*****************************************************************/ void swap(double& a, double& b) { double temp = a; a = b; b = temp; }//end swap /*****************************************************************/
/*************************************************/ //moreSortMain.cpp //R. A. Hillyard //October 15, 2001 //Main program for moreSort /*************************************************/
#include<iostream> #include "moreSort.h"
using namespace std;
int main() { char choice; int a = 0, b = 0, c = 0; double x = 0.0, y = 0.0, z = 0.0; cout << "Double or Ints [D/I]: "; cin >> choice; if(choice == 'I') { cout << "enter three ints: "; cin >>a>>b>>c; sort3(a,b,c); cout << "\n sorted: " << a << " " << b << " " << c << endl; } if(choice == 'D') { cout << "enter three doubles: "; cin >>x>>y>>z; sort3(x,y,z); cout << "\n sorted: " << x << " " << y << " " << z << endl; } return 0; }//end main /**************************Program output*************************/ Double or Ints [D/I]: D enter three doubles: 6.6 4.3 8.2 sorted: 4.3 6.6 8.2 /*****************************************************************/ Double or Ints [D/I]: I enter three ints: 8 2 9 sorted: 2 8 9