Structs and Functions, calculating min, max, average and proving true

Hello,
I am having great trouble understanding exactly what to do with the assignment below. I have created some sort of code, based on my assumptions that I will basically be sorting the values assigned to the vector, and if I do that correctly, the function will return true:

Define a struct called SummaryStatistics that has the following members: double min, double max, double average. Define a function SummaryStatistics calculateStats( vector<double> data) that takes a vector of doubles and returns a SummaryStatistics struct with its data members assigned to the corresponding values.

Test and verify this function yourself before uploading. This assignment will be unit tested meaning that your code, not its output will be tested directly.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example:

The following functions, when called, should return true;

bool sanityCheck1() {
vector <double> v = {2, 8, 5, 5, 6, 10, 9, 8, 4, 5};
/*
min = 2
max = 10
avg = 6.2
*/
SummaryStatistics ss = calculateStats(v);
return (ss.min == 2 && ss.max==10 && ss.average == 6.2);
}

bool sanityCheck2() {
vector <double> v = {667, 159, 722, 550, 275, 914, 914, 237, 44, 835};
/*
Test {667, 159, 722, 550, 275, 914, 914, 237, 44, 835}
min = 44
max = 914
avg = 531.7
*/
SummaryStatistics ss = calculateStats(v);
return (ss.min == 44 && ss.max == 914 && ss.average == 531.7);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>

struct SummaryStatistics {
	double min;
	double max;
	double average;
};

bool SummaryStatistics calculateStats(vector<double> data) {
	int i, j, n, temp;												// holding temporary variable
	n = 10;															//number of elements in array

	for (i = 1; i < n; i++)
	{
		for (j = 0; j < (n - 1); j++)
		{
			if (data[j + 1] < data[j])				// sorts in ascending order
			{
				temp = data[j];								// switches elements in array
				data[j] = data[j + 1];
				data[j + 1] = temp;
			}
		}
	}
	max = data[9];
	min = data[0];

	average = 0.0;
	for (i = 0; i < n; i++)											//average
	{
		average = average + (double)data[i];						//casting to double and adding all values within array
	}
	average = average / (double)n;
}

int main() {
	double input;
	vector<double> data;
	cout << "Enter 10 numbers, and this program will calculate the max, min, and average of the numbers; if calculated correctly, the function will return true: " << endl:
	while (cin >> input)
		data.push_back(input);

	calculateStats(input);

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.