jeudi 10 décembre 2020

Why is my "if" condition completely skipped? C

I was writing a program that takes numbers from a text file, divides them into groups of three (a,b,c), next treats them as coefficients of a square function (ax^2+bx+c) and calculates deltas out of them. Problem is, the amount of numbers in the text file doesn't have to be divisible by 3 - that leaves us with possibly having 3's or 6's after the decimal dot. I tried writing:

if(r==2/3)
{
    printf("ax^2+bx\n");
}
    else
        if(r==1/3)
        {
        printf("ax^2");
        }
    else

int n - amount of numbers in the file
int k - (k=n/3) which is a number of groups of 3
float x - (x=(float)n/3), which is a number of groups of three with 1/3 or 2/3 remaining
float r - r=x-(float)k which is just the remaining 1/3 or 2/3

k=n/3;
x=(float)n/3;
r=x-(float)k;

I checked everything and each variable needed for this calculation is correct, so why does my program entirely skip the "if" condition?

Here's a sample that represents this problem:

#include <stdio.h>
int main()
{
int k,n;
float x,r;
printf("Please state the amount of numbers in the file: ");
scanf("%d",&n);
k=n/3;
x=(float)n/3;
r=x-(float)k;
printf("There's %d group(s) of 3\n",k);
printf("%d/3=%f\n",n,x);
printf("%f-%d=%f\n",x,k,r);
if(r==2/3)
{
    printf("ax^2+bx\n");
}
    else
        if(r==1/3)
        {
        printf("ax^2");
        }
    else


return 0;
}

Aucun commentaire:

Enregistrer un commentaire