CrazyEngineers
  • sreeraj05
    sreeraj05

    MemberJul 25, 2011

    c aptitude array

    #include
    #include
    void main()
    {
    int a[]={0,1,2,3,4,5,6,7,8,9,10,11,12};
    int num=0,i=0;
    clrscr();
    num=a[++i+a[++i]]+a[++i];
    printf("%d",num);
    getch();
    }

    how it produce output as 9
    can u explain parsing of a[++i+a[++i]]+a[++i]
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Saandeep Sreerambatla

    MemberJul 25, 2011

    Thread moved to CS section.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJul 26, 2011

    a[++i+a[++i]]+a[++i]
    a[ ++i + a[++i]] + a[++i]
    a[ ++i + a[++i]] + a[1]
    a[ ++i + a[2]] + a[1]
    a[ 3 + a[2]] + a[1]
    a[ 3 + 2] + a[1]
    a[5] + a[1]
    5 + 1
    6
    This is the way it would parse... Let me try again... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Pensu

    MemberJul 26, 2011

    I encountered the same kind of problem few days ago. I had a statement like this and it was not showing the expected result. @praveen: i also thought that it should work like this. But the real thing is ++i is not the whole statement so the value of i is not incremented at this place only. First the value of i is incremented in the whole statment i.e. it is incremented 3 times. So i becomes 3. Now wherever the compiler sees pre-incremented part(++x) it puts the final value and wherever it sees the post incremented part(x++), it puts the initial value. Now our statement:

    a[++i+a[++i]]+a[++i]

    Here all the "i"s are pre incremented, so the final i.e. 3 will be posted.

    a[3+a[3]]+a[3]
    a[3+3]+3
    a[6]+3
    6+3
    9

    This is one of the examples why these kind of statements are avoided in programming.
    You can take one more example....
    int x=10;
    y=x++ + x++;

    Logically the output should be 21, but its 20 only.
    You can try more examples like y=x++ + x++ + ++x;



    P.S.: Correct me if i am wrong.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJul 26, 2011

    nicepeeyush
    I encountered the same kind of problem few days ago. I had a statement like this and it was not showing the expected result. @praveen: i also thought that it should ork like this. But the real thing is ++i is not the whole statement so the value of i is not incremented at this place only. First the value of i is incremented in the whole statment i.e. it is incremented 3 times. So i becomes 3. Now wherever the compiler sees pre-incremented part(++x) it puts the final value and wherever it sees the post incremented part(x++), it puts the initial value. Now our statement:

    a[++i+a[++i]]+a[++i]

    Here all the "i"s are post incremented, so the final i.e. 3 will be posted.

    a[3+a[3]]+a[3]
    a[3+3]+3
    a[6]+3
    6+3
    9

    This is one of the examples why these kind of statements are avoided in programming.
    You can take one more example....
    int x=10;
    y=x++ + x++;

    Logically the output should be 21, but its 20 only.
    You can try more examples like y=x++ + x++ + ++x;



    P.S.: Correct me if i am wrong.
    The logic is undeniable!!! 😲 But yeah, right!!! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • sreeraj05

    MemberJul 26, 2011

    very very thanks dear friends
    Are you sure? This action cannot be undone.
    Cancel
  • manishks

    MemberJul 27, 2011

    Can we really increment the value of all the "i's" at one go? We need to follow the rules. We should first increment the value of 'i' which is inside the parenthesis and then the others. We need to follow the sequence. Following the sequence we get the result as 6 instead of 9. I guess 6 is the correct answer.

    Correct me if i am wrong!
    Are you sure? This action cannot be undone.
    Cancel
  • Pensu

    MemberJul 27, 2011

    @musicfreak: U r right....logically the answer should be 6, but u try to run the code, u will get the answer 9. The reason given is that when the value of i is incremented first time thats not the end of the statement, so the compiler goes till the end of statement, doingg all the increment operation. After that the values are put in our expression. A bit awkward, but this was the best explanation i could find.....😛
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJul 27, 2011

    @All: Nicepeeyush is right... Confirmed... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • manishks

    MemberJul 28, 2011

    Yes very logical explanation indeed!
    Are you sure? This action cannot be undone.
    Cancel
  • Pensu

    MemberJul 28, 2011

    @musicfreak: try reading "Exploring C" by yashwant kanitkar......you will forget what logic is.....😉
    Are you sure? This action cannot be undone.
    Cancel
  • manishks

    MemberJul 28, 2011

    ok thanx peeyush😀
    Are you sure? This action cannot be undone.
    Cancel
  • vinci

    MemberOct 10, 2011

    exactly logically it would be 6
    Are you sure? This action cannot be undone.
    Cancel
  • computeridiot007

    MemberOct 16, 2011

    logical reason behind this can be explained with an example of another c program.Becasue when unary operators come into action it uses a concept called stack and reverse order execution.
    #include<stdio.h>
    
    void main()
    {
    int num=0;
    
    printf("%d%d%d%d",num++,++num,num++,++num);
    getch();
    }
    
    
    you make think that output of the program might be num++,++num,num++,++num executed in this way.you will execute from left to right.
    According to your approach answer is 0224.
    But c will execute in reverse order from right to left ++num,num++,++num, num++ and now you think answer is 1133.But it is not since it will put it in stack and take it in reverse order so the
    correct answer is 3311
    Hope this might answer your queries

    Correct me if i am wrong.
    Visit my blog here:<a href="https://novicetoprogramming.blogspot.com/" target="_blank" rel="nofollow noopener noreferrer">Virtual Helper</a>
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register