rainwater_stillicide

Challenge: BASIC001

30 posts in this topic

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.

0

Share this post


Link to post
Share on other sites

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.

neat.. Would be cool to see how people approach this.. Here's mine..

#include <stdio.h>

// bet it'll bomb on big numbers, but...

void show (int a, int
{
printf("a = %i, \tb = %i\n",a,;
return;
}

int main (void)
{
int a, b;
a = 14;
b = 29009;

show(a,;

a = a*b;
b = a/b;
a = a/b;

show(a,;

return 0;
}

edit:

Oh damn, you said function. I'm gonna have to think about this a bit.

Edited by PurpleJesus
0

Share this post


Link to post
Share on other sites


void swap(int *a,int * { *a^=*b^=*a^=*b; }

int main(void) {
int a = 4, b = 5;
printf("%d %d\n", a, ;

swap(&a,&;

printf("%d %d\n", a, ;
}

edit: there are also some processor that have a swap instruction.

Edit by Ohm: Hid answer with spoiler tags.

Edited by Ohm
0

Share this post


Link to post
Share on other sites

So I don't get it, whoever the winner is do they choose the next challenge or something?

Edited by SigFLUP
0

Share this post


Link to post
Share on other sites

So I don't get it, whoever the winner is do they choose the next challenge or something?

Anyone can add a challenge. Here are the rules for the challenges forum:

0

Share this post


Link to post
Share on other sites

okay, here it is as a function...

#include <stdio.h>

// bet it'll bomb on big numbers, but...

void show (int a, int
{
printf("a = %i, \tb = %i\n",a,;

return;
}

void swap (int *a, int *
{
*a = *a * *b;
*b = *a / *b;
*a = *a / *b;

return;
}

int main (void)
{
int a, b;

a = 10000;
b = 33;

show(a,;
swap(&a,&;
show(a,;
swap(&a,&;
show(a,;

return 0;
}

SigFlup, Could you explain how yours works. I'm curious to know what's happening there.

--btw: read the rules, Should this be posted as a new topic, but titled "question: BASIC001"? Little confusion here.

edit: posthumously added spoiler. opps.

Edited by PurpleJesus
1

Share this post


Link to post
Share on other sites

--btw: read the rules, Should this be posted as a new topic, but titled "question: BASIC001"? Little confusion here.

I believe the solutions should be posted in a new topic called "Answer: BASIC001". I'm not sure whether the person who creates the Challenge thread is also meant to create the Answer thread, or whether it's create by the first person to come up with an answer.

Edited by rainwater_stillicide
0

Share this post


Link to post
Share on other sites

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.

Yay for only using addition and subtraction!:


#include <stdio.h>

void swap( int *a, int *b );

int main( int numArgs, char *argList[] )
{
int a = 45;
int b = 77;

printf( "a = %d b = %d\n", a, b );
swap( &a, &b );
printf( "a = %d b = %d\n", a, b );
return 0;
}

void swap( int *a, int *b )
{
*a = ( *a + *b );
*b = ( *a - *b );
*a = ( *b - *a );
*a = ( *a - *a - *a );
}

And folks, don't forget the spoiler tags. just surround your code tags with spoiler tags.

Edited by FLW_FTW
0

Share this post


Link to post
Share on other sites

This was one of the first things we got thought in programming class when we were talking about logical operations.

Pseudo code:


//The power of XOR!!

function swap(var adress a, var adress {
a = a xor b
b = a xor b
a = a xor b
}

a = 10
b = 20
swap (a,

Also I wonder for the people using purely arithmetic functions (like purplejesus) what would happen is the second variable is zero.

//edit, WTF? every time I save changes it capitalizes my b in the code :question::confused::blink:

Edited by DanielG
1

Share this post


Link to post
Share on other sites

In AVR assembly (I'd do something else for larger values):

.cseg

.def r16 = a ;define register a (temp variable)

.def r17 = b ;ditto

ldi a, 5 ;load immediate 5 into a

ldi b, 12 ;ditto

lsl a ;logical shift left 4 times

lsl a

lsl a

lsl a

andi a, 0b11110000 ;bitmask, clears junk (on second thought, this is unnecessary)

andi b, 0b00001111 ;clears junk, again, unnecessary >.>

xor a, b ;places bit patters into cleared nibbles

xor b, a ;ditto

lsr b ;shift b back into position

lsr b

lsr b

lsr b

andi a, 0b00001111 ;clear old a from a's most significant nibble

0

Share this post


Link to post
Share on other sites

Pretty much the same mechanism using Python, again, assuming smaller integer values:

a = 34

b = 22

swap(a, B)

def swap(a, B):

a = a << 8

a = a ^ b

b = a

b = b >> 8

a = a & 65281 #(65535 - 255) or 0b1111111100000000

a = a >> 8

0

Share this post


Link to post
Share on other sites

Just a note on the answer threads, I put those rules in place for larger challenges where there may be lengthy answers and a lot of people answering. Each answer would then have its own thread, so it can be discussed. Don't worry about spamming threads, that's what this forum is for. Create as many threads as you need/want.

I really intended to post IGOR001 last week, but I'm really sick. Every time I think I'm getting better and have time/energy to finish it up and write the post, it just doesn't happen.

Anyway, this challenge has a more interesting counterpart: How do you reverse a string without allocating a second string?

0

Share this post


Link to post
Share on other sites

Just a note on the answer threads, I put those rules in place for larger challenges where there may be lengthy answers and a lot of people answering. Each answer would then have its own thread, so it can be discussed. Don't worry about spamming threads, that's what this forum is for. Create as many threads as you need/want.

I really intended to post IGOR001 last week, but I'm really sick. Every time I think I'm getting better and have time/energy to finish it up and write the post, it just doesn't happen.

Anyway, this challenge has a more interesting counterpart: How do you reverse a string without allocating a second string?

Is that a new challenge?

0

Share this post


Link to post
Share on other sites

Not really. The tricky part was already solved, now you just have to iterate over a string and do it.

1

Share this post


Link to post
Share on other sites

Not really. The tricky part was already solved, now you just have to iterate over a string and do it.

OK, here we go:


#include <stdio.h>

void swap( char *a, char *b );

int main( int numArgs, char *argList[] )
{
int len;
int i;

char *str = argList[1];

printf( "1- %s\n", str );

len = strlen( str );

/*swap the first and last letter, and move towards the middle.*/
for( i = 0; i < (len/2); ++i )
{
swap( &str[i], &str[len-(i+1)] ); /*be careful not to swap the \0 at the end!*/
}

printf( "2- %s\n", str );

return 0;
}

void swap( char *a, char *b )
{
*a = ( *a + *b );
*b = ( *a - *b );
*a = ( *b - *a );
*a = ( *a - *a - *a );
}

0

Share this post


Link to post
Share on other sites


void swap(int *a, int *
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}

0

Share this post


Link to post
Share on other sites

Also I wonder for the people using purely arithmetic functions (like purplejesus) what would happen is the second variable is zero.

It bombs horribly.. Good catch.

edit: easy fix though..

#include <stdio.h>

// bet it'll bomb on big numbers, but...

void show (int a, int
{
printf("a = %i, \tb = %i\n",a,;

return;
}

void swap (int *a, int *
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;

return;
}

int main (void)
{
int a, b;

a = -60;
b = -0;

show(a,;
swap(&a,&;
show(a,;
swap(&a,&;
show(a,;

return 0;
}

Edited by PurpleJesus
0

Share this post


Link to post
Share on other sites

EDIT : I fixed my code


puts "What number for a?"
a = gets.chomp.to_i
puts "What number for b?"
b = gets.chomp.to_i

a,b = b,a #here's the part that is swaped

puts ""
puts "----"
puts ""

puts "a is " + a.to_s
puts "b is " + b.to_s

this is much simpler

Edited by bcrscahh198987
0

Share this post


Link to post
Share on other sites

So.... Is everyone using C#??

I recognize that <studio.h> header

Edited by bcrscahh198987
0

Share this post


Link to post
Share on other sites

So.... Is everyone using C#??

I recognize that <studio.h> header

um, No. most of it is in C

and it's <stdio.h> the Standard C library I/O functions,

0

Share this post


Link to post
Share on other sites

So.... Is everyone using C#??

I recognize that <studio.h> header

um, No. most of it is in C

and it's <stdio.h> the Standard C library I/O functions,

void swap(int* a,int* B){

*b=*b+*a;

*a=*b-*a;

*b=*b-*a;

}

0

Share this post


Link to post
Share on other sites

SigFlup, Could you explain how yours works. I'm curious to know what's happening there.

oh sorry, I didn't really answer that. If you xor something by a number and xor it again you get the same number... so it's kinda like hiding a number inside another number. So we xor one number into another and the other into the first one. Rinse and repeat and you just swapped them.

0

Share this post


Link to post
Share on other sites

Here's my implementation in Python. I'm kind of surprised nobody did this... It doesn't seem to break the rules at all.

def swap(a,:
return (b,a)

(a, = swap(a,

Granted, it's not as cool as doing the XOR trick, but it's a lot shorter code than anyone else has written.

You can also shorten it even further, like so:

swap = lambda (a,: (b,a)
(a, = swap((a,)

Edited by z3ros3c
0

Share this post


Link to post
Share on other sites

Here's my answer with python. I looked around at some other peoples, and I feel like I'm missing something huge though. If I am could someone care to explain?


def swapab(a,:
a = a + b
b = a - b
a = a - b
return a,b

Edited by Lord Wud
0

Share this post


Link to post
Share on other sites

This is quite fun. ;)

// File: main.cpp
// Title: BinRev BASIC001
// Description: Using the language of your choice: Write a function in which
// two integer variables (a and have their values swapped
// without using any temporary variables.
// Author: codered22
// Date: 2010-10-23

#include<iostream>

using namespace std;

void swapValues(int & _a, int & _ {
_a += _b;
_b = _a - _b;
_a = _a - _b;
}

int main() {
int a, b;

cin >> a >> b;

swapValues(a, ;

cout << a << " " << b;

return 0;
}

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now