Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[Updated]
I was trying to solve this problem:
1098 - Sequence IJ 4 - URI Online Judge[^]

I used else if statement to print integer when needed. It worked for two conditions but it's not working for the third condition. The code I wrote is given below.
[sorry for my bad English]

What I have tried:

#include<stdio.h>
int main(){
int a,b,c,d,e, about;
double x,y,z, sum;
double i, j;
for(i=0; i<=2; i=i+0.2){
    for(j=1; j<=3; j++){

        if(i==0.0){
            printf("%.0lf %.0lf\n", i, j+i);
        }else if(i==1.0){
        printf("%.0lf %.0lf\n", i, j+i);
        }else if(i==2.0){
        printf("%.0lf %.0lf\n", i, j+i);
        }else{
        printf("%.1lf %.1lf\n", i, j+i);
        }
    }
}
return 0;
}
Posted
Updated 26-Oct-21 19:15pm
v3

What is happening is you are bumping up against small, numerical errors in floating point values. The value of i after ten 0.2s were added will not match 2.0 exactly. It probably will out to be about eight or nine decimal places but not exactly. If you want to see this more closely, add this statement right before the if :
C++
printf( "i is %18.16f\n", i );
That will show you the value of i to sixteen decimal places.

To get around this you need to allow for a tolerance which is often called epsilon. There are lots of ways to deal with this and here is one option. Write a function called CloseEnough or something similar. Here's how it might look.
C++
// function will return 1 if close enough, 0 if not

int CloseEnough( double value1, double value2 )
{
    double delta = fabs( value1 - value2 );  // obtain absolute value of difference
    return ( delta < 1.0E-6 ) ? 1 : 0;       // compare with epsilon value
}
In this logic the epsilon value is 1E-6 or 0.000001. When comparing with 2.0 it will return 1 if the value is between 1.999999 and 2.000001 and zero if it isn't. This not an particularly general purpose solution because it will give bad results for very large and very small numbers but for values of the magnitude of yours it should be suitable.
 
Share this answer
 
Comments
CPallini 21-Oct-21 2:19am    
5.
Quote:
I used else if statement to print integer when needed. It worked for two conditions but it's not working for the third condition. The code I wrote is given below.

Because your code says so:
C++
#include<stdio.h>
int main(){
int a,b,c,d,e, about;
double x,y,z, sum;
double i, j;
for(i=0; i<2; i=i+0.2){ // i is never equal to 2.0 in the loop
    for(j=1; j<=3; j++){

        if(i==0.0){
            printf("%.0lf %.0lf\n", i, j+i);
        }else if(i==1.0){
        printf("%.0lf %.0lf\n", i, j+i);
        }else if(i==2.0){ // so this condition is never met
        printf("%.0lf %.0lf\n", i, j+i);
        }else{
        printf("%.1lf %.1lf\n", i, j+i);
        }
    }
}
return 0;
}

[Update]
Quote:
1st two outputs are integers... But the third printf is printing float... that's why I am telling that third statement is not working.

Stop guessing, make sure, try with this code.
C++
        if(i==0.0){
            printf("A %.0lf %.0lf\n", i, j+i);
        }else if(i==1.0){
            printf("B %.0lf %.0lf\n", i, j+i);
        }else if(i==2.0){
            printf("C %.0lf %.0lf\n", i, j+i);
        }else{
            printf("D %.1lf %.1lf\n", i, j+i);
        }
    }
}
return 0;
}

Use the letter in each printf to distinguish which printf was used.
Ur learn debugger.
 
Share this answer
 
v2
Comments
Jahirul Sarker 21-Oct-21 9:22am    
I changed it to for(i=0; i<=2; i=i+0.2).
But it's not working. Same problem remains. Third condition is not working
Patrice T 21-Oct-21 9:57am    
How can you know which condition matched since the 3 first output are same ?
Jahirul Sarker 24-Oct-21 2:23am    
1st two outputs are integers... But the third printf is printing float... that's why I am telling that third statement is not working.
Patrice T 24-Oct-21 2:59am    
See updated solution.
Here's the solution:
#include<stdio.h>

int main(){
double i,j;


for(i=0; i<=2; i=i+0.2){
    for(j=1; j<=3; j++){

        if (i<0.1){
            printf("I=%.0lf J=%.0lf\n", i, j+i);
        }else if(i>0.9&&i<1.1){
        printf("I=%.0lf J=%.0lf\n", i, j+i);
        }else if(i>1.8&&i<2.1){
        printf("I=%.0lf J=%.0lf\n", i, j+i);
        }else{
        printf("I=%.1lf J=%.1lf\n", i, j+i);
        }
    }
}
return 0;
}
 
Share this answer
 
Comments
CHill60 27-Oct-21 4:58am    
For every single one of your questions you have subsequently posted a solution and marked it as the "best answer". That is actually quite rude, and devalues the contribution from other members that led you to that solution.
Some members will also see that as "reputation point hunting" and consider it to be abuse of the site - that could lead to your account being suspended.
Please mark the most helpful solution as the "best" - you could even be guided by the rest of the community - at the time of writing Solution 1 has 5 upvotes
Jahirul Sarker 27-Oct-21 6:47am    
Thank you. Honestly, I wasn't aware of that. I was stupid. I wasn't aware of the points.
I will never repeat this mistake again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900