This is similar to the exercise we did in class but with two changes: (1) Use doubles instead of ints; (2) The user will be providing the numbers. Just like the class exercise, output the sum, average, highest, and lowest of the user's numbers.
Output:
This program will ask you to enter 5 numbers with decimal places.
It will then show you the total, average, largest number, and smallest number.
Enter a number: [user types: 5.2]
Enter a number: [user types: 9.8]
Enter a number: [user types: 2.5]
Enter a number: [user types: 17.5]
Enter a number: [user types: 7.5]
Total: 42.5
Average: 8.5
Lowest: 2.5
Highest: 17.5
#include <iostream>
using namespace std;
int main()
{
double arr[5];
cout << "Please, enter 5 values: ";
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
double sum = 0;
double lowest = arr[0];
double highest = arr[0];
for (int i = 0; i < 5; i++)
{
sum += arr[i];
if (arr[i] < lowest)
lowest = arr[i];
if (arr[i] > highest)
highest = arr[i];
}
double aver = sum / 5;
cout << "\nTotal: " << sum
<< "\nAverage: " << aver
<< "\nLowest: " << lowest
<< "\nHighest: " << highest;
}
Comments
Leave a comment