Question: In c++ please. Develop a program that repeatedly asks the user to provide a list of non-negative integers and calculate the statistical metrics of them.
In c++ please.
Develop a program that repeatedly asks the user to provide a list of non-negative integers and calculate the statistical metrics of them.
main.cpp
The main program. Include a main function and multiple other statistical functions.
stats.hpp/stats.cpp
This pair of files holds the two statistical functions.
test.cpp
Provided file to run the tests
makefile
Provided makefile to allow make automation
Sample run
Welcome to the statistics app! Please input a list of up to 10 non-negative integers. The inputs should be separated by spaces and ended with a -1 value. The last -1 will not be included in the calculation. Please input here: 1 2 7 10 12 -1 You have input 5 values. The sum is 32 The average is 6.4
Requirement
You must use an array to store the data first. The array should have a size 10 which is enough. The actual size should be counted and it should be less and equal than 10. This is a typical partially filled array.
Implement a function called sum to calculate the sum. It should take an array as its parameter with the actual size and return the sum as an int value.
Implement a function called average to calculate the average. It should take an array as its parameter with the actual size and return the average as a double value for more precision. You can choose to call the sum function as needed.
Your main function should handle the user interaction and print the result. Calculations should be delegated to the statistical functions. Consider a for loop with early exit to get the user inputs.
Provided files
**makefile**
SHELL = /bin/bash
main: main.o stats.o
g++ -std=c++14 -o $@ $^
test: test.o stats.o
g++ -std=c++14 -o $@ $^
./test
test-run: main
echo -e "1 2 7 10 12 -1 " | ./main
%.o: %.cpp
g++ -std=c++14 -c -o $@ $<
clean:
$(RM) *.o main test
**test.cpp**
#include "stats.hpp" #include
using namespace std;
bool approx(double v1, double v2, double epsilon) { return abs(v1 - v2) < epsilon; }
void testSum() { cout << "Start testing sum function..." << endl; int vals[] = {1, 2, 3, 4, 5}; assert(sum(vals, 4) == 10); assert(sum(vals, 5) == 15); cout << "Tests all pass" << endl; }
void testAvg() { cout << "Start testing average function..." << endl; int vals[] = {1, 2, 3, 4, 5}; assert(approx(average(vals, 4), 2.5, 0.001)); assert(approx(average(vals, 5), 3.0, 0.001)); cout << "Tests all pass" << endl; }
int main() { testSum(); testAvg(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
