Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

Im trying to find the standard deviation from a structure of numbers.

the array is in the format:

s[x].sid= 301111;

s[x].mark= 50;

s[x].grade= 'A';

were x is incremented for every user.

heres the code for the standard deviation, its in the form of a function initalised using:

float stdDev(struct student s[], int n);

and called using:

stdDev(staff,ARR_SIZE);

heres the function... it just crashes! theres 5 entrys, which should be passed to the function in n

float stdDev(struct student s[], int n)

{

int x;

float mean, sigma;

mean = s[x].sid / n;

for (x=0; x<5; x++) n += (mean - s[x].mark)*(mean-s[x].mark);

sigma = sqrt(n/5);

printf("Std Deviation is %f",sigma);

return sigma;

the array is in the format:

s[x].sid= 301111;

s[x].mark= 50;

s[x].grade= 'A';

were x is incremented for every user.

heres the code for the standard deviation, its in the form of a function initalised using:

float stdDev(struct student s[], int n);

and called using:

stdDev(staff,ARR_SIZE);

heres the function... it just crashes! theres 5 entrys, which should be passed to the function in n

float stdDev(struct student s[], int n)

{

int x;

float mean, sigma;

mean = s[x].sid / n;

for (x=0; x<5; x++) n += (mean - s[x].mark)*(mean-s[x].mark

sigma = sqrt(n/5);

printf("Std Deviation is %f",sigma);

return sigma;

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for (x=0; x<5; x++)

to:

for (x=0; x<n; x++)

as 'n' is the size of the array!

if you send your email to madlan@yahoo.com i will reply with the whole code,

if that would be any easier? or is there a better way to work out the Standard Deviation!

float stdDev(struct student s[], int n);

*/function called with:*/

stdDev(staff,ARR_SIZE);

*/Function:*/

float stdDev(struct student s[], int n)

{

int x = 0;

float mean, sigma;

mean = s[x].sid / n;

for (x=0; x<n; x++) n += (mean - s[x].mark)*(mean-s[x].mark

sigma = sqrt(n/5);

printf("Std Deviation is %f",sigma);

Change to the following:

float mean_mark(struct student s[], int n)

{

int i;

int acc = 0;

for( i=0; i<n; i++ )

{

acc += s[i].mark;

}

return (float)acc / (float)n;

}

float stdDev(struct student s[], int n)

{

int x;

float mean, sigma, sum = 0.0f;

mean = mean_mark( s, n );

for (x=0; x<n; x++)

{

sum += pow(mean - s[x].mark, 2.0);

}

sigma = sqrtf(sum/(n -1));

std::cout << "Std Deviation is %f" << sigma;

return sigma;

}

And, btw, you voilated your agreement by posting your email address!

Good luck!

The code above is pure C. pow is included in the standard math.h

So the code is working for you now?

So the code is working for you now?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial>>>The code above is pure C

???!!!

replace that with a printf() and replace

pow (x, 2.0)

with

(x * x)

C

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

mean = s[x].sid / n;

but x has not been initialized!

Try to change:

int x;

to:

int x = 0;