1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//Symbolic Constants
const int MIN_VALUE = -100; //The smallest possible number
const int MAX_VALUE = 100; //The largest possible number
int getValue( int lowerBound, int upperBound );
float findLow(float,float,float,float);
float findHigh(float,float,float,float);
float calcSum(float,float,float,float);
float calcMean(float,float,float,float);
float calcStdDev(float,float,float,float);
//Put the function prototypes here
int main()
{
/**********************************************************************
Variable Dictionary
num1, num2, num3, num4 The 4 numbers that are enteded by the user
sum The calculated sum
highNum, lowNum The largest and smallest numbers
avg The calculated average
stdDev The calculated standard deviation
again The user's choice for continuation
**********************************************************************/
int num1, num2, num3, num4;
int sum, highNum, lowNum;
double avg, stdDev;
char again = 'Y';
cout << fixed << setprecision(3);
//While the user wants to perform calculations
while( again == 'Y' or again == 'y' )
{
//Get 4 numbers from the user
num1 = getValue( MIN_VALUE, MAX_VALUE );
num2 = getValue( MIN_VALUE, MAX_VALUE );
num3 = getValue( MIN_VALUE, MAX_VALUE );
num4 = getValue( MIN_VALUE, MAX_VALUE );
//Find the smallest and largest numbers in the list
lowNum = findLow( num1, num2, num3, num4 );
highNum = findHigh( num1, num2, num3, num4 );
//Calculate the sum, average and standard deviation of the numbers
sum = calcSum( num1, num2, num3, num4 );
avg = calcMean( num1, num2, num3, num4 );
stdDev = calcStdDev( num1, num2, num3, num4 );
//Display the numbers that were used in the calculations and the
//results of the calculations
cout << endl << endl << "The numbers that were entered: "
<< setw(5) << num1 << setw(5) << num2 << setw(5) << num3
<< setw(5) << num4
<< endl << " Smallest:" << setw(20) << lowNum
<< endl << " Largest:" << setw(21) << highNum
<< endl << " Sum:" << setw(25) << sum
<< endl << " Average:" << setw(21) << avg
<< endl << " Standard Deviation:" << setw(10) << stdDev
<< endl << endl;
//Ask the user if they have another set of numbers
cout << "Again (Y/N)? ";
cin >> again;
}
return 0;
}
//Code the funtions below this line
int num1, num2, num3, num4 (int getValue)
{
cout << "Enter a value in the range (-100 - 100): ";
cin >> getValue;
while (getValue < -100.0 || getValue > 100.0)
{
cout << "Error: the value must be between -100 and 100. Try again: ";
cin >> getValue;
}
}
float findLow(float num1, float num2, float num3, float num4)
{
float Low = num1;
if (num2 < Low)
Low = num2;
if (num3 < Low)
Low = num3;
if (num4 < Low)
Low = num4;
return Low;
}
float findHigh(float num1, float num2, float num3, float num4)
{
float High = num1;
if (num2 > High)
High = num2;
if (num3 > High)
High = num3;
if (num4 > High)
High = num4;
return High;
}
float calcSum(float num1, float num2, float num3, float num4)
{
return num1 + num2 + num3 + num4;
}
float calcMean(float num1, float num2, float num3, float num4)
{
float avg = (num1 + num2 + num3 + num4) / 4;
return avg;
}
float calcStdDev (float num1, float num2, float num3, float num4)
{
return ((num1 * num1) + (num2 * num2) + (num3 * num3) + (num4 * num4))-(((num1+num2+num3+num4)*(num1+num2+num3+num4))/4);
}
|