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

  1. #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);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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. #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("&#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;
    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;
    
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread