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
|
\*This program is designed to take five numbers input by the user and return the; sum, product, average,
largest value and smallest value of the provided numbers.
Algorithm steps:
1. Create a function to calculate the sum of 5 numbers
2. Create a function to calculate the product of 5 numbers
3. Create a function to calculate the average of 5 numbers
4. Create a function that determines the largest of 5 numbers
5. Create a function that determines the smallest of 5 numbers
6. Create a main function
7. Create an output prompting the user to
*/
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
using namespace std;
double findSum(double num1, double num2, double num3, double num4, double num5) {
double sum = (num1 + num2 + num3 + num4 + num5);
return sum;
}
double findProduct(double num1, double num2, double num3, double num4, double num5) {
double product = (num1 * num2 * num3 * num4 * num5);
return product;
}
double findAvg(double num1, double num2, double num3, double num4, double num5) {
double avg = findSum(num1, num2, num3, num4, num5)/5;
return avg;
}
double findLargest(double num1, double num2, double num3, double num4, double num5) {
if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5) {
return num1;
}
if (num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5) {
return num2;
}
if (num3 > num2 && num3 > num1 && num3 > num4 && num3 > num5) {
return num3;
}
if (num4 > num2 && num4 > num3 && num4 > num1 && num1 > num5) {
return num4;
}
if (num5 > num2 && num5 > num3 && num5 > num4 && num5 > num1) {
return num5;
}
}
double findSmallest(double num1, double num2, double num3, double num4, double num5) {
if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5) {
return num1;
}
if (num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5) {
return num2;
}
if (num3 < num2 && num3 < num1 && num3 < num4 && num3 < num5) {
return num3;
}
if (num4 < num2 && num4 < num3 && num4 < num1 && num1 < num5) {
return num4;
}
if (num5 < num2 && num5 < num3 && num5 < num4 && num5 < num1) {
return num5;
}
}
int main()
{
char newnumbers;
double n1 = 0;
double n2 = 0;
double n3 = 0;
double n4 = 0;
double n5 = 0;
//double sum = findSum(n1, n2, n3, n4, n5);
//double num1, num2, num3, num4, num5; FIXME // Fixed
while(true)
cout << "input 5 numbers separated by a space" << endl;
cin >> n1 >> n2 >> n3 >> n4 >> n5;
cout << "The sum of the 5 numbers is: " << findSum(n1, n2, n3, n4, n5) << endl;
cout << "The product of the 5 numbers is: " << findProduct(n1, n2, n3, n4, n5) << endl;
cout << "The average of the 5 numbers is: " << findAvg(n1, n2, n3, n4, n5) << endl;
cout << "The largest of the 5 numbeers is: " << findLargest(n1, n2, n3, n4, n5) << endl;
cout << "The smallest of the 5 numbeers is: " << findSmallest(n1, n2, n3, n4, n5) << endl;
cout << "Would you like to enter new numbers?? (y = yes/ n = no)" << endl; //prompts the user to enter y (yes) or n (no)
cin >> newnumbers;
if (newnumbers == 'y') //if newnumbers == 'y' then allows user to enter 5 new numbers
continue;
else
break;
cin.ignore(1000, '\n');
cin.get();
return 0;
}
|