Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Swapping logic

Reply
Thread Tools

Swapping logic

 
 
ZUBAIR TRALI
Guest
Posts: n/a
 
      09-23-2012
#include<stdio.h>
/*new logic for swapping*/
#include<conio.h>
void main()
{
int x=20,y=30;
printf("nos before swapping are %d and %d\n",x,y);
x=x*y/(y=x);
printf("nos after swapping are %d and %d\n",x,y);
getch();
}
 
Reply With Quote
 
 
 
 
Richard Damon
Guest
Posts: n/a
 
      09-23-2012
On 9/23/12 8:48 AM, ZUBAIR TRALI wrote:
> #include<stdio.h>
> /*new logic for swapping*/
> #include<conio.h>
> void main()
> {
> int x=20,y=30;
> printf("nos before swapping are %d and %d\n",x,y);
> x=x*y/(y=x);
> printf("nos after swapping are %d and %d\n",x,y);
> getch();
> }
>


This invokes undefined behavior, as you set the value of y, and also use
the value of y (for a purpose other than getting the value to set y to).
The language does not promise that the sub expression x*y will be
executed before the sub expression y=x.

Also, x*y needs to fit withing the range of an int, so it will only work
for "small" numbers, and it fails if y == 0 (as 0/0 again invokes
undefined behavior).
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-23-2012
On 9/23/2012 8:48 AM, ZUBAIR TRALI wrote:
> #include<stdio.h>
> /*new logic for swapping*/
> #include<conio.h>
> void main()
> {
> int x=20,y=30;
> printf("nos before swapping are %d and %d\n",x,y);
> x=x*y/(y=x);
> printf("nos after swapping are %d and %d\n",x,y);
> getch();
> }


Wonderful![*] Splendid![*] You're a genius![*]

Unfortunately, my stupid computer misbehaves when I use
your brilliant technique with x=0. It even misbehaves when
both x and y are 50000 to start with (which ought to be
trivial to "swap"). Also, my compiler complains

foo.c:6: warning: operation on 'y' may be undefined

What am I[*] doing wrong?
[*] <http://dictionary.reference.com/browse/irony>

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Casey Carter
Guest
Posts: n/a
 
      09-23-2012
On 2012-09-23 07:48, ZUBAIR TRALI wrote:
> #include<stdio.h>
> /*new logic for swapping*/
> #include<conio.h>
> void main()
> {
> int x=20,y=30;
> printf("nos before swapping are %d and %d\n",x,y);
> x=x*y/(y=x);
> printf("nos after swapping are %d and %d\n",x,y);
> getch();
> }
>

I think you're looking for:
x ^= y;
y ^= x;
x ^= y;

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-23-2012
On 2012-09-23, ZUBAIR TRALI <(E-Mail Removed)> wrote:
> #include<stdio.h>
> /*new logic for swapping*/
> #include<conio.h>
> void main()
> {
> int x=20,y=30;
> printf("nos before swapping are %d and %d\n",x,y);
> x=x*y/(y=x);


The problem here is that you're accessing y in an expression where y is
modified. That is undefined behavior.

More precisely, your accessing y for a purpose other than determining a new
value to be stored into y.

The following also accesses y in an expression where it is modified:

y = y + 1;

However, the access in "y + 1" is a necessary operand for computing the value
stored in the assignment. This imposes an order, which makes it well defined.

This one is not defined:

y + (y = 3);

The y on the left of the + is accessed, but that access is not necessary for
the y = 3 assignment on the right. Because it is not necessary, it does not
have to be ordered relative to the assignment, and so we do not know when that
access occurs: before, during or after the completion of assignment to y?

If you write such ambiguous expressions in your C programs, they will be
nonportable between compilers and possibly unreliable even on one compiler.
For instance, if you change some code-generation options such as optimization
settings, the behavior might change.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-23-2012
On 9/23/2012 12:35 PM, Casey Carter wrote:
> On 2012-09-23 07:48, ZUBAIR TRALI wrote:
>> #include<stdio.h>
>> /*new logic for swapping*/
>> #include<conio.h>
>> void main()
>> {
>> int x=20,y=30;
>> printf("nos before swapping are %d and %d\n",x,y);
>> x=x*y/(y=x);
>> printf("nos after swapping are %d and %d\n",x,y);
>> getch();
>> }
>>

> I think you're looking for:
> x ^= y;
> y ^= x;
> x ^= y;


Don't do this. See Question 20.15c on the comp.lang.c
Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      09-24-2012
On Sun, 23 Sep 2012 14:19:51 -0400, Eric Sosman wrote:

>> I think you're looking for:
>> x ^= y;
>> y ^= x;
>> x ^= y;

>
> Don't do this. See Question 20.15c on the comp.lang.c
> Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>.


And especially don't do this (or equivalent):

*p ^= *q;
*q ^= *p;
*p ^= *q;

as it's equivalent to *p=0 if p==q.

 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      09-24-2012
Eric Sosman wrote:

> Wonderful![*] Splendid![*] You're a genius![*]


YHBT.

 
Reply With Quote
 
Andrew Smallshaw
Guest
Posts: n/a
 
      09-26-2012
On 2012-09-23, Eric Sosman <(E-Mail Removed)> wrote:

>> I think you're looking for:
>> x ^= y;
>> y ^= x;
>> x ^= y;

>
> Don't do this. See Question 20.15c on the comp.lang.c
> Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>.


Which is of course complete nonsense. It's the kind of thing that
happens when somone's _opinion_ that is implictly based on one
specific use case gets treated as undisputable and universal fact.
The smallest system I've ever used C on had 53 bytes of RAM. You
try making the argument that the correct solution is _always_ a
temporary variable in that kind of environment.

--
Andrew Smallshaw
(E-Mail Removed)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-26-2012
On 9/26/2012 1:20 AM, Andrew Smallshaw wrote:
> On 2012-09-23, Eric Sosman <(E-Mail Removed)> wrote:
>
>>> I think you're looking for:
>>> x ^= y;
>>> y ^= x;
>>> x ^= y;

>>
>> Don't do this. See Question 20.15c on the comp.lang.c
>> Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>.

>
> Which is of course complete nonsense.[...]


It's people like you who make me glad there are people
like Steve Summit.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Logic Map/Logic Flow Chart. (Example Provided) spike Python 8 02-09-2010 12:31 PM
Asynchronous Logic Gates and Analog Logic Gates Jyoti Ballabh Software 3 11-26-2009 06:48 PM
Blades, ASR's and Hot Swapping Rambo via WinServerKB.com MCSE 0 07-01-2005 03:20 PM
tips for swapping t1 config for 1700s and pix 6.3 jcharth@hotmail.com Cisco 0 07-01-2005 01:32 PM
swapping wlan cards Luddite Cisco 0 04-03-2005 11:16 PM



Advertisments