Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

What performance issues are there in the company?

Answered: 1 week ago