need help on finding what i missed!

This is a discussion on need help on finding what i missed! within the C Programming forums, part of the General Programming Boards category; hello all, i made a simple function , its pretty simple, but i dont know why a specific if block ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    need help on finding what i missed!

    hello all, i made a simple function , its pretty simple, but i dont know why a specific if block doesnt execute !
    ( the one which checks whether power is zero or not )
    here is the code
    and below is the screen shot i got from a sample input , i also mentioned what i expected to get instead of that

    Code:
    void Integral( double multipicant,double power,char sign[],int size,int operand_number)
    {
    
    
            static int i =5,y=7,index = 0;
    
            int int_power = 0
            int condition = 0;
    
           if ( index == 5)
           {
                   i = 5;
                   y = 11;
    
           }
           if ( index >5 )
           {
                   i+=1;
                   y = 11;
           }
    
    
    
           int_power = (int)power;//used to check if power is zero ( because power is declared double , and it is not wise to use a double variable in an if statement,
                                  //i converted it to int
           if ( 0 == int_power)
           {
                   condition = 1;
                   gotoxy(i,y+4);
                   printf("%0.2f X ",fabs(multipicant));
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+5,y+4);
    
                           printf("%c",sign[index]);
                   }
    
           }
    
           if ( power == -1)
           {
                   condition = 0;
    
                   gotoxy(i,y+4);
                   printf("%0.2f * Ln(x)",fabs(multipicant));
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+14,y+4);
    
                           printf("%c",sign[index]);
                   }
    
           }
    
    
           else
           {
                   condition = 0;
    
                   gotoxy(i+9,y+2);
                   printf("%0.0f + 1",power);//the power of X
                   gotoxy(i+7,y+3);
                   printf("( X )");//print X
                   gotoxy(i,y+4);
                   printf("%0.2f *",fabs(multipicant));//multipicant
                   printf(" ------");//division line
                   gotoxy(i+6,y+5);
                   printf(" %0.0f + 1",power);//power
    
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+14,y+4);
                           printf("%c",sign[index]);
                   }
           }
           if (condition == 1)
           {
                   i+=6;
           }
           else
           {
                   i+=15;
           }
    
           index++;
    
            return ;
    }
    Sample sreenshot
    http://master.huricane.googlepages.c...screenshot.JPG
    tanx in advance
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int_power = (int)power;
    This is equivalent:

    Code:
    if ( 0 == (int)power)
    Interesting that you observe "because power is declared double , and it is not wise to use a double variable in an if statement" then less than ten lines later:

    Code:
    if ( power == -1)
    It is not *necessarily* bad to use a double in an if; more specifically, it is bad to use == to compare a double or a float to an integer such as -1 or 0. So you are correct to change the double, but you should do it with a simple cast and you should do it consistently.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by MK27 View Post
    Code:
    int_power = (int)power;
    This is equivalent:

    Code:
    if ( 0 == (int)power)
    Interesting that you observe "because power is declared double , and it is not wise to use a double variable in an if statement" then less than ten lines later:

    Code:
    if ( power == -1)
    It is not *necessarily* bad to use a double in an if; more specifically, it is bad to use == to compare a double or a float to an integer such as -1 or 0. So you are correct to change the double, but you should do it with a simple cast and you should do it consistently.
    tanx , i forgot that part!
    but still i cant get the first if block to execute ! i m really confused! whats wrong with it?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want something to only happen if a condition is false, maybe you should put it in an else block? (The else block you have only corresponds to power == 1.)

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >but i dont know why a specific if block doesnt execute
    May be because the power value is not equal to 0. Try putting a print statment just above the if statment and see what does power varibale hold. Try debugging through that may. You might be able to pin point the error.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by tabstop View Post
    If you want something to only happen if a condition is false, maybe you should put it in an else block? (The else block you have only corresponds to power == 1.)
    yes, thats right, but as you see the first if block is placed before the second if .. else blocks, meaning if the " if statement condition is true then the block must execute.
    i think i should add some more checks to the if's continuation conditions ,
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by ssharish2005 View Post
    >but i dont know why a specific if block doesnt execute
    May be because the power value is not equal to 0. Try putting a print statment just above the if statment and see what does power varibale hold. Try debugging through that may. You might be able to pin point the error.

    -ssharish
    i did it prints 0! i dont know either!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Masterx View Post
    yes, thats right, but as you see the first if block is placed before the second if .. else blocks, meaning if the " if statement condition is true then the block must execute.
    i think i should add some more checks to the if's continuation conditions ,
    What makes you think it doesn't get executed? It does, so far as I can tell, but it gets overwritten when the else part of the -1 check happens.
    Last edited by tabstop; 06-21-2009 at 12:40 PM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Pretty much very strange, try placing it this way

    Code:
    if( int_power == 0)
    This shouldn't make any difference at all. But worth a try. Between would you be post the whole code, so that i could try as well on my system.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by tabstop View Post
    What makes you think it doesn't get executed? It does, so far as I can tell, but it gets overwritten when the else part of the -1 check happens.
    May be that right, he should probably use the if-elseif-else statment to skip the second if check.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    i changed the last else to the following and it seems they are fine only if i dont use sth like "6x^-1" meaning a negative 1 power! if i do that ! the next zero powered expression would be altered! and the power would get incremented by one! which i have no idea whats happening to them!!


    Code:
    else if ( (-1!= (int)power )&& ( 0 != (int)power) )
    1.sample one showing the correct output

    sample 1.JPG - Free Image Hosting at TurboImageHost
    2.sample 2 showing the messed up output!!
    sample2.JPG - Free Image Hosting at TurboImageHost
    Last edited by Masterx; 06-21-2009 at 07:25 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  12. #12
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by ssharish2005 View Post
    Pretty much very strange, try placing it this way

    Code:
    if( int_power == 0)
    This shouldn't make any difference at all. But worth a try. Between would you be post the whole code, so that i could try as well on my system.

    -ssharish
    here it is . please note that where ever you see these words they mean
    tavan = power
    zarib = multipicant
    . and the function in question is Integral() ;
    and after download remove the .txt form the end of the file .
    i translated some words to english , and now i think its fine .
    Attached Files Attached Files
    Last edited by Masterx; 06-21-2009 at 07:27 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  13. #13
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    any idea why this is happening ?
    i also noticed that when that strange result happens, the power is actually 0 not 1.
    i mean in this picture the power of 8 seems to be incremented by 1! but actually it is not like that! i used a printf! and saw that the power inside the block is still 0!
    i dont know how that got there! with power 0! because of the condition check!

    as you see everything before expressions like ( 6x^-1) is fine , and if blocks seem to work perfectly ! but when you use expressions with negative 1 (-1) power! this happens!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #14
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    i finally figured it out . the problem was caused by :
    if ( 0 == (int)power)
    and the likes,
    i changed the power to an integer value out of the blocks and used that integer_power as a check in if statement continuation condition . and it worked!
    here is the final bit of code
    Code:
    double Integral( const double multipicant,const double power,char sign[],int size,int operand_number)
    {
    
            
            double integral = 0.0;
            static int i =5,y=7,index = 0;
            int integer_power = 0;
            int condition = 0;
    
           if ( index == 5)
           {
                   i = 5;
                   y = 11;
                   //condition = 1;
           }
           if ( index >5 )
           {
                   i+=1;
                   y = 11;
           }
    
           integer_power = power;
    
           if ( integer_power == 0 )
           {
    //               gotoxy(35,30);
    //               printf("\tp1%d",power);
                   condition = 1;
                   gotoxy(i,y+4);
                   printf("%0.1fX",fabs(multipicant));
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+5,y+4);
    
                           printf("%c",sign[index]);
                   }
    
           }
    
         else if ( integer_power == -1)
           {
                   condition = 0;
    
                   gotoxy(i,y+4);
                   printf("%0.2f * Ln(x)",fabs(multipicant));
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+14,y+4);
    
                           printf("%c",sign[index]);
                   }
    
           }
    
    
         else  if ( ( integer_power != -1 )&& ( integer_power != 0 ) )
           {
                   condition = 0;
    
    
                   gotoxy(i+9,y+2);
                   printf("%0.0f + 1",power);//powere X
                   gotoxy(i+7,y+3);
                   printf("( X )");//khode X
                   gotoxy(i,y+4);
                   printf("%0.2f *",fabs(multipicant));//multipicant
                   printf(" ------");//khate kasri
                   gotoxy(i+6,y+5);
                   printf(" %0.0f + 1",power);//poweremakhraj
    
                   if ( index != operand_number-1 )
                   {
                           gotoxy(i+14,y+4);
                           printf("%c",sign[index]);
                   }
           }
    
    
    
           if (condition == 1)
           {
                   i+=6;
           }
           else
           {
                   i+=15;
           }
    
           index++;
    
            return integral;
    }
    thankyou everyone for your helps
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:41 PM
  2. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  3. Replies: 8
    Last Post: 04-07-2004, 02:29 PM
  4. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 10:06 AM
  5. Finding the mode of an array
    By Shnakepup in forum C++ Programming
    Replies: 16
    Last Post: 05-16-2003, 11:01 AM

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