// This program demonstrates the vector\'s empty member function. #include <iost
ID: 3888543 • Letter: #
Question
// This program demonstrates the vector's empty member function.
#include <iostream>
#include <vector>
using namespace std;
// Function prototype
double avgVector(vector<int>);
int main()
{
__________________ // A vector to hold values
int numValues; // The number of values
double average; // To hold the average
// Get the number of values to averge.
cout << "How many values do you wish to average? ";
cin >> numValues;
// Get the values and store them in the vector.
for (int count = 0; count < numValues; count++)
{
____________________; //fill in the blank
____________________;
____________________;
____________________;
}
// Get the average of the values and display it.
average = avgVector(values);
cout << "Average: " << average << endl;
return 0;
}
//*************************************************************
// Definition of function avgVector. *
// This function accepts an int vector as its argument. If *
// the vector contains values, the function returns the *
// average of those values. Otherwise, an error message is *
// displayed and the function returns 0.0. *
//*************************************************************
double avgVector(vector<int> vect)
{
int total = 0; // accumulator
double avg; // average
if (__________________) // Determine if the vector is empty
{
________________________;
________________________;
}
else
{
__________________________;
____________________________;
____________________________;
}
return avg;
}
Explanation / Answer
// This program demonstrates the vector's empty member function.
#include <iostream>
#include <vector>
using namespace std;
// Function prototype
double avgVector(vector<int>);
int main()
{
vector<int> vect; // A vector to hold values
int numValues; // The number of values
double average; // To hold the average
// Get the number of values to averge.
cout << "How many values do you wish to average? ";
cin >> numValues;
// Get the values and store them in the vector.
for (int count = 0; count < numValues; count++)
{
int num;
cout<<"Enter number:";
cin >> num;
//push elements to back of vector.
vect.push_back(num);
}
// Get the average of the values and display it.
average = avgVector(vect);
cout << "Average: " << average << endl;
return 0;
}
//*************************************************************
// Definition of function avgVector. *
// This function accepts an int vector as its argument. If *
// the vector contains values, the function returns the *
// average of those values. Otherwise, an error message is *
// displayed and the function returns 0.0. *
//*************************************************************
double avgVector(vector<int> vect)
{
int total = 0; // accumulator
double avg; // average
if (vect.empty()) // Determine if the vector is empty
{
cout << "Vector is empty" << endl;
return 0;
}
else
{
//calculate sum of values in vector and store it in total
for(vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
total += *it;
//calculate average
avg = total/vect.size();
}
return avg;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.