In: Computer Science
STL vector C++
|
Write a program in C++ that prompts the user to
Enter test scores (-1 to quit)
(see output)
The program calculates and displays
the average of the test scores.
calculate and return the average to main()
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<double> a;
void getScores();
float calcAvg(vector<double> &a);
void displayAvg();
int main()
{
getScores();
displayAvg();
return(0);
}
void getScores()
{
double mark,d;
cout<<"\nEnter a score(-1 to quit):";
cin>>mark;
while(mark!=-1)
{
a.push_back(mark);
cout<<"\nEnter a score(-1 to
quit):";
cin>>mark;
}
}
float calcAvg(vector<double> &a){
float sum;
for(int i=0; i<a.size();i++)
{
sum += a[i];
}
float avg = sum/a.size();
return avg;
}
void displayAvg()
{
cout << "The Average Score is
"<<calcAvg(a);
}