Question

In: Computer Science

STL vector C++ /* ---- OUTPUT ---- Enter test scores (-1 to quit) Enter a score:  77...

STL vector C++

/* ---- OUTPUT ----

Enter test scores (-1 to quit)

Enter a score:  77

Enter a score:  83

Enter a score:  99

Enter a score:  67

Enter a score:  88

Enter a score:  -1

The average score is 82.8.

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.

  • Use a vector to hold the values
    • double data type
  • Pass the vector to a function named:  getScores
    • Use this function to get the scores.
    • The user can enter as many scores, and then press -1 to stop.
  • Then pass the vector to a function named:  calcAvg
    • In this function, add up all values in the vector, then

  calculate and return the average to main()

  • Finally, call a function named:  displayAvg
    • Display the average of the test scores.

Solutions

Expert Solution

#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);
}


Related Solutions

Discuss tradeoffs between using the C++ STL list and vector.
Discuss tradeoffs between using the C++ STL list and vector.
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Language: C++ Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in...
Language: C++ Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in inserting and finding elements, with a relatively large data set. What you'll be measuring is the execution time. Obviously it's dependent on the speed of the computer one uses, so what matters is the relative speed (relative to the vector's speed in percentage) create a vector of 100,000 elements of integers, with the values initially set as 1~100,000, in this order. randomize the ordering...
C++ please test 1 > run Enter 1st vector (2 floats): 0 0 Coordinates cannot both...
C++ please test 1 > run Enter 1st vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 1st vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 1st vector (2 floats): 1 0 Enter 2nd vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 2nd vector (2 floats): 0 0 Coordinates cannot both be zero. Enter 2nd vector (2 floats): 1 0 First vector: (1, 0) has length 1 Second vector: (1, 0) has...
1. The test scores of 10 students are listed below. 32, 69, 77, 82, 100, 68,...
1. The test scores of 10 students are listed below. 32, 69, 77, 82, 100, 68, 88, 95, 75, 80 a. Determine the five-number summary and draw a boxplot for the given data above. Minimum ________    Q1 ________    Median ________    Q3 ________ Maximum ________ b. Which number(s) is suspected outliers? Justify your answer.    c. Is it possible to have a mean of 96 if the scores ranged from 65 to 95? Justify your answer.
Using only core C++ (no special libraries, except STL vector or string if you want), write...
Using only core C++ (no special libraries, except STL vector or string if you want), write a C++ program that allows a user to input a string and (a) Checks if the expression is a valid polynomial. Parentheses or negation are not allowed. Spaces should be ignored. E.g., the following are valid i. n^2+2*n+5 ii. 2*n + 4.54* n^5 +4 +5*n and the following are invalid iii. n^3n iv. n^4.2 v. 5n vi. n^3 -3*n if an input is given...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Do men score higher on average compared to women on their statistics test? test scores of...
Do men score higher on average compared to women on their statistics test? test scores of ten randomly selected male statistics students and twelve randomly selected female statistics students are shown below. Male: 63 91 92 56 76 61 83 61 62 71 Female: 46 85 57 53 46 70 46 72 53 61 84 46 Assume both follow a Normal distribution. What can be concluded at the the α = 0.05 level of significance level of significance? For this...
Do men score lower on average compared to women on their statistics test? Test scores of...
Do men score lower on average compared to women on their statistics test? Test scores of thirteen randomly selected male statistics students and twelve randomly selected female statistics students are shown below. Male:  84 80 91 86 78 80 69 93 64 81 75 95 63 Female:  92 76 99 73 80 99 70 95 97 95 94 74 Assume both follow a Normal distribution. What can be concluded at the the αα = 0.05 level of significance level of significance? For...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT