Thursday, July 30, 2009

What is the function of these codings" a[i]=a[i+1] & a[i+1]=a[i] " in C programming language?

These are simple assignment statements.





a[i] = a[i+1]


is equivalent to the statement:


assign the (i+1)-th element of the array a to the i-th element to the array a





Similarly,


a[i+1] = a[i]


means to assign the (i)=th element of the array a to the (i+1)-th element of the array a





In C the LValue (Left hand side value) is the one that receives the value from the right side.





say b=5, assigns 5 to the variable b.





Again the ampersand (%26amp;) is binary operator in C. It performs a binary AND operation on both operands (i.e., the ones on it''s both sides) and returns the result.





In this case a[i]=a[i+1] %26amp; a[i+1] = a[i]


the code always returns True since the result of assignments (1; as the assignments actually takes place) gets AND-ed

What is the function of these codings" a[i]=a[i+1] %26amp; a[i+1]=a[i] " in C programming language?
a[i]=a[i+1] %26amp; a[i+1]=a[i]





Humm! seems something is wrong: in C you can assign several things at the same time, for example:


a=b=c=1+2%26amp;3


however, the compiler throws a warning since you are trying to assign a value to an operation (that is, a[i+1] %26amp; a[i+1]=a[i])


The operation a[i+1] %26amp; a[i+1] changes nothing, I mean, a[i+1] %26amp; a[i+1] is a[i+1]. But since it's an operation, you can't assign to it a[i]. However, you can assign a[i]=a[i+1] %26amp; a[i+1].
Reply:if a is an array


i case


the value of i th element in the array is given by the i+1 th element


this technique is used in looping where one element is deleted from the list





-----------


ii case


-----------


the value of i+1 th element in the array is given by the i th element


this technique is used in looping where one element is inserted in the list
Reply:The function means nothing and it will not compile.





To break it's meaning down in words, it means:


Firstly, imagine the array as mailboxes for an apartment building all lined up.





1. Given any mailbox,


2. open the next mailbox,


3. read the contents and read it again


4. combine the knowledge of the first and second time you read the information (which will be the same)





5. the knowledge you obtained in step 3 and 4, should now be equivalent what's in the previous mailbox





As soon as you reach step 5, the logic fails. It is true that there may exist a possibility that it may succeed; for example, two empty mailboxes, however this is one possibility, and does NOT always hold true. The equation states it must always be true.











Let's simplify it mathematically:


Let a[i] be represented by X


and


a[i+1] be represented by Y;


Therefore we have:


X = Y %26amp; Y = X





Syntactically it doesn't make any sense.


Proof; arbitrarily, let X = 5, and Y = 8; thus we have


5 = (8 %26amp; 8) = 5





if we solve the (8 %26amp; 8) part we get 8





(take the binary representation of 8 and binary AND it like this: [8 in binary is 01000]


01000


01000


%26amp;


-------------


01000; this is again, 8.)


so, that means:





5 = 8 = 5





Finally:


The equations


X = Y %26amp; Y = X; can therefore be simplified, by (key part here) EVALUATING the Y %26amp; Y to being the VALUE OF Y (and not a variable any more). SO, we get


X = {some value of Y%26amp;Y} = X; the compiler knows now we have something like the example above; and is aware it can logically evaluate the statement as being invalid.





What would be valid is:


int X, Y;


X=Y=X; // valid; since X and Y has has no initial value complier WARNING


int a[2];


int i=0;


a[i] = a[i+1] = a[i]; // valid; compiler WARNING (not initialized)


a[i]= a[i+1] %26amp; a[i+1]; // valid





a[i] = a[i+1] %26amp; a[i+1] = a[i]; // INVALID since (a[i+1] %26amp; a[i+1]) is NO LONGER A VARIABLE





Thus invalid code; compiler will throw an error.
Reply:if this related to array, then we can find such type of code is used in swapping of two consecutive values.


No comments:

Post a Comment