C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-13-2008, 11:29 PM   #1
Registered User
 
Join Date: May 2008
Posts: 29
Question 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);

}
Livnat is offline   Reply With Quote
Old 07-13-2008, 11:44 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-13-2008, 11:46 PM   #3
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 07-14-2008, 12:40 AM   #4
Registered User
 
Join Date: May 2008
Posts: 29
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("%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;
printf("please enter a %d*%d array\n",i,j);
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;

}
Livnat is offline   Reply With Quote
Old 07-14-2008, 12:46 AM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
you should reread the format specifiers for printf and scanf

%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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 07-14-2008, 12:57 AM   #6
Registered User
 
Join Date: May 2008
Posts: 29
Thanks that helped!
The Standard deviation is still not 1... I'll continue working on it later
Livnat is offline   Reply With Quote
Old 07-14-2008, 10:51 AM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Tags
array, function, standard deviation

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Array Allocation function P4R4N01D C++ Programming 6 05-15-2009 02:04 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
Passing my array to function pooty tang C Programming 8 09-15-2004 12:19 PM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM
I need help with passing pointers in function calls vien_mti C Programming 3 04-24-2002 10:00 AM


All times are GMT -6. The time now is 01:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22