96.8k views
0 votes
Code and test this exercise in Visual Studio, then upload your Source.cpp file for checking.

1. Prompt the user to enter five numbers, being five people's weights. Store the numbers in a vector of doubles. Output the vector's numbers on one line, each number followed by one space. Ex: 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 89.5 142 166.3 93
2. Also output the total weight, by summing the vector's elements.
3. Also, output the average of the vector's elements.
4. Also, output the max vector element. Ex: 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 89.5 142 166.3 93 Total weight: 726.8 Average weight: 145.36 Max weight: 236

User Erich
by
5.0k points

1 Answer

6 votes

Answer:

Check the explanation

Step-by-step explanation:

#include <iostream>

#include<vector>

using namespace std;

int main()

{

//declaration

vector<double> weights;

double weight,totalWeight=0,avgWeight,maxWeight;

//reading data from console

for(int i=0; i<5; i++)

{

cout<<"Enter weight "<<i+1<<": ";

cin>>weight;

weights.push_back(weight);

}

//caliculating max total and average weight and printing the weights

maxWeight=weights.at(0);

cout<<"You entered: ";

for(int i=0; i<5; i++)

{

totalWeight+=weights.at(i);

if(weights.at(i)>maxWeight)

maxWeight=weights.at(i);

cout<<weights.at(i)<<" ";

}

avgWeight=totalWeight/5.0d;

cout<<"\\Total weight: "<<totalWeight<<endl;

cout<<"Average weight: "<<avgWeight<<endl;

cout<<"Max weight: "<<maxWeight<<endl;

return 0;

}

OUTPUT:

Code and test this exercise in Visual Studio, then upload your Source.cpp file for-example-1
User Vivek Doshi
by
5.8k points