Question

In: Computer Science

Write a C++ program that inputs a sequence of integers into a vector, computes the average,...

Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input:

10
20
0
99
-1

would produce the following output:

N: 4
Avg: 32.25
10
20
0
99

The main program has been written for you, your job is to implement the two functions InputData and GetAverage. See the comments in the "student.cpp" file for specific implementation details.

main.cpp

#include <iostream>
#include <vector>

using namespace std;

// functions from student.cpp:
int InputData(vector<int>& V);
double GetAverage(vector<int> V, int N);

int main()
{
// assume at most 100 inputs:
vector<int> inputs(100);
int N;
double avg;

N = InputData(inputs);
avg = GetAverage(inputs, N);

cout << "N: " << N << endl;
cout << "Avg: " << avg << endl;

for (int i = 0; i < N; ++i)
{
cout << inputs.at(i) << endl;
}

return 0;
}

student.cpp

int InputData(vector<int>& V)
{

// TODO
  
return 0;
}

//
// GetAverage
//
// Returns the average of the first N values in the vector; if N is 0 then
// 0.0 is returned.
//
double GetAverage(vector<int> V, int N)
{

// TODO

return 0.0;
}

Solutions

Expert Solution

Source Code in C++:

#include <iostream>
#include <vector>

using namespace std;

// functions from student.cpp:
int InputData(vector<int> &V);
double GetAverage(vector<int> V, int N);

int main()
{
// assume at most 100 inputs:
vector<int> inputs(100);
int N;
double avg;

N = InputData(inputs);
avg = GetAverage(inputs, N);

cout << "N: " << N << endl;
cout << "Avg: " << avg << endl;

for (int i = 0; i < N; ++i)
{
cout << inputs.at(i) << endl;
}

return 0;
}


int InputData(vector<int> &V)
{

int N=0;
int inp;
do
{
cin >> inp;
if(inp>=0)
{
V.at(N)=inp;
N++;
}
}while(inp>=0);
return N;
}

//
// GetAverage
//
// Returns the average of the first N values in the vector; if N is 0 then
// 0.0 is returned.
//
double GetAverage(vector<int> V, int N)
{
if(N==0)
return 0.0;
double sum=0;
for(int i=0;i<N;i++)
sum+=V[i];
return sum/N;
}

Output:


Related Solutions

Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program, falling.cpp, that inputs a distance in meters from the user and computes...
Write a C++ program, falling.cpp, that inputs a distance in meters from the user and computes the amount of time for the object falling from that distance to hit the ground and the velocity of the object just before impact. Air resistance is discounted (assumed to fall in a vacuum). To compute the time, use the formula: t = Square Root (2d / g) where d is the distance in meters and g is the acceleration due to gravity on...
c program Write a program that asks the user to enter a sequence of 15 integers,...
c program Write a program that asks the user to enter a sequence of 15 integers, each either being 0, 1, or 2, and then prints the number of times the user has entered a "2" immediately following a "1". Arrays are not allowed to appear in your code. Include ONLY THE SCREENSHOT OF YOUR CODE in an image file and submit the file.
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
Using a vector of integers that you define. Write a C++ program to run a menu...
Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 2. Write a function called getValidAge that allows a user to...
Directions: Using a vector of integers that you define. Write a C++ program to run a...
Directions: Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 1. Write a function called getValidAge that allows a user...
Write a C++ program that inputs three integers in the range [1..13] that represent a hand...
Write a C++ program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is...
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT