enter image description here

Help to understand the condition. I kind of did, but not sure. Help the result to write to the file.

int main() { int x,y; FILE *f = fopen("y.txt", "r"); printf ("Vvedite perenemyu X = "); scanf ("%d", &x); if (x >= 10) y = 1; else if (x > -0.5 && x < 10 && x != 0) y = 0; else if (x < -0.5) y = -1; fprintf(f,"y = %d\n",y); fclose (f); return 0; } 
  • 2
    Check is easier than asking. And I'm not at all sure that your code is clear ... - Vladimir Martyanov
  • Aaaa mmmm, did you even understand what you wrote? - psixiator 4:39 pm
  • 2
    First understand. And secondly - did you understand? - Vladimir Martyanov
  • one
    What should we do if х is still zero or 0.5? - Igor

1 answer 1

 int y; if (x >= 10) y = 1; else if (x > -0.5 && x < 10 && x != 0) y = 0; else if (x < -0.5) y = -1; 

The problem is that with x==0 and x == -0.5 value of y simply unknown.

Do you have this expression -0.5<x<10 will be interpreted as (-0.5 < x) < 10 ? those. the result of the comparison in parentheses ( true or false ) will be compared with 10, and will always give true .

I hope that you will be able to read the value of double x using scanf("%lf") ?

Yes, the condition x < 10 can be thrown away - since we got to it, x exactly less than 10 ... But this is not true for x < 0.5 - the same value 0 will pass to the last if ...

  • I enter X here, it counts and checks all this, but do I need to print Y? - psixiator
  • Well, by itself ... - Harry
  • is it necessary to write printf ("y =", & y)? - psixiator
  • one
    Have you actually been to lectures at least sometimes? :) printf("y = %d\n",y); - if, of course, y declared as int ... - Harry
  • one
    Open file with fopen , use fprintf , close file with fclose ... - Harry