2.7k views

The output of the following program is

main()
{
static int x[] = {1,2,3,4,5,6,7,8}
int i;
for (i=2; i<6; ++i)
x[x[i]]=x[i];
for (i=0; i<8; ++i)
printf("%d", x[i]);
}
1. 1 2 3 3 5 5 7 8
2. 1 2 3 4 5 6 7 8
3. 8 7 6 5 4 3 2 1
4. 1 2 3 5 4 6 7 8
| 2.7k views
+1
#include<stdio.h>
int main()
{
static int x[]={1,2,3,4,5,6,7,8};
int i;
for(i=0;i<8;i++)
printf("%d ",x[i]);
printf("\n");
for(i=2;i<6;++i)
{
x[x[i]]=x[i];
printf("\n  i=%d  x[%d]=%d",i,x[i],x[i]);
}
printf("\n");
for(i=0;i<8;i++)
printf("%d ",x[i]);
return 0;
}

Run this you will understand..!
0
I think there was a similar question asked this year in IIIT H entrance i.e PGEE

 x[] = {1,2,3,4,5,6,7,8}
 for (i=2; i<6; ++i)
x[x[i]]=x[i];

i= 2,

x[ x[2] ] = x[ 2]
x[ 3 ] = 3

i= 3,

x[ x[3] ] = x[ 3]
x[ 3 ] = 3                   // see for i=2

i= 4,

x[ x[4] ] = x[ 4 ]
x[ 5 ] = 5

i= 5,

x[ x[5] ] = x[ 5 ]
x[ 5 ] = 5                         // see for i=4

Hence array x[] = { 1, 2, 3, 3, 5, 5, 7, 8}

Ans- A

by Boss (26.5k points)
selected by
0
it is 1,2,3,3,5,5,7,8 .
+1
thanks .. my bad ..written by mistake ..

now updated..
0
@vijay are we considering index of array starting from 0 ?

second confusion is no which are we taking for i is that index value or elements of array .?
0
@Amit  .. yes ...here start index for array is 0, because loop is running from 0 to 7 and we have total 8 elements in array x[].

and for x[i] ... i is being used as index value for array x[].
0
Can you tell when the value of 'i' is incremented as in the for loop it is given that it is preincrement

so will 'i' be incremented at the beginning of the loop or at the end of loop

initially our array looks like this:

 1 2 3 4 5 6 7 8

at i=2

x[3] = x[2];  ===   x[3] == 3    ->     4 = 3

 1 2 3 3 5 6 7 8

at i=3
x[3] = x[3];   ---->   no change

 1 2 3 3 5 6 7 8

at i=4
x[5] = x[4];

 1 2 3 3 5 5 7 8

at i=5

x[5] = x[5];   ------>  no change

at i=6  loop exit condition

thus

output at last:

 1 2 3 3 5 5 7 8
by (361 points)
0
correct...good one
 1 2 3 4 5 6 7 8

this is an array starting with index 0.

when i=2; x[2]==3

x[x[i]]=x[i]  => x[3]=3; // this will replace the value of x[3] by 3

i=3 ; x[3]==3

x[3]=3

i=4 , x[4]==5

x[5]=5 // it will replace 6 by 5

i=5 ; x[5]==5

x[5]=5

Now the modified answer would be 12335578

by Junior (951 points)
edited
0
i < 6, not i <=6
+1
Thnaku sir for correcting me.
0

@Anjali_aspirant you have used comparison operator "==" in your solution. there is no comparison operator in question there is only assignment operator "=" ...I guess  or i misunderstood something .

0
== operator is used for comparing the two values that are equal, like in mathmatics .

suppose you have to say x value is 3. then how will you write in maths ? x=3 right?

in the same way ..if i have to say x[3] is equal to 2 ..then i can write x[3]==2 in c

if i am misunderstanding any thing plse correct me.
0

but why are you comparing here  x[2]==3 in your sol .I did not say your solution is wrong but i am not getting the meaning of this line

0

ok i will explain u.

when i=2; x[2]==3//this line means that at  index 2 value is 3 before entering into loop.

my purpose of writing  x[2]=3 is to simplify this statement x[x[i]]=x[i] . I have written the value of inner bracket ie x[2]

x[x[i]]=x[i]

x[x[2]] ==> x[3]=3

0
@shekhar : now u get my solution ? plse let me know
0

@Anjali_aspirant got you.

0
okie
1,2,3,3,4,5,6,7 according to me
by (281 points)
+2
how...u got this..