3 Replies - 562 Views - Last Post: 21 November 2011 - 01:52 PM Rate Topic: -----

#1 theonegandalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 11

Help with an exercise in C

Posted 20 November 2011 - 04:38 AM

Hello,

I want your help with an exercise in C. Well, it gives us the piece of code(i've posted it below) and it telling us :

Watch the output for input 4,4. Do the same with 3, 4. Explain if the behavior of your program seems logic or if you observe something strange. And what is that. Also, explain how you can fix the problem that is presented, so that it has right output.

In this part of the code :
res = (--i == 3) || (j++ == 7);
i know that if the first condition is true, then res is set to true and it doesn't check the second condition. But i can't figure out what i have to do. Can you help me?


#include <stdio.h>
int main( void )
{
    int i, j;
    int res;

    scanf("%d", &i);
    scanf("%d", &j);

    res = (--i == 3) || (j++ == 7);

    printf("%d\n", i);
    printf("%d\n", j);
    printf("%d\n", res);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with an exercise in C

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Help with an exercise in C

Posted 20 November 2011 - 05:35 AM

The code is well formed; What is it that you or your instructor intended the program to do with that input?
Was This Post Helpful? 0
  • +
  • -

#3 gundy_75  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 24
  • Joined: 10-November 09

Re: Help with an exercise in C

Posted 20 November 2011 - 10:16 AM

res = ((--i == 3) || (j++ == 7));



Now it will check both conditions.
Was This Post Helpful? 0
  • +
  • -

#4 theonegandalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 11

Re: Help with an exercise in C

Posted 21 November 2011 - 01:52 PM

View Postgundy_75, on 20 November 2011 - 10:16 AM, said:

res = ((--i == 3) || (j++ == 7));



Now it will check both conditions.


Nah it isn't. Anyway apparently our professor wanted to show us how OR (||) works. I asked him and he told me

the goal of the exercise was to find a way to raise j even if --i == 3 was true. Anyway what i believe is that it was

a mistake to give us this exercise. Thanks anyway guyz! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1