Program goal:
User enters five numbers. Store the numbers in a vector of doubles. Output the vector's numbers on one line, each number followed by one space. (2 pts)
My issue:
Input and output additional numbers in the beginning.'
Desired Output-------------
Enter weight 1:
236.0
Enter weight 2:
89.5
Enter weight 3:
142.0
Enter weight 4:
166.3
Enter weight 5:
93.0
You entered: 236.00 89.50 142.00 166.30 93.00
Total weight: 726.80
Average weight: 145.36
Max weight: 236.00
Current Output------------
Enter weight 1:
0.0
Enter weight 2:
0.0
Enter weight 3:
0.0
Enter weight 4:
0.0
Enter weight 5:
236.0
Enter weight 6:
89.5
Enter weight 7:
142.0
Enter weight 8:
166.3
Enter weight 9:
93.0
You entered: 0.00 0.00 0.00 0.00 0.00 236.00 89.50 142.00 166.30 93.00
Total weight: 726.80
Average weight is: 145.36
Max weight: 236.00
-----
#include <iostream>
#include <iomanip>
#include <vector>
usingnamespace std;
int main() {
constint inputNum = 5;
vector<double>input; //(inputNum); // this prefills the vector hence the 0's
double currInput;
// int i; not a good idea for loops to be initialized this way
// int j;
// int l; l is easily confused with the number '1'
double totalWeight;
double averageWeight;
double maxWeight;
for (int i = 0; i < inputNum; ++i){
cin >> currInput;
input.push_back(currInput);
}
cout << fixed << setprecision(1);
for (int i = 0; i < input.size(); ++i)
{
cout << "You entered at " << i << ": " << input.at(i) << endl;
}
totalWeight = 0;
for (int j = 0; j < input.size(); ++j){
totalWeight += input.at(j);
}
cout << "Total weight: " << totalWeight << endl;
averageWeight = totalWeight/ 5;
cout << "Average weight is: " << averageWeight << endl;
maxWeight = input.at(0);
for (int l = 0; l < input.size(); ++l){
if ( input.at(l) > maxWeight){
maxWeight = input.at(l);
}
}
cout << "Max weight: " << maxWeight << endl;
return 0;
}
1 2 3 4 5
You entered at 0: 1.0
You entered at 1: 2.0
You entered at 2: 3.0
You entered at 3: 4.0
You entered at 4: 5.0
Total weight: 15.0
Average weight is: 3.0
Max weight: 5.0
Program ended with exit code: 0