View Full Version: Programming challenge!

C++ Learning Community > C++ in General > Programming challenge!

Pages: [1] 2

Title: Programming challenge!
Description: Are you good enough?


Kylevision - August 14, 2003 07:45 PM (GMT)
My challenge to you is to try and to write a program that shows its own source.

TheHawgMaster - August 15, 2003 03:01 AM (GMT)
That's just as impossible as finding the last reflection of two paralell mirrors.

Sam Fisher vs Solid Snake - August 15, 2003 03:03 AM (GMT)
actually its possible, but it could screw over so much

dr voodoo - August 15, 2003 10:37 AM (GMT)
Actually if you don't use any txt file to store the code it's impossible. Try tipping it out and you'll see the problem SFvsSS

ih8censorship - August 16, 2003 01:14 AM (GMT)
yeah i tryed it....
QUOTE
That's just as impossible as finding the last reflection of two paralell mirrors.
yep thats true... unless you knew about how assembly language worked real good then you could display the REAL asm source of your program. other than that...

FHCandyman - August 16, 2003 01:16 AM (GMT)
you know kylevision, this is a C++ beginners board :P

Kylevision - August 16, 2003 03:22 AM (GMT)
It is possible, although hard.

dr voodoo - August 16, 2003 06:31 PM (GMT)
QUOTE
unless you knew about how assembly language worked real good then you could display the REAL asm source of your program. other than that...

If you mean any code and not just the C++ code then it's simple! All Hex Editor can do it! Tell it to open itself and you'll see the mashine code. And writting a hex editor isn't at all that hard.

It is impossible to output the exact C++ code because all information about macros and comments are lost.

TheHawgMaster - August 16, 2003 06:52 PM (GMT)
I dunno, the whole problem seems kind of un-defined. Would you consider it to be showing the source if I had it as a string held as a program resource? That would be easy to do. But to actually have it as a string in the source is impossible.

Kylevision - August 18, 2003 03:36 AM (GMT)
ok, let me say this, I dont care how you do it, make a program that shows its source.

mikawo - August 26, 2003 12:14 PM (GMT)
Someone made a demo that disassembles itself and displays its asm code onto the screen. It was written in ASM. I would give you a link, but alas I have no clue what the URL was.

I don't know why you made that challenge. Did you want to know if it was possible or did you want to see if the people on the forum had those kind of skills?

Uraldor - November 12, 2003 01:55 AM (GMT)
Hi,

This is not impossible at all. It's possible in quite a few languages (probably all), including C/C++, without having to decompile/disassemble code. Programs like this are called "Quines". Here is an example of a quine written in C:
CODE
#include <stdio.h>
int main(void){char*s="#include <stdio.h>%cint main(void){char*s=%c%s%c;printf(s,10,34,s,34,10);return(0);}%c";printf(s,10,34,s,34,10);return(0);}


Cheers
OJ

EDIT: Btw, you can find an interesting and friendly programming forum at http://forum.eyedolon.com

ih8censorship - November 12, 2003 02:02 AM (GMT)
dude-thats COOL! :D

*edit* we have more members :P but i suppose yours was started later on too....

TheRealNapster - November 12, 2003 02:10 AM (GMT)
I could do it if I knew how to use C++ language to open a file on my computer. Does anyone know how to do that? :huh:

ih8censorship - November 12, 2003 02:18 AM (GMT)
yeah read my tutorials on the home page on files.they should be helpfull, but i think the idea was making an exe that dident read from a file display the source that was used to make it.

MonkeyMan - November 12, 2003 02:55 AM (GMT)
hehe... also can be done in BF...

PS: Yes, i still do stop by here. :D

Uraldor - November 13, 2003 02:45 AM (GMT)
I could do it if I knew how to use C++ language to open a file on my computer. Does anyone know how to do that?

I can't see how that will help you solve the problem. You generally don't have the source code of your program available as a file when you're running your program, and opening the source file and blatting it to the screen is cheating ;)

So I disagree with you, you can't do it just by opening the exe. You need to be smarter.

OJ

PS. Yes, the forum is quite new, give it time and it'll build up! There are other programming challenges listed in there aswell if you're interested.

TheRealNapster - November 13, 2003 12:38 PM (GMT)
there is a way to open a file on your computer(such as a notepad containing your source code) through a program. you just need to write it in code and i dont know how to do that yet. how is that cheatin? :huh:

ih8censorship - November 13, 2003 01:24 PM (GMT)
because then its just a program that reads a file its not a program that displayes its own source after you compiled it you could change whatever you wanted the text to be in the source.

netcrusher88 - November 16, 2003 09:30 PM (GMT)
x86 ASM:
CODE
bits 16
org 256
mov ah,64
mov bx,1
mov cx,720
mov dx,b+16
int 33
mov cx,90
mov si,b+16
c:push cx
mov ah,64
mov bx,1
mov cx,6
mov dx,b
int 33
mov cx,7
a:push cx
mov al,[si]
shr al,4
add al,48
cmp al,58
jb g
add al,39
g:mov [b+8],al
lodsb
and al,15
add al,48
cmp al,58
jb e
add al,39
e:mov [b+9],al
mov ah,64
mov bx,1
mov cx,5
mov dx,b+6
int 33
pop cx
loop a
mov al,[si]
shr al,4
add al,48
cmp al,58
jb h
add al,39
h:mov [b+8],al
lodsb
and al,15
add al,48
cmp al,58
jb n
add al,39
n:mov [b+9],al
mov ah,64
mov bx,1
mov cx,4
mov dx,b+6
int 33
pop cx
loop c
mov ah,64
mov bx,1
mov cx,5
mov dx,b+11
int 33
mov ah,76
int 33
b   db 13,10,' db 0x  ,',13,10,'end'
db 0x62,0x69,0x74,0x73,0x20,0x31,0x36,0x0d
db 0x0a,0x6f,0x72,0x67,0x20,0x32,0x35,0x36
db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68
db 0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f,0x76
db 0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a,0x6d
db 0x6f,0x76,0x20,0x63,0x78,0x2c,0x37,0x32
db 0x30,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x64
db 0x78,0x2c,0x62,0x2b,0x31,0x36,0x0d,0x0a
db 0x69,0x6e,0x74,0x20,0x33,0x33,0x0d,0x0a
db 0x6d,0x6f,0x76,0x20,0x63,0x78,0x2c,0x39
db 0x30,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x73
db 0x69,0x2c,0x62,0x2b,0x31,0x36,0x0d,0x0a
db 0x63,0x3a,0x70,0x75,0x73,0x68,0x20,0x63
db 0x78,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61
db 0x68,0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f
db 0x76,0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a
db 0x6d,0x6f,0x76,0x20,0x63,0x78,0x2c,0x36
db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x64,0x78
db 0x2c,0x62,0x0d,0x0a,0x69,0x6e,0x74,0x20
db 0x33,0x33,0x0d,0x0a,0x6d,0x6f,0x76,0x20
db 0x63,0x78,0x2c,0x37,0x0d,0x0a,0x61,0x3a
db 0x70,0x75,0x73,0x68,0x20,0x63,0x78,0x0d
db 0x0a,0x6d,0x6f,0x76,0x20,0x61,0x6c,0x2c
db 0x5b,0x73,0x69,0x5d,0x0d,0x0a,0x73,0x68
db 0x72,0x20,0x61,0x6c,0x2c,0x34,0x0d,0x0a
db 0x61,0x64,0x64,0x20,0x61,0x6c,0x2c,0x34
db 0x38,0x0d,0x0a,0x63,0x6d,0x70,0x20,0x61
db 0x6c,0x2c,0x35,0x38,0x0d,0x0a,0x6a,0x62
db 0x20,0x67,0x0d,0x0a,0x61,0x64,0x64,0x20
db 0x61,0x6c,0x2c,0x33,0x39,0x0d,0x0a,0x67
db 0x3a,0x6d,0x6f,0x76,0x20,0x5b,0x62,0x2b
db 0x38,0x5d,0x2c,0x61,0x6c,0x0d,0x0a,0x6c
db 0x6f,0x64,0x73,0x62,0x0d,0x0a,0x61,0x6e
db 0x64,0x20,0x61,0x6c,0x2c,0x31,0x35,0x0d
db 0x0a,0x61,0x64,0x64,0x20,0x61,0x6c,0x2c
db 0x34,0x38,0x0d,0x0a,0x63,0x6d,0x70,0x20
db 0x61,0x6c,0x2c,0x35,0x38,0x0d,0x0a,0x6a
db 0x62,0x20,0x65,0x0d,0x0a,0x61,0x64,0x64
db 0x20,0x61,0x6c,0x2c,0x33,0x39,0x0d,0x0a
db 0x65,0x3a,0x6d,0x6f,0x76,0x20,0x5b,0x62
db 0x2b,0x39,0x5d,0x2c,0x61,0x6c,0x0d,0x0a
db 0x6d,0x6f,0x76,0x20,0x61,0x68,0x2c,0x36
db 0x34,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x62
db 0x78,0x2c,0x31,0x0d,0x0a,0x6d,0x6f,0x76
db 0x20,0x63,0x78,0x2c,0x35,0x0d,0x0a,0x6d
db 0x6f,0x76,0x20,0x64,0x78,0x2c,0x62,0x2b
db 0x36,0x0d,0x0a,0x69,0x6e,0x74,0x20,0x33
db 0x33,0x0d,0x0a,0x70,0x6f,0x70,0x20,0x63
db 0x78,0x0d,0x0a,0x6c,0x6f,0x6f,0x70,0x20
db 0x61,0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61
db 0x6c,0x2c,0x5b,0x73,0x69,0x5d,0x0d,0x0a
db 0x73,0x68,0x72,0x20,0x61,0x6c,0x2c,0x34
db 0x0d,0x0a,0x61,0x64,0x64,0x20,0x61,0x6c
db 0x2c,0x34,0x38,0x0d,0x0a,0x63,0x6d,0x70
db 0x20,0x61,0x6c,0x2c,0x35,0x38,0x0d,0x0a
db 0x6a,0x62,0x20,0x68,0x0d,0x0a,0x61,0x64
db 0x64,0x20,0x61,0x6c,0x2c,0x33,0x39,0x0d
db 0x0a,0x68,0x3a,0x6d,0x6f,0x76,0x20,0x5b
db 0x62,0x2b,0x38,0x5d,0x2c,0x61,0x6c,0x0d
db 0x0a,0x6c,0x6f,0x64,0x73,0x62,0x0d,0x0a
db 0x61,0x6e,0x64,0x20,0x61,0x6c,0x2c,0x31
db 0x35,0x0d,0x0a,0x61,0x64,0x64,0x20,0x61
db 0x6c,0x2c,0x34,0x38,0x0d,0x0a,0x63,0x6d
db 0x70,0x20,0x61,0x6c,0x2c,0x35,0x38,0x0d
db 0x0a,0x6a,0x62,0x20,0x6e,0x0d,0x0a,0x61
db 0x64,0x64,0x20,0x61,0x6c,0x2c,0x33,0x39
db 0x0d,0x0a,0x6e,0x3a,0x6d,0x6f,0x76,0x20
db 0x5b,0x62,0x2b,0x39,0x5d,0x2c,0x61,0x6c
db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68
db 0x2c,0x36,0x34,0x0d,0x0a,0x6d,0x6f,0x76
db 0x20,0x62,0x78,0x2c,0x31,0x0d,0x0a,0x6d
db 0x6f,0x76,0x20,0x63,0x78,0x2c,0x34,0x0d
db 0x0a,0x6d,0x6f,0x76,0x20,0x64,0x78,0x2c
db 0x62,0x2b,0x36,0x0d,0x0a,0x69,0x6e,0x74
db 0x20,0x33,0x33,0x0d,0x0a,0x70,0x6f,0x70
db 0x20,0x63,0x78,0x0d,0x0a,0x6c,0x6f,0x6f
db 0x70,0x20,0x63,0x0d,0x0a,0x6d,0x6f,0x76
db 0x20,0x61,0x68,0x2c,0x36,0x34,0x0d,0x0a
db 0x6d,0x6f,0x76,0x20,0x62,0x78,0x2c,0x31
db 0x0d,0x0a,0x6d,0x6f,0x76,0x20,0x63,0x78
db 0x2c,0x35,0x0d,0x0a,0x6d,0x6f,0x76,0x20
db 0x64,0x78,0x2c,0x62,0x2b,0x31,0x31,0x0d
db 0x0a,0x69,0x6e,0x74,0x20,0x33,0x33,0x0d
db 0x0a,0x6d,0x6f,0x76,0x20,0x61,0x68,0x2c
db 0x37,0x36,0x0d,0x0a,0x69,0x6e,0x74,0x20
db 0x33,0x33,0x0d,0x0a,0x62,0x20,0x20,0x20
db 0x64,0x62,0x20,0x31,0x33,0x2c,0x31,0x30
db 0x2c,0x27,0x20,0x64,0x62,0x20,0x30,0x78
db 0x20,0x20,0x2c,0x27,0x2c,0x31,0x33,0x2c
db 0x31,0x30,0x2c,0x27,0x65,0x6e,0x64,0x27
end

There is a quines list, PM me for the url.

Oh, and I didn't write this. ASM is torture.

Incubator - November 17, 2003 08:30 PM (GMT)
QUOTE (Uraldor @ Nov 13 2003, 02:45 AM)
I could do it if I knew how to use C++ language to open a file on my computer. Does anyone know how to do that?

I can't see how that will help you solve the problem. You generally don't have the source code of your program available as a file when you're running your program, and opening the source file and blatting it to the screen is cheating ;)

So I disagree with you, you can't do it just by opening the exe. You need to be smarter.

OJ

PS. Yes, the forum is quite new, give it time and it'll build up! There are other programming challenges listed in there aswell if you're interested.

depends :P
I have about 16gb of both containing source code and the binaries, its quite easy though then, I just read through an ifstream from /usr/portage/distfiles/<packagename>-<versionnumber>/work
:P

but as usual, big nono for many wwindoze apps....



netcrusher88 - November 19, 2003 04:03 AM (GMT)
Yes, a C++ quine can be done. It's online.
Here's an easy one:
CODE



Obviously that's not ANSI C or C++, but try compiling it! It should work(I think)!

Incubator - November 19, 2003 10:15 PM (GMT)
i'd love to but i dont see any source :P

the code tags are empty

netcrusher88 - November 20, 2003 08:34 PM (GMT)
QUOTE (Incubator @ Nov 19 2003, 02:15 PM)
i'd love to but i dont see any source :P

the code tags are empty

That's the point: the program does exactly nothing and prints itself, which is nothing. Try it; that's the shortest quine known to man.

Incubator - November 20, 2003 11:10 PM (GMT)
clever ;)

RichyT - November 25, 2003 07:24 PM (GMT)
QUOTE (Kylevision @ Aug 14 2003, 07:45 PM)
My challenge to you is to try and to write a program that shows its own source.

CODE

int main()
{
  cout << "its own source.";
  system("pause");
  return 0;
}


its childish, its immature, its stupid... but i like it

TheHawgMaster - November 25, 2003 11:19 PM (GMT)
Awesome RichyT :lol:

ih8censorship - November 25, 2003 11:51 PM (GMT)
oh man!!!! i wish i would have thought of that first!!!!!

Uraldor - November 26, 2003 02:02 AM (GMT)
QUOTE
depends :P
I have about 16gb of both containing source code and the binaries, its quite easy though then, I just read through an ifstream from /usr/portage/distfiles/<packagename>-<versionnumber>/work
:P

but as usual, big nono for many wwindoze apps....


Hehe... well being a Gentoo user doesn't necessarily count ;) Besides, if you ship your program to another gentoo installation it might not have the distro downloaded.. so it wouldn't work..

.. but I digress :) It's a good point :D

Dragon - November 26, 2003 02:23 AM (GMT)
RichyT, that's funny. :lol:

RichyT - November 30, 2003 01:43 PM (GMT)
lol :huh:

abhishek - March 25, 2005 10:04 PM (GMT)
CODE
#include<stdio.h>
void main()
{char*s="#include<stdio.h>%c void main(){char*s=%c%s%c;printf(s,10,34,s,34,10);}%c";
printf(s,10,34,s,34,10);
}


i can understand the whole program but i didnt get what this printf(s,10,34,s,34,10)
since what i learned was that printf usually was used as something like print("blah");
etc. but all i code make out was that this 34 10 and 10 34 was doing some type converion but i dont know what this 10 34 means :wacko:

any help would be greatly appreciated

:)

myork - March 25, 2005 10:24 PM (GMT)
QUOTE (abhishek @ Mar 25 2005, 05:04 PM)
CODE
#include<stdio.h>
void main()
{char*s="#include<stdio.h>%c void main(){char*s=%c%s%c;printf(s,10,34,s,34,10);}%c";
printf(s,10,34,s,34,10);
}


i can understand the whole program but i didnt get what this printf(s,10,34,s,34,10)
since what i learned was that printf usually was used as something like print("blah");
etc. but all i code make out was that this 34 10 and 10 34 was doing some type converion but i dont know what this 10 34 means :wacko:

any help would be greatly appreciated

:)

Ask yourself what is the value of 's'

Then ask why does it look funny.
Then read the man page for printf()

abhishek - March 26, 2005 03:13 PM (GMT)
i am usin windows not unix :( .
but i finally got my answer thnx a lot, looks like someone needs to really kick me to get wrk done arnd here ;)

Shackleb0lt - March 27, 2005 12:36 AM (GMT)
how can you say writing a program that shows its code is impossible? you just put it in cout!!1!11!1one11!elevenhundreteleven!1

Shackleb0lt - March 27, 2005 12:54 AM (GMT)
um, during the making of my program I encountered a logical error..... you ABSOLUTELY CANNOT display the console the whole program, since you would have to write everything that's in it, even the part where you display the program, but when you write and you see it on the console, you realize the code where output the program is not in the console, where the whole program apparently is. Then you write that part again, so in the console you don't only see the program, but ALSO the code where you output the code. But then you realize that the part that outputs the part that outputs (this repetition is NOT a typing mistake) is not in the program.
SO, the part where you output the source code can never be written. It is phisically impossible to do it. You can, however, output everything else. :D

Shackleb0lt - March 27, 2005 01:05 AM (GMT)
violá
CODE

#include <iostream>
using namespace std;

int main(){
   int a = 10, b = 11;
   int sum = a + b;
   cout<<"The sum is "<<a + b<<"\n\n";
   cout<<"I am a program made by Shackleb0lt and I show my source:\n\n"
   //the next would be repetition    
         "#include <iostream>\n"
         "using namespace std;\n\n"
         "int main(){\n"
         "    int a = 10, b = 11;\n"
         "    int sum = a + b;\n";
         cout<<"    cout<<\"The sum is \"<<a + b<<\"\\n\\n\";\n"
         "    cout<<\"I am a program made by Shackleb0lt and I show my source:\\n\\n\"\n"
         "    //the next would be repetition\n"
         "cin.get();\n"
         "return 0;\n"
         "}";
                 
   cin.get();    
   return 0;
}


DeAs91 - March 27, 2005 01:10 AM (GMT)
Piece a cake:

CODE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   ifstream in("main.cpp"); // The name of the source file
   string text;
   string line;
   while(getline(in, line))
   {
       text.append(line);
       text.append("\n");
   }
   cout << text;
   return 0;
}

donprogc++ - March 27, 2005 01:15 AM (GMT)
Nice DeAs91 B)

DeAs91 - March 27, 2005 01:39 AM (GMT)
QUOTE (donprogc++ @ Mar 27 2005, 01:15 AM)
Nice DeAs91 B)

:D




* Hosted for free by InvisionFree