C/C++ simple challenge

Status
Not open for further replies.

game_dev

Broken In
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]
 

lamrinnirmal

Journeyman
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? :wink:
 

game_dev

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

Code:
#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 !
 

game_dev

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

Bye !
 

cg84

Broken In
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!!!
 
OP
shoegoe

shoegoe

Broken In
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

Code:
#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

In the zone
@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! :p
 

alib_i

Cyborg Agent
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
 
OP
shoegoe

shoegoe

Broken In
shoegoe said:
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

Code:
#include<stdio.h>
#include<conio.h>

void main()
{
	FILE *fd;
	char c;
	clrscr();
	fd = fopen("c:\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 ><~

:)


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~!


 

alib_i

Cyborg Agent
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
 
OP
shoegoe

shoegoe

Broken In
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.. :D

any more challenges?:?: :?:
 

cg84

Broken In
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... :(
 
Status
Not open for further replies.
Top Bottom