# C/C++ simple challenge



## shoegoe (Oct 30, 2004)

we all know how to swap numbers in a program..

BUT we use a variable temp/like that to store the initial value..

can the same be done without using a TEMP variable?


----------



## cg84 (Oct 30, 2004)

very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped...


----------



## sujithtom (Oct 30, 2004)

That was a cool idea. How about manipulating fuctions???


----------



## cg84 (Oct 30, 2004)

sujithtom said:
			
		

> That was a cool idea. How about manipulating fuctions???



what??? didnt get ur question...


----------



## sms_solver (Oct 31, 2004)

cg84 from where did you find this solution?



> take 2 nos. x & y and do the following:
> x=x+y
> y=x-y
> x=x-y


----------



## alib_i (Oct 31, 2004)

its very old question !!

there are many more answers possible
x= x*y
y= x/y
x= x/y


----------



## NikhilVerma (Oct 31, 2004)

Ok now here is another simple one....

Do the same swapping with strings... without using temp variable...


----------



## cooljeba (Oct 31, 2004)

cg84 said:
			
		

> very simple...
> take 2 nos. x & y and do the following:
> x=x+y
> y=x-y
> ...


this is the best way to do it with out using a temp variable 


..:: peace ::..
jeba


----------



## vysakh (Oct 31, 2004)

*c++*

if you have c++ programs please mail me
vysakh007@gmail.com


----------



## alib_i (Oct 31, 2004)

Write the smallest C program which prints itself.


----------



## swatkat (Nov 1, 2004)

main()
{
 getche();
}
How about this?


----------



## lamrinnirmal (Nov 1, 2004)

> That was a cool idea. How about manipulating fuctions???



you mean manipulating program execution flow?as in the main calls function A but you wanna call B, without B(); anywhere in the program?


----------



## alib_i (Nov 1, 2004)

swatkat said:
			
		

> main()
> {
> getche();
> }
> How about this?



dont know the answer .. 
but i dont think that's correct


----------



## shoegoe (Nov 1, 2004)

O...


----------



## cnukutti (Nov 2, 2004)

What is the thing about manipulating functions??
and what do you mean by printing itself. 
should it read the .c file and print the source code???


----------



## swatkat (Nov 2, 2004)

"Write the smallest C program which prints itself."
What do you mean by this?


----------



## DKant (Nov 2, 2004)

NikhilVerma said:
			
		

> Ok now here is another simple one....
> 
> Do the same swapping with strings... without using temp variable...



Simple? Well I dunno. Sumthin' with pointers? But I don't see how u can do any manips on pointers.  

Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.


----------



## alib_i (Nov 3, 2004)

@swatcat ...
frankly speaking .. even i dont know ... this exactly was a problem statement in one programming contest long time back ..
i still dont know wht it means and how to solve it


----------



## vysakh (Nov 3, 2004)

i give up 
whats the answer???


----------



## shoegoe (Nov 3, 2004)

cg84 said:
			
		

> very simple...
> take 2 nos. x & y and do the following:
> x=x+y
> y=x-y
> ...



Ths...
BUT

How to swap more than 2 variables .....
(DONT TELL ME TO USE THE SAME CODE FOR EACH 2 SWAPS

ANSWERS??


----------



## game_dev (Nov 3, 2004)

The smallest code that prints itself : (In the question, the format of the output was not specified. So, the program prints itself in binary (not 0s & 1s) but ASCII codes of the binary). The code is C++ code as it uses iostream; but can be converted to C easily. But this is just a guess & not a definite solution.

#include<iostream.h>
void main()
{
	void 	(*f_ptr)()=main;
	char 	*ptr=(char *)f_ptr;
	cout << ptr;
}

Any other answers/corrections in above code ? Please post. 
[/code]


----------



## rahuld (Nov 4, 2004)

*ok try this*

main()
{
 printf("think digit");
main();
}


----------



## alib_i (Nov 4, 2004)

i think u got it right game_dev
will have to check it !!!


----------



## lamrinnirmal (Nov 4, 2004)

> main()
> {
> printf("think digit");
> main();
> }



pal ....cant use recursion in main!

anyways guys find out the swaping string thingy ! it sounds amazing. my comp teacher who keeps screaming that he knows C for 10 yrs , not that im insulting him. he knows a lot and keeps giving us such programs but when i asked him this he couldnt do it.
 I HAVE GOTTA KNOW THE SOLUTION TO THIS MAN! i wont move without it!




> this exactly was a problem statement in one programming contest long time back ..


do you have any more of such stuff dude?
and anyone knows dennis ritchie's email add?


----------



## game_dev (Nov 4, 2004)

Swapping of strings without temp variables is quite simple. It uses the same principle of swapping 2 integers. The program : 


```
#include <iostream.h>
void main()
{
	char a[50],b[50];
	int j=0;
	for(int i=0;i<50;i++)
	a[i]=b[i]=0;
	cout << "Enter string 1 : ";
	cin >> a;
	cout << "Enter string 2 : ";
	cin >> b;
	for(i=0;;i++)
	{
		if(a[i]=='\0') break;
		if(b[i]=='\0') {j=1;
			break;}
		a[i]=a[i]+b[i];
		b[i]=a[i]-b[i];
		a[i]=a[i]-b[i];
	}
	if(j==0)
	{
		j=i;
		do
		{
			a[i]=b[i];
			i++;
		}while(b[i]!='\0');
		b[j]='\0';
		a[i]='\0';
	}
	else
	{
		j=i;
		do
		{
			b[i]=a[i];
			i++;
		}while(a[i]!='\0');
		a[j]='\0';
		a[i]='\0';
	}
	cout << "String 1 : "<<a<<endl;
	cout << "String 2 : "<<b<<endl;
}
```
No third char variable is used. No user enterable character has an ASCII value more than 127. So this code will work for any user entered string. 
Hope this helps. Bye !


----------



## lamrinnirmal (Nov 4, 2004)

dude .........you re the man!


----------



## game_dev (Nov 4, 2004)

Thanks for the compliments. As for Dennis Ritchie's e-mail address, goto : *www.cs.bell-labs.com/who/dmr/

Bye !


----------



## cg84 (Nov 5, 2004)

DKant said:
			
		

> Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.



No dkant,  using add/subtract to swap without temps is pretty good. even if ur values are close to the limits(or even on the limits), the swap will take place without any problems. the reason for that is binary arithmetic. just add ur nos in binary, reject the carry out, and then subtract(using 2's complement) rejecting the borrow out. u will get the same nos u started with, only their places would be swapped. go ahead, try it, it works!!!


----------



## shoegoe (Nov 5, 2004)

alib_i said:
			
		

> Write the smallest C program which prints itself.



ok....But i dont know wheather it is correct answer to ur question..BUT


```
#include<stdio.h>
#include<conio.h>

void main()
{
	FILE *fd;
	char c;
	clrscr();
	fd = fopen("printme.c", "r"); /* Enter the address of the file */
	while (!feof(fd))
	{
		fscanf(fd, "%c", &c);
		putchar(c);
	}
	fclose(fd);
	getch();

}
```

OK...its in "C" AS U TOLD~

WELL>>THIS PROGRAM STILL PRINTS ITSELF .....

Tell me weather its correct or ><~


----------



## DKant (Nov 6, 2004)

@cg84 thx 4 that m8...I was beginning 2 lose my fundae. But actually C doesn't reject the carry out - that happens only when u use unsigned int. So the way it ultimately works is sumwhat skrud up..but it works!


----------



## alib_i (Nov 7, 2004)

yaar ..  shoegoe ..
frankly speaking i dont know the answer ..
but when i tried running the code on my TC .. the program went haywire

i wud ask other C studs here to verify the answer


----------



## shoegoe (Nov 7, 2004)

shoegoe said:
			
		

> alib_i said:
> 
> 
> 
> ...




amm......
you should* save the above code in a file named printme.c*( in this case c:\printme.c)

then compile and run as an ordinary program...

hope it works~!


----------



## shoegoe (Nov 7, 2004)

anyone tried it?


----------



## alib_i (Nov 7, 2004)

yes man .. it worked ...
u did it completely right ...
i had accidently stored it as printme.cpp ..
that's y it was giving error ..
its working prefect now


----------



## shoegoe (Nov 8, 2004)

alib_i said:
			
		

> yes man .. it worked ...
> u did it completely right ...
> *i had accidently stored it as printme.cpp *..
> that's y it was giving error ..
> its working prefect now



U asked a program in C and not cpp~!

Good to help.. 

any more challenges?


----------



## alib_i (Nov 8, 2004)

yeah .. my mistake ..
u did it right


----------



## cg84 (Nov 9, 2004)

ok. i've another idea...
lets use shoegoe's printme.c program, but just make a couple of changes to it. firstly, rename the main() function to something else, like print(). and don't save this file as printme.c, give it another name like printit.c.
now, make a file called printme.c(in the same dir as printme.c) and write this code in it:

#include "printit.c"
void main()
{
      print();
}

now, now, i know that it really isnt the answerr. its just using a loophole in the Q. technically,  printme.c is a complete C program in itself, isnt it? so maybe, this is ur answer...


----------



## firewall (Nov 9, 2004)

smallest c program to print itself :::

go to  *forums.devshed.com/archive/t-159303


----------



## NikhilVerma (Nov 9, 2004)

dunno if it's right.....


----------



## prankzter (Nov 11, 2004)

MAN...this guy is piling up his 11th class homework on us!!!

u R in cbse board...right??


----------



## alib_i (Nov 12, 2004)

*f00k u prankzter ..*
its not any 11th class homework

this question was asked in an all-india  c/c++ programming contest  3yrs back....
the only problem is that i dont remember the question properly.
and dont know the right answer too.

and to tell u ..
im not in 11th ..
im in 4th yr of my college ..
see how angry i am 
<----------------------


----------



## DKant (Nov 13, 2004)

Anybody done the magic square thingy? (It was something like sum of all rows/cols/diags of an nxn matrix is the same...I don't remember exactly. U have to build that matrix of course)

I haven't thought a lot about it..How do we implement it? Iterative methods? Remember this was just *one* question in a 3 hour/so C++ challenge.


----------



## NikhilVerma (Nov 13, 2004)

Actually tere is a prescribed method for making that square...

One of my freinds made that program ... Maybe I could get it from him... or make my own...

BTW check these out

*math.nmsu.edu/breakingaway/Lessons/magicsquare1/magicsquare.html
*www.math.hmc.edu/funfacts/ffiles/10001.4-8.shtml


----------



## vysakh (Nov 13, 2004)

try this
//program to print the numbers in a triangle
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int ar[10][10],n,i,j;
	cout<<"Enter number of rows:";
	cin>>n;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			ar_[j]=0;
	cout<<"The pascal triangle for "<<n<<" rows is :\n";
	for(i=0;i<n;i++)
	{
		cout<<"\n";
		ar[0]=1;
		for(j=0;j<=i;j++)
		{
			cout<<ar[j]<<"\t";
			ar[i+1][j+1]=ar[j]+ar[j+1];
		}
	}
	getch();
}_


----------



## jcmac (Nov 13, 2004)

#include<iostream.h>
void main()
{
int a,b,c;
cout<<"enter 2 numbers";
cin>>a,b;
c=a+b;
cout<<"first number :"<<c-a;
cout<<"second number :"<<c-b;
}


----------



## jcmac (Nov 13, 2004)

#include<iostream.h>
void main()
{
int a,b,c;
cout<<"enter 2 numbers";
cin>>a,b;
c=a+b;
cout<<"first number :"<<c-a;
cout<<"second number :"<<c-b;
}


----------



## vysakh (Nov 13, 2004)

you cant cin like that


----------



## it_waaznt_me (Nov 13, 2004)

And temp variable is not to be used ..


----------



## vinaypatel (Nov 14, 2004)

x=x+y 
y=x-y 
x=x-y


----------



## NikhilVerma (Nov 14, 2004)

Tat problem has already been solved


----------



## zahirdhada (Jun 5, 2009)

One line code for swapping two variables, a and b, without using third variable

a^=b^=a^=b

the above line of code will swap values of a and b


----------

