In: Computer Science
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average).
Use the following main function in driver1b.cpp as a starting point for testing your function.
int main() {
int min = 0;
int max = 0;
double avg = 0.0;
vector<int> test1{ 12, 15, 543, 1024 };
compileStats(test1, min, max, avg);
cout << "For test 1: " << min << " " << max << " " << avg << endl;
vector<int> test2{ -8 };
compileStats(test2, min, max, avg);
cout << "For test 2: " << min << " " << max << " " << avg << endl;
vector<int> test3{ -40, 39, -39, 40, 38, -38, 0, 0 };
compileStats(test3, min, max, avg);
cout << "For test 3: " << min << " " << max << " " << avg << endl;
}
// You may not use any “built-in” C++ classes other than iostream and vector.
/*
* C++ Program to find out min max average
*/
#include <iostream>
#include <vector>
using namespace std;
void compileStats(vector<int> vec, int &min, int &max, double &avg);
int main()
{
int min = 0;
int max = 0;
double avg = 0.0;
vector<int> test1{12, 15, 543, 1024};
compileStats(test1, min, max, avg);
cout << "For test 1: " << min << " " << max << " " << avg << endl;
vector<int> test2{-8};
compileStats(test2, min, max, avg);
cout << "For test 2: " << min << " " << max << " " << avg << endl;
vector<int> test3{-40, 39, -39, 40, 38, -38, 0, 0};
compileStats(test3, min, max, avg);
cout << "For test 3: " << min << " " << max << " " << avg << endl;
return 0;
}
void compileStats(vector<int> vec, int &min, int &max, double &avg)
{
int sum = 0;
min = vec[0];
max = vec[0];
for (int i = 0; i < vec.size(); i++)
{
if (min > vec[i])
min = vec[i];
if (max < vec[i])
max = vec[i];
sum += vec[i];
}
avg = (double)sum/vec.size();
}
/* Program ends here */