Partially Filled Arrays and Calling a function within a function

Write your question here.

I have this assignment for class where I am supposed to develop a program to calculate standard deviation of an array and calculate the average of the array. That part was easy enough.

Now, I am having issues with trying to find a way to stop entering values into the array when the user does not want to enter 20 values. The size of the array has to be 20 as dictated by the assignment, but the user can enter only 5 values and call it a day. How do I tell my program that 5 values is enough?

Second question how do I call the average function that I made into the standard deviation function that I made as well?

here is what i have so far...


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

double arrayAverage(double array[], int size);
double calculateStandardDev(double array[], int size);

int main()
{
	const int SIZE = 20;
	const int SENTINEL = 0;
	double array[SIZE];

	cout << "Enter array elements (Enter the value '0' when you wish to stop entering values): ";
	

	for (int i = 0; i < SIZE; ++i)
		cin >> array[i];
	

	cout << "Standard Deviation = " << calculateStandardDev(array, SIZE) << "\n";
	cout << "Average of Array = " << arrayAverage(array, SIZE) << "\n";
}

// arrayAverage:	Calculates the average number of the array.
// Inputs:
// Outputs:
// Notes:

double arrayAverage(double array[], int size)
{
	double average = 0;
	for (int i = 0; i < size; i++)
	{
		average += array[i];
	}

	return average / size;
}


// calculateStandardDev:	Calculates the standard deviation of the array.
// Inputs:
// Outputs:
// Notes:

double calculateStandardDev(double array[], int size)
{
	double sum = 0, mean, standardDeviation = 0;

	for (int i = 0; i < size; ++i)
	{
		sum += array[i];
	}

	mean = sum / size;

	for (int i = 0; i < size; ++i)
		standardDeviation += pow(array[i] - mean, 2);

	return sqrt(standardDeviation / size);
}
Last edited on
Topic archived. No new replies allowed.