So I have to solve the standard deviation from a array of numbers inputted by the user. The program must also print out the array for the user to see. The user will be allowed to input numbers untill there is 100 values or they input a negative number. The problem I keep on getting is that my function stdDev not all control paths return a value. I believe the problem is that i could equal zero which would make the average divide by zero so it wouldn't pass through. I'm not sure on how to correct this problem, as there could be something else wrong with my code. I appreciate the help beforehand.
Code:
#include<iostream>
#include<cmath>
usingnamespace std;
void printarray(double[], int);
double stdDev(double[], int);
double average(double[], int);
constint MAXSIZE = 100;
int main()
{
double array1[100];
int i = 0;
double somenumber;
double S;
do {
cout <<
"Please enter a number to enter into the array or a negative number to stop."
<< endl;
cin >> somenumber;
if (somenumber >= 0) {
array1[i] = somenumber;
i++;
}
} while (somenumber >= 0 && i <= MAXSIZE);
cout << "There were " << i << " numbers entered into the array." << endl;
printarray(array1, i);
S = stdDev(array1, i);
cout << "The Standard Deviation for your array is " << S << " ." << endl;
return 0;
}
void printarray(double numbarray[], int size)
{
for (int j = 0; j < size; j++) {
cout << "Your array will be printed out below." << endl;
cout << numbarray[j] << endl;
}
}
double stdDev(double s[], int size)
{
double sum = 0;
double avg = average(s, size);
for (int l = 0; l < size; l++) {
sum += pow(s[l] - avg, 2);
sqrt(sum / size);
return sqrt(sum / size);
}
}
double average(double s[], int size)
{
double sum = 0;
for (int i = 0; i < size; i++) {
sum += s[i];
}
return sum / size;
}