Thread: Problem in output of a function

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    29

    Question 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;
    printf("please enter a %d*%d array\n",i,j);
    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    May 2008
    Posts
    29
    thanks, the loop was there in mistake...
    it still doesn't solve the problem
    i already divided by i*j when i calculated avg2...

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So is this like the 4th topic about this program?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It is getting rather annoying.
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    29
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Livnat View Post
    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. #9
    Registered User
    Join Date
    May 2008
    Posts
    29
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Livnat View Post
    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. #11
    Registered User
    Join Date
    May 2008
    Posts
    29
    Thanks tabstop! That was the problem...
    no more topics about this one...

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    29
    Quote Originally Posted by Salem View Post
    It is getting rather annoying.
    Hello Salem,
    I just saw you gave me a new way to debug in the previous topic about this non ending assignment... You're right it's smarter to have all the different problems in the same topic.
    I appreciate your help...
    -L

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM