# Thread: Problem in output of a function

1. ## Problem in output of a function

Hi,
I try to make this code that normalizes an array.when I try to print the standard deviation it comes out like this -1.#ID... and so on...
If somebody finds the reason for it I'll appreciate the advise...
Code:
```#include <stdio.h>
#include <math.h>

void scan_b(double b[30][30]  , int , int );
void print_b(double b[30][30] , int  , int );
void norm_b(double  b[30][30], int , int );
double std_b(double b[30][30], int , int );
double avg_b(double b[30][30], int , int );

int main()
{
int k,l;
double c[30][30];
printf("please enter the number of rows and columns\n");
scanf("%d%d" , &k, &l);
scan_b(c ,k,l);
printf("The original array is:");
print_b (c,k,l);
norm_b( c ,  k,  l);
printf("The normalized array is:");
print_b (c,k,l);
printf ("the Standard deviation is: %f\n" ,std_b( c , k,  l));
printf ("the mean value is: %f\n" ,avg_b( c ,  k,  l));
system ("PAUSE");
return 0;
}

void scan_b(double b[30][30] , int i, int j)
{
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
scanf("%lf" , &(b[m][n]));
}

void print_b(double b[30][30] , int i, int j)
{
int m,n;

for (m=0 ; m<i ; m++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%f " , b[m][n]);
}
printf("\n");
}

void norm_b(double b[30][30] , int i, int j)
{
double sum=0,sum2=0,avg, avg2;
int m,n;

for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=(b[m][n]);
avg=sum/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]-avg;

for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum2+=pow(b[m][n],2);
avg2=sum2/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]/sqrt(avg2);
}

double avg_b(double b[30][30], int i, int j)
{
double sum=0,avg, avg2;
int m,n;

for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=b[m][n];

avg=sum/(i*j);
return avg;
}

double std_b(double b[30][30] , int i, int j)
{
double sum2,sum,std,avg,avg2,var;
int m,n;
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=b[m][n];
avg=sum/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum2+=pow(b[m][n],2);
avg2=sum2/(i*j);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
var=avg2-pow(avg,2);
std=sqrt(var);
return std;

}```

2. Because you have an ... interesting ... way of computing standard deviations. You should have
Code:
`var = avg2/(i*j)-pow(avg,2);`
It also shouldn't be inside a doubly-nested for loop.

You also need to initialize sum to 0 before you start adding things to it.

3. thanks, the loop was there in mistake...
it still doesn't solve the problem
i already divided by i*j when i calculated avg2...

5. I don't see where the standard deviation is actually being returned to the caller (or otherwise used after it's calculated). That is probably part of the problem.

Also, learn how to delegate. You labored on certain functions, you should know where you can use them.

Code:
```#include <math.h>

double avg_b (double b[30][30], int i, int j)
{
int m, n;
double sum = 0.0;

for (m = 0; m<i; m++) {
for (n = 0; n<j; n++) {
sum += b[m][n];
}
}
return sum / (double)(i * j);
}

double norm_b(double b[30][30], int i, int j)
{
double calcbuf[30][30] = {0.0,};
double avg = 0.0;

int m,n;

avg = avg_b(b, i, j);

for (m = 0; m<i; m++) {
for (n = 0; n<j; n++) {
calcbuf[m][n] = b[m][n] - avg;
calcbuf[m][n] *= calcbuf[m][n]; /***squaring***/
}
}

return sqrt(avg_b(calcbuf, i, j));
}```

6. It is getting rather annoying.

7. I printed the standard deviation in the main function

Code:
`printf ("the Standard deviation is: &#37;f\n" ,std_b( c , k,  l));`
It already worked before i accidentally deleted something and I can't find what...

8. Originally Posted by Livnat
thanks, the loop was there in mistake...
it still doesn't solve the problem
i already divided by i*j when i calculated avg2...
I am aware of that. Since you need to divide by it twice, that means you're one short.

9. why do I need to divide it twice?
var = <x^2>-<x>^2
std=sqrt(var)
<x^2> is avg2...

anyways that's not the problem. I have something wrong going on with the programming it doesn't even print a number....

10. Originally Posted by Livnat
why do I need to divide it twice?
var = <x^2>-<x>^2
std=sqrt(var)
<x^2> is avg2...

anyways that's not the problem. I have something wrong going on with the programming it doesn't even print a number....
Yes, you're right there, I'm misreading my formula. So that works. You do still need to initialize sum and sum2 to 0 before you start adding things to them.

11. Thanks tabstop! That was the problem...