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 andGetAverage. 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

need to change the code for desired functionality of program.

if you don't want to change main program then just copy the 2 function added at last in the code.

here is edited code:

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() {
   // declare variables
   vector<int> inputs;
   int N;
   double avg;

   // get input from user
   cout << "Enter numbers to calculate Average :(enter negative number to stop entering)" << endl;
   int input; // variable input to store input number from use
   // while loop to get input numbers
   while (true) {
       cin >> input; // get user input
       if (input < 0) {
           // negative number act as sentinel value
           break; // break the while loop and calculate average
       }
       else {
           // add number to vactor
           inputs.push_back(input);
       }
   }

   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){
   return V.size();
}

// 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;
   }
   else {
       int sum = 0; //sum of total numbers counted
       // loop for each number to count average
       for (int i = 0; i < N; i++) {
           sum = sum + V.at(i); // add each number to the sum
       }
       // calculate average
       double avg = sum / (N * 1.0); //multiply by 1.0 to get double division insted of integer division
       return avg;
   }
}


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...
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...
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
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values. Use else-if selection and comparative operators such as '<=' or '>=' to evaluate the number that is the smallest value. If one or more values are the same and the lowest value your program should be able to report the lowest value correctly. Don't forget to first scanf in the users input. Ex: If the input is: 7 15 3 the output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT