### Need help with standard deviation function. Something seems to be wrong with the SD value once I run the program

#include<iostream>
#include<iomanip>
#include <cmath>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<conio.h>

double Mean (int [], int, double &, double &);
double standardDeviation (int [], int,double &, double &, double &, double &);
int findMedian (int [ ] , int, double&);
void Sort(int * , int);

int main()
{
int ar[100],n,i,min=0,max=0; //Initializing variables
double avg=0.0,median=0.0, sum=0.0, sum2=0.0, sD=0.0;
printf("How many numbers do you want to use? "); //Gets the size of the array
scanf("%d",&n);

printf("\nPlease enter each number and press the enter key. \n"); //Gets the contents of the array
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
min=ar[0]; //Traverses through the array and finds the smallest number
for(i=0;i<n;i++)
{
if(ar[i]<=min)
{
min=ar[i];
}
}
max=ar[0]; //Traverses through the array and finds the largest number
for(i=0;i<n;i++)
{
if(ar[i]>=max)
{
max=ar[i];
}
}

printf("\n\t The smallest number is: %d",min);
printf("\n\t The largest number is: %d",max);

Mean(ar, n, sum, avg);
printf("\n\t The mean is: %f",avg);

sD = standardDeviation(ar,n,sum,sum2,avg,sD);
printf("\n\t The standard deviation is: %d",sD);

findMedian(ar, n, median);
printf("\n\t The median of your unsorted list is: %f",median);

Sort(ar, n);
printf("\n\t The numbers listed in ascending order are: ");
for(i=0;i<n;i++)
{
printf("%d", ar[i]);
printf(" ");
}
getch();
}

double Mean (int ar[], int n, double &sum, double &avg)
{
for(int i=0;i<n;i++) //Finds the sum of the array
{
sum= sum+ ar[i];
avg= sum/ float(n);
}
return avg;
} // Function for mean

double standardDeviation (int ar[], int n, double &sum, double &sum2, double &avg, double &sD)
{
for(int i=0;i<n;i++) //Finds the sum of the array
{
sum= sum+ ar[i];
}
avg= sum/ float(n);

for ( int i = 0; i <=n; i++ )
{
sum2 += pow(ar[i]-avg,2);
}
sD= sqrt((sum2/n));
return sD;
}

int findMedian(int ar[ ] , int n, double &median)
{
if (n%2==1)
{ /* if there is an odd number of elements I want n/2 - 1 */
median = ar[n/2];
return median;
}
else
median = ( ar[n/2] + ar[n/2 - 1] ) / 2;
return median; /* otherwise I want the average of n/2 -1 and n/2*/
}

void Sort(int *array, int n)
{
bool swapped = true;
int j = 0;
int tmp;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (array[i] > array[i + 1])
{
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
}
A couple things. sum is not 0 when standardDeviation is entered.

The valid indices for an array of n elements is 0 to n-1, so the second for loop in standardDeviation accesses memory it shouldn't.

In `printf("\n\t The standard deviation is: %d",sD);` %d is not a correct format specifier for a variable of type double. Why are you using C input and output anyway?
I was under the assumption that I was doing things the correct way? I'm a little rusty on programming I haven't done it since undergraduate. How can I resolve the issue?

I changed my code and now i don't have sum initialized as 0.0, I also changed the specifier to %lf, should I change it from C?
Last my for loop now reads:

for ( int i = 0; i <=n; i++ )
{
sum2 += pow(ar[i]-avg,2);
}
sD= sqrt(sum2/(n-1));
return sD;

I am getting a new value that looks a lot nicer now however it is still wrong, how can I resolve the issue?
Put your code inside code tags, post it together with your input and results.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #include #include #include #include #include #include using namespace std; //Prototypes double Mean (int [], int, double &, double &); double standardDeviation (int [], int, double &, double &, double &, double &); int findMedian (int [ ] , int, double&); void Sort(int * , int); int main() //Main Function { int ar[100],n,i,min=0,max=0; //Initializing variables double avg=0.0,median=0.0, sum, sum2, sD; printf( "How many numbers do you want to use? "); //Gets the size of the array scanf("%d",&n); printf("\nPlease enter each number and press the enter key. \n"); //Gets the contents of the array for(i=0;i=max) { max=ar[i]; } } printf("\n\t The smallest number is: %d",min); printf("\n\t The largest number is: %d",max); Mean(ar, n, sum, avg); printf("\n\t The mean is: %lf",avg); sD = standardDeviation(ar,n,sum,sum2,avg,sD); printf("\n\t The standard deviation is: %lf",sD); findMedian(ar, n, median); printf("\n\t The median of your unsorted list is: %lf",median); Sort(ar, n); printf("\n\t The numbers listed in ascending order are: "); for(i=0;i array[i + 1]) { tmp = array[i]; array[i] = array[i + 1]; array[i + 1] = tmp; swapped = true; } } } }``````

Input/Output:
How manynumbers do you want to use? 5

Please enter each number and press the enter key.
3
4
5
1
2

The smallest number is: 1
The largest number is: 5
The mean is: 3.000000
The standard deviation is: 4.769696
The median of your unsorted list is: 5.000000
The numbers listed in ascending order are: 1 2 3 4 5

The answer for standard deviation should be 1.5811

 I changed my code and now i don't have sum initialized as 0.0

I wasn't suggesting that you shouldn't initialize it, in fact they must be initialized if you expect the other functions to continue working, the way you have it designed.

I was suggesting that if sum isn't 0 (and it isn't) when you enter standardDeviation and you don't set it to 0, then the calculation will be wrong (and it is.) (And you're still accessing memory you shouldn't in the second for loop.)

The use of all these references is silly. None of what you're passing as references now should be passed as references.

For instance, it would make a lot more sense to write mean as:

 ``12345678`` ``````double mean(int array[], unsigned n) { int sum = 0 ; for ( unsigned i=0; i(n) ; }``````
Is there someone that can help me to figure out how to write the standard deviation function correctly? @Cire you said that I am still accessing memory I shouldn't in the second for loop,How can I resolve this? What should I look to change?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128`` ``````#include #include #include #include #include #include #include using namespace std; //Prototypes double Mean (int [], unsigned); double standardDeviation (int [], unsigned); int findMedian (int [ ] , unsigned); void Sort(int * , unsigned); //Initializing variables int ar[100],n,i; double avg=0.0,median=0.0, sum, sum2, sD; int main() //Main Function { int min=0,max=0; cout<<"How many numbers do you want to use? "; //Gets the size of the array cin>>n; cout<>ar[i]; } min=ar[0]; //Traverses through the array and finds the smallest number for(i=0;i=max) { max=ar[i]; } } cout<(n) ; } double standardDeviation (int ar[], unsigned n) { for(int i=0;i(sD); } int findMedian(int ar[ ] , unsigned n) { if (n%2==1) { // if there is an odd number of elements I want n/2 median = ar[n/2]; return median; } else median = ( ar[n/2] + ar[n/2 - 1] ) / 2; return static_cast(median); //otherwise I want the average of n/2 -1 and n/2 } void Sort(int *array, unsigned n) { //Sorts the array in ascending order bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < n - j; i++) { if (array[i] > array[i + 1]) { tmp = array[i]; array[i] = array[i + 1]; array[i + 1] = tmp; swapped = true; } } } }``````

Input/Output:
How manynumbers do you want to use? 5

Please enter each number and press the enter key.
3
4
5
1
2

The smallest number is: 1
The largest number is: 5
The mean is: 3.000000
The standard deviation is: 2.17945
The median of your unsorted list is: 5.000000
The numbers listed in ascending order are: 1 2 3 4 5

The answer for standard deviation should be 1.5811

Hello.

The code looks nice, just a few minor bugs here and there. I calculated the standard deviation of your numbers and the answer I got is sqrt(2). I will post my solution here and then I will check an on-line standard deviation calculator to see if my answer is right.

Nvm it works now:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````#include #include #include using namespace std; //Prototypes double Mean (int [], int); double standardDeviation (int [], int, double); int main() //Main Function { int n; double avg = 0; double median = 0; double median2 = 0; double sD; int min=0; int max = 0; cout<<"How many numbers do you want to use? "; //Gets the size of the array cin>>n; int *ar = new int[n]; int tempAr[n]; cout<>ar[i]; tempAr[i] = ar[i]; //a temp array } sort (tempAr, tempAr+n);//Sort into ascending order min=tempAr[0]; //Traverses through the array and finds the smallest number max=tempAr[n -1]; //Traverses through the array and finds the largest number cout<<"\tThe smallest number is: "<

 ```\$ ./File How many numbers do you want to use? 5 Please enter each number and press the enter key. 3 4 5 2 1 The smallest number is: 1 The largest number is: 5 The mean is: 3 The standard deviation is: 1.58114 The median of your unsorted list is: 5 The median of your sorted list is: 3 The numbers listed in ascending order are: 1 2 3 4 5```
Last edited on
Thank you so much Smac89!!!
Topic archived. No new replies allowed.