# Solving Standard Deviation with Arrays

• 11-02-2012
Trw2011
Solving Standard Deviation with Arrays
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; }```
• 11-02-2012
Salem
Please paste as text in future.

Copying this from your IDE with it's own colouring rules doesn't work well.

[ FONT=Consolas][ SIZE=2][ COLOR=#0000ff][ FONT=Consolas][ SIZE=2][ COLOR=#0000ff][ FONT=Consolas][ SIZE=2][ COLOR=#0000ff]#include[ /COLOR][ /SIZE][ /FONT][ /COLOR][ /SIZE][ /FONT][ /COLOR][ /SIZE][ /FONT][ FONT=Consolas][ SIZE=2][ COLOR=#a31515][ FONT=Consolas][ SIZE=2][ COLOR=#a31515][ FONT=Consolas][ SIZE=2][ COLOR=#a31515]<iostream>
• 11-02-2012
Salem
> The problem I keep on getting is that my function stdDev not all control paths return a value.
So why is the return statement INSIDE the for loop?
• 11-02-2012
Trw2011
Sorry that made me look stupid. I'm sorry for the colored text as well. Now when I try to start without debugging, I keep getting an errors LNK2019 and LNK1120, which usually means I made a easy mistake somewhere again, but I can't seem to find it.
• 11-02-2012
Salem