# Thread: Help with a function that suppose to normalize an array

1. ## Help with a function that suppose to normalize an array

Hello,
This function suppose to make the mean value zero and the Standard deviation 1,
Only it makes them both zero.
Somebody has an idea why?
Thanks!

Code:
```void norm_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);
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++)
sum+=pow(b[m][n],2);
avg2=sum/(i*j);

for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]/sqrt(avg2);

}```

2. Do you know what sum is supposed to be at various stages?

Test it with a known set of data, then run the code with the debugger and examine the program state as it runs. When reality != expectation, you've found a bug.

Without a debugger, try putting printf statements at key points to print key values.

3. You do know that array elements are sequential (regardless of dimensions too), yes?

In this case, rows and columns hold no bearing. Thus it can be written as,

Code:
```void norm_b(double * b, size_t n)
{
/* ... */
size_t i = 0;

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

/* ... */
return;
}```
Which is far easier to read. You're also leaving a huge chunk of the problem out, how do you know that the StDev and mean are 0? You don't print them or anything. Unless you used a debugger... even then...

Also, you might want to work on your indenting

4. Thanks Salem for the advise. I printed the first sum and its zero no matter what array I input.
I don't understand why...

Hi zacs7 I'm bit afraid of pointers in this point so I prefer doing it the old two dimensional way...
Any ways this is the full code, I left it out because it's long...

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("&#37;d%d" , &k, &l);
scan_b(c ,k,l);
print_b (c,k,l);
norm_b( c ,  k,  l);
printf ("the Standard deviation is: %lf\n" ,std_b( c , k,  l));
printf ("the mean value is: %lf\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("%d" , &(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("%d" , b[m][n]);
}
printf("\n");
}

void norm_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]);
printf("sum=%lf",sum);

avg=sum/(i*j);
printf("avg=%lf",avg);
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++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%d" , b[m][n]);
}
printf("\n");

for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
sum+=pow(b[m][n],2);
printf("sum2=%lf",sum);
avg2=sum/(i*j);
printf("avg2=%lf",avg);
for (m=0 ; m<i ; m++)
for (n=0 ; n<j ; n++)
b[m][n]=b[m][n]/sqrt(avg2);
for (m=0 ; m<i ; m++)
{
printf("\n");
for (n=0 ; n<j ; n++)
printf("%d" , b[m][n]);
}
printf("\n");

}

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;
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]-avg),2);

std=sqrt(sum2/(i*j));
return std;

}```

5. you should reread the format specifiers for printf and scanf

&#37;lf used for doubles in scanf, in printf %f should be used

%d used for integers - using it for doubles can cause different errors including crashes

6. Thanks that helped!
The Standard deviation is still not 1... I'll continue working on it later

7. If you're using gcc, then use
gcc -W -Wall -ansi -pedantic -O2 prog.c

Fix anything it complains about, including printf/scanf format errors.