Prompt the user to enter five numbers, being five people\'s weights. Store the n
ID: 3919776 • Letter: P
Question
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.
Also define and call a double returning function, weightSummary, that accepts the vector as a parameter, and does the following.
(a) output the total weight, by summing the vector's elements.
(b) output the average of the vector's elements.
(c) output the max vector element.
(d) returns the sum of the total weight, average and max vector element.
#include <iostream>
// include vector library
using namespace std;
//Function prototype
double weightSummary(vector<double>);
int main() {
vector<double> list1; //Do NOT modify this vector declaration
/* Type your code here. */
//Do NOT modify these last two statements
double showResults = weightSummary(list1);
return 0;
}
//Function definition
double weightSummary(vector<double> v){
// Type your code here
}
Explanation / Answer
#include <iostream>
#include <vector>
// include vector library
using namespace std;
//Function prototype
double weightSummary(vector<double>);
int main()
{
vector<double> list1; //Do NOT modify this vector declaration
double w;
for(int i = 0; i < 5; i++) {
cin >> w;
list1.push_back(w);
}
for(int i = 0; i < list1.size(); i++) {
cout << list1[i] << " ";
}
cout << endl;
//Do NOT modify these last two statements
double showResults = weightSummary(list1);
return 0;
}
//Function definition
double weightSummary(vector<double> v)
{
double sum = 0, max = v[0];
for(int i = 0; i < v.size(); i++) {
sum += v[i];
if(v[i] > max) {
max = v[i];
}
}
double average = sum / v.size();
cout << "total weight = " << sum << endl;
cout << "average = " << average << endl;
cout << "max = " << max << endl;
return sum + average + max;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.