1

I was asked to find the output of the following code segments. For any expression I was asked to evaluate form right to left by my teacher.

    #include<iostream.h>
    #include<conio.h>
    int main()
    {clrscr();
     int a[]={10,20,30,40};
     for(int i=0;i<3;)
        a[i]=a[++i];
     for(i=0;i<4;++i)
        cout<<a[i]<<endl;
     return(0);
     }

Now if I evaluate line7 from right to left, my output should be:

      20
      30
      40
      40

But if I evaluate from left to right, it will be

      10
      20
      30
      40

On running the program the output was case2.

Here's another one.

          #include<iostream.h>
          #include<conio.h>
          int main()
          {clrscr();
           int a[]={10,20,30,40};
           for(int i=0;i<3;)
               a[++i]=a[i];
           for(i=0;i<4;++i)
               cout<<a[i]<<endl;
           return(0);
           }

This time when I evaluated form right to left:

           10
           20
           30
           40

and from left to right:

           10
           10
           10
           10

And on running it I got case 1.

In which direction am I really supposed to evaluate? Why are both cases not matching?

SMcCK
  • 155
  • 7

0 Answers0