# Thread: Help with statistics level math within a function

1. ## Help with statistics level math within a function

1. Thank you in advance for any help offered.

2. While I realize math is extremely important to know for programming, I am a beginning student at my school after graduating high school some 15 years ago. I am starting from scratch in math so I will have a solid foundation for learning trig and calc. The C class I am in has elementary algebra as it's prerequisite, however the instructor keeps giving us problems with higher levels of math such as statistics.

3. My code works and is doing what I am asking of it, so my problem must lie in a misunderstanding of the problem itself. I have tried learning the standard deviation function myself but I must be missing a key piece because my answers do not match the online calculators.

4. My assignment is to generate some random numbers, acquire the standard deviation and the mean. My issue is that the standard deviation function is not returning the correct answer. I have tried inserting printf statements to see where the math goes wrong. However since this level of math is beyond my abilities there is little I can do with that information. It seems that the answer goes askew after the line

sum_of_i2 = pow(sum_of_i + i, 2);

the portion of code that needs attention in my opinion is:

sum_of_i2 = pow(sum_of_i + i, 2);
temp = sqrt((sum_of_x2) - (sum_of_i2));
std_dev = temp / SIZE * (SIZE - 1);

However the whole code is posted below if needed.

Code:
```////////////////////////////////////////////////////////
//Libraries
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

////////////////////////////////////////////////////////
//CONSTANTS
#define SIZE 20
#define WIDTH 5
#define LOW 0
#define HIGH 100

////////////////////////////////////////////////////////
//PROTOTYPES
void generate_array(int array[SIZE]);
double calculate_mean(int array[SIZE], double);
double calculate_stand_dev(int array[SIZE], double);
char get_order(char);
void organize_array(int array[SIZE]);
void display_array(int array[SIZE]);
void display_info(double, double);

////////////////////////////////////////////////////////
//Program
int main()
{
//Do-While loop exit flag
char exit;
//Do-While loop for continuous repetition
do
{
////////////////////////////////////////////////////////
//Program body
////////////////////////////////////////////////////////
int array[SIZE] = {0};
int j_flag = 0;
char order = ' ';
double mean = 0.0;
double std_dev = 0.0;
srand(time(NULL));

generate_array(array);
mean = calculate_mean(array, mean);
std_dev = calculate_stand_dev(array, std_dev);
display_array(array);
order = get_order(order);
organize_array(array);
display_array(array);
display_info(mean, std_dev);

///////////////////////////////////////////////////////
//Do-While loop exit condition and instructions
printf("\n\nWould you like to continue?\n\n");
printf("\nIf so press 'Y' and hit enter\n\n");
printf("\nOtherwise press 'N' to quit.\n\n");
fflush(stdin);
scanf("%c",&exit);
exit = toupper(exit);

while (exit != 'Y' && exit != 'N')
{
//Validation for exit condition(valid inputs are y/n)
printf("Invalid, try again\n");
fflush(stdin);
scanf("%c",&exit);
exit = toupper(exit);
}

}
while (exit == 'Y');

return 0;
}
////////////////////////////////////////////////////////
//NUMBER GENERATOR
void generate_array(int array[SIZE])
{
int i, j, num;
for (i = 0; i < SIZE; i ++)
{
num = rand() % (HIGH - LOW + 1) + LOW;
array[i] = num;
}
}
////////////////////////////////////////////////////////
//CALCULATE MEAN
double calculate_mean(int array[SIZE], double mean)
{
double sum = 0.0;
int i;
for(i = 0; i < SIZE; i ++)
{
sum = sum + array[i];
}
mean = sum / SIZE;
return mean;
}
////////////////////////////////////////////////////////
//CALCULATE STANDARD DEVIATION
double calculate_stand_dev(int array[SIZE], double std_dev)
{
double sum_of_x2 = 0.0;
double sum_of_i = 0.0;
double sum_of_i2 = 0.0;
double temp, num;
int i, j;

for(i = 0; i < SIZE; i ++)
{
num = array[i];
sum_of_x2 = sum_of_x2 + pow(num, 2);
sum_of_i = sum_of_i + i;
}
sum_of_i2 = pow(sum_of_i + i, 2);
temp = sqrt((sum_of_x2) - (sum_of_i2));
std_dev = temp / SIZE * (SIZE - 1);

return std_dev;
}
////////////////////////////////////////////////////////
//USER PREFERENCE OF DISPLAY ORDER
char get_order(char order)
{
printf("\nHow would you like your numbers displayed?");
printf("\n\nPress A for ascending order.");
printf("\nPress D for descending order.\n");
fflush(stdin);
scanf("%c",&order);
order = toupper(order);

while (order != 'A' && order != 'D')
{

printf("Invalid, try again\n");
fflush(stdin);
scanf("%c",&exit);
order = toupper(order);
}
return order;
}
////////////////////////////////////////////////////////
//ORGANIZATION OF DATA
void organize_array(int array[SIZE], char order)
{
int i,i2;

for(i=0; i<SIZE; i++)
{
for(i2=0; i2<SIZE-1; i2++)
{
if (order == 'D')
{
if(array[i2]<array[i2+1])
{
int temp = array[i2+1];
array[i2+1] = array[i2];
array[i2] = temp;
}
}
else
{
if(array[i2]>array[i2+1])
{
int temp = array[i2+1];
array[i2+1] = array[i2];
array[i2] = temp;
}
}
}
}
}
////////////////////////////////////////////////////////
//DISPLAY OF ARRAY
void display_array(int array[SIZE])
{
int i;
int Crlf = 0;
printf("\n");
printf("The test scores are as follows : \n");
for(i = 0; i < SIZE; i++)
{
printf("%d   ",array[i]);
Crlf ++;
if(Crlf >= WIDTH)
{
printf("\n");
Crlf = 0;
}
}
}
////////////////////////////////////////////////////////
//DISPLAY OF MEAN AND STANDARD DEVIATION
void display_info(double mean, double std_dev)
{
printf("\n\nThe standard deviation is: %.2f", std_dev);
printf("\n\nThe mean is: %.2f", mean);

}
/////////////////////////////////////////////////////////
//END OF PROGRAM```

2. Calculating standard deviation requires simplest algebra only. (square and square root)

Let me study your code a bit and see what's up.

I would use your first loop, to recalculate your mean from the data. (Or bring that number over as a parameter, since it was previously calculated in another function.)

Then in a second for loop, work with the mean average to calculate the squared difference from the mean, for each data point.

Finally, add the mean differences up, and divide by the number of data points you have, and then take the square root of that number.

Take each part, step by step, and it will be more clear what needs to be done.

The first example here, is what you want, but in C, the assignments work from right to left, instead of left to right, like they show here:
http://en.wikipedia.org/wiki/Standard_deviation

I really don't understand what sum_of_i is all about:
Code:
`sum_of_i = sum_of_i + i;`
Put a watch on that as you step through it, and see if it's really what you want here.

3. You need the mean in the function, you feed it std_dev which is zero in main.

Code:
```//CALCULATE STANDARD DEVIATION
double calculate_stand_dev(int array[SIZE], double std_dev)
{
double sum_of_x2 = 0.0;
double sum_of_i = 0.0;
double sum_of_i2 = 0.0;
double temp, num;
int i, j;

for(i = 0; i < SIZE; i ++)
{
num = array[i];
sum_of_x2 = sum_of_x2 + pow(num, 2);
sum_of_i = sum_of_i + i;
}
sum_of_i2 = pow(sum_of_i + i, 2);
temp = sqrt((sum_of_x2) - (sum_of_i2));
std_dev = temp / SIZE * (SIZE - 1);

return std_dev;
}```
Standard deviation - Wikipedia, the free encyclopedia

4. The way that you are explaining it seems more simple than the symbols and the verbal instructions the teacher gave me.

These symbols are why I say the math is beyond my abilities. The individual concepts are not a problem, it's properly deciphering this thing that is beyond me.

Code:
```                    ____________________________________
/    n         /     n      \ 2
/   ___         |   ___       |
S =   _          / N \   '  2   - |   \  '      |
\       /    /__.  X      |   /__. X    |
\     /      i-1         \   i-1  [i] /
\   /   --------------------------------------
\ /                n(n -1)
V```

what was explained to me in class was to:
1. add up the totals and then square them.
2. square each number individually and tally them up.
3. then take the two previos sums and obtain the difference.
4. then divide that difference by the ammount of numbers minus one.
5. and then finally get the square root of that product.

No offense to my teacher but English is not his first language and it makes one heck of a obstacle to overcome, so something may have been on me in translation.

5. fflush vs gets
Cprogramming.com FAQ > Why fflush(stdin) is wrong

Edit:
Code:
```	mean = calculate_mean(array, mean);
std_dev = calculate_stand_dev(array, std_dev);```
What's the purpose of 2nd parameter?!

6. If you look at the link I provided it takes you straight to the formula you need to use. When I look at your function there seem to be more than it needs to really.

You calculate the mean in main, then take mean and give that as an argument to calcualte_stand_dev(). You have the array with the values, and the mean. Inside the function all you really need is std_dev, i and a sum variable. You have the loop in place already.

Code:
```//CALCULATE STANDARD DEVIATION
double calculate_stand_dev(int array[SIZE], double mean)
{
double std_dev = 0;
int i, sum = 0;

// loop through the array and sum (values - mean) ^ 2

// calculate std_dev by square root of ( sum / SIZE )
}```

7. subsonics ty for your help

Im not sure y my teacher has n(n-1) in his equation as none of the formulas on that wiki look like what he gave us. But now that I understand the equation properly it was as simple as you said...ty

Code:
```////////////////////////////////////////////////////////
//CALCULATE STANDARD DEVIATION
double calculate_stand_dev(int array[SIZE], double mean)
{

double std_dev = 0.0;
double sum = 0.0;
double num = 0.0;
int i;

printf("\n\n");
for(i = 0; i < SIZE; i ++)
{
num = array[i];
sum = sum + pow((num - mean), 2);
}
std_dev = sum / SIZE;
std_dev = sqrt(std_dev);

return std_dev;
}```

8. The sigma symbol is just a shorthand way of representing a sum of numbers, and there are simple and obvious connections between it and 'for' loops. Suppose you have a bunch of numbers that you want to add up; let's call them x_0, x_1, x_2, etc, up to x_(n-1) so that you have n different numbers. Then the symbol

Code:
```        n
___
sum = \   ' x_i
/___.
i=0```
means, pretty much, "Go from i=0 to i=n-1, and add up all the x_i's, and that's your sum"

It might make more sense to you if you think of it in terms of for loops and arrays; the following function is equivalent:

Code:
```double Sigma( int n, double x[] ) {
int i;
double sum=0;
for( i=0; i<n ;i++ ) {
sum += x[i];
}
return sum;
}```
The function goes from i=0, up to n-1, and adds up all the x[i]'s, which is exactly what the sigma symbol means.

9. ya that does make sense actually. My teachers verbal english is difficult at best. The introduction of those symbols followed by a fast explanation was not the best way to learn it.

I appreciate all the help provided, I was able to implement it in my program and I learned something new today ty all