Question: Write a function called compute_average that takes a vector as an argument, and returns the average of the values in the vector. I will provide

Write a function called compute_average that takes a vector as an argument, and returns the average of the values in the vector. I will provide a main() function that reads the values from standard input, calls your function, and outputs the result. You must provide a file called compute_average.h which contains the definition of the compute_average function, and one called compute_average.cpp which contains the actual implementation. Remember that

your compute_average.cpp should #include your compute_average.h

the header file should NOT contain a using statement

in your header file, all STL types must be prefaced with "std::"

your header file should guard against being #include'd twice.

compute_average.h

#ifndef COMPUTE_AVG_H #define COMPUTE_AVG_H #include "compute_average.h" #include #include #endif

compute_average.cpp

#include #include

#include"compute_average.h"

double compute_average(std::vector arr) { double sum=0; std::vector::iterator it; for(it=arr.begin();it!=arr.end();it++) { sum=sum+*it; } sum=sum/arr.size();

return sum; }

exercise_4.7.cpp

#include

#include

#include "compute_average.h"

using namespace std;

int main()

{

vector v;

double x;

while (cin >> x)

{

v.push_back(x);

}

cout << "Average : " << compute_average(v) << ' ';

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!