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
|
#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
//getvalue
int getValue( int MIN_VALUE, int MAX_VALUE )
{int number = 0;
do
{ cout << "Enter a number.";
cin >> number;
if(number >= MIN_VALUE && number <= MAX_VALUE)
return number;
else
{cout << "Error: the value must be between -100 and 100. Try again:";}
}
while(1);
}
//lowNUM
int findLow(int x,int y,int z,int a)
{int smallest = x;
if (y < smallest)
smallest = y;
if (z < smallest)
smallest = z;
if (a < smallest)
smallest = a;
return smallest;
}
//highnumber
int findHigh(int x,int y,int z,int a)
{int largest = x;
if (y > largest )
largest = y;
if (z > largest)
largest = z;
if (a > largest)
largest = a;
return largest;
}
//sum
int calcSum(int w, int x, int y, int z)
{int sumsum = w + x + y + z;
return sumsum;}
//mean
int calcMean(int w, int x, int y, int z)
{double mean = (w + x + y + z)/4.000;
return mean;}
//stddev
int calcStdDev(int w, int x, int y, int z)
{
double sigma1 = (w*w) + (x*x) + (y*y) +(z*z);
double sigma2 = (w+x+y+z)*(w+x+y+z);
double stddev = sqrt(((sigma1)-(sigma2/4.0))/3.0);
return stddev;}
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;
}
|