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?
#include <iostream>
#include <cmath>
#include <cstdlib>
usingnamespace std;
double arrayAverage(double array[], int size);
double calculateStandardDev(double array[], int size);
int main()
{
constint SIZE = 20;
constint 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);
}