# Thread: need help on finding what i missed!

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

2. 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.

3. Originally Posted by MK27
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?

4. 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. >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

6. Originally Posted by tabstop
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 ,

7. Originally Posted by ssharish2005
>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!

8. Originally Posted by Masterx
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.

9. 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

10. Originally Posted by tabstop
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

11. 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

12. Originally Posted by ssharish2005
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 .

13. 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!

14. 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

Popular pages Recent additions