• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

mini programming challenge! if you prgram at all, come try this, it's more fun than hard.

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

joohang

Lifer
Oct 22, 2000
12,340
1
0
I'll try this just for hell of it in C#. :)

In a few hours, though. I need to cook some food and drink some coffee. Gotta prepare to watch the late-night ballgame tonight. :D
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Ameesh
Originally posted by: notfred
ohhh what about numbers larger then a float can handle?
We won't worry about those, or extremely large integers, either.

lol, dont like youre program breaking do yah? :):p

I thought it'd just be fair to keep the numbers within the range of standard variable types, like 32 bit integers and 2 byte floating point numbers. The intention was never to round off 12341234234634576345645675784846375673.3455674674567456 to the nearest hundred thousandth :)

Why write one if it is built in and works well already?
Cause it's more fun that way.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Beau6183
Originally posted by: notfred
Why write one if it is built in and works well already?
Cause it's more fun that way.

yes, but if you have the ability, wouldn't it be easier/shorter/less time consuming to use it?

I win! ;):p

It'd be easier to not write this program at all since it really has no use, but that's not the point....
 

Chu

Banned
Jan 2, 2001
2,911
0
0
Blargh, if you want a real challange, try to solve this classic problem:

Write a program that will print out its own source code.

Anyone who has taken a class in compilers, don't ruin it ;)
 

wolf550e

Golden Member
May 22, 2000
1,370
0
0
Originally posted by: xirtam
Such perls of wisdom...

LOL. I don't think php4 would have been interesting as a challenge, and I won't be coding it in x86 assembly unless I'm paid for it...
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
Originally posted by: Chu
Blargh, if you want a real challange, try to solve this classic problem:

Write a program that will print out its own source code.

Anyone who has taken a class in compilers, don't ruin it ;)
Are you talking about printing the source code in the language it was originally written in by decompiling it? or just load up the source file and then output it to the printer?
 

crystal

Platinum Member
Nov 5, 1999
2,424
0
76
Originally posted by: Chu
Blargh, if you want a real challange, try to solve this classic problem:

Write a program that will print out its own source code.

Anyone who has taken a class in compilers, don't ruin it ;)

#!c:/perl/bin/perl.exe -w
use strict;

open (F, "$0");
print <F>;
close(F);

 

Chu

Banned
Jan 2, 2001
2,911
0
0
>>

Are you talking about printing the source code in the language it was originally written in by decompiling it? or just load up the source file and then output it to the printer?

<<

Printing the source code to stdout. In some languages this is trivial (ahem, that perl example). In most, it is not.

-Chu

P.S., try this in Java . . .
 

Lithium381

Lifer
May 12, 2001
12,452
2
0
i could do it in pascal, the best language ever invented~! :D so give me a few minutes and i'll have it ready, bear with me....bbl
 

Chu

Banned
Jan 2, 2001
2,911
0
0
>>

#!c:/perl/bin/perl.exe -w
use strict;

open (F, "$0");
print <F>;
close(F);

<<

Blargh, I am tired. A little revision to the challenge. The purpose of this challenge is to get a *COMPILED* program that will print out its source. The trivial answer, to print out a file containg the source, does not satisfy this. BTW, if you look at a book on compilers, this problem is related to the 'acid test' of mature languges, which is to get a compiler to compile itself.

-Chu
 

Lithium381

Lifer
May 12, 2001
12,452
2
0
man that's hard...., well harder than i though, i'm going to give up for now, it's frustrating when i get random numbers....
 

ajayjuneja

Golden Member
Dec 31, 2001
1,260
0
76
Alright Chu, how's this? This is in C -- compiled under gcc :)


-----------------------------------------
#define MAX_PRG 5000

#include <iostream.h>
#include <stdio.h>

char program[] = "\n"
"#define MAX_PRG 7000\n"
"#include <string.h>\n"
"void cat(char *name, char *dst, char *src) {\n"
"\n"
" strcat(dst,\"\\nchar \");\n"
" strcat(dst,name);\n"
" strcat(dst,\"[] = \\\"\"0;\n"
" for(int i = 0;src;i++) {\n"
" switch(src) {\n"
" case \'\\n\': strcat(dst,\"\\\\n\\\"\\n \\\"\"); break;\n"
" case \'\\\\\': strcat(dst,\"\\\\\\\\\"); break;\n"
" case \'\\\'\': strcat(dst,\"\\\\\\\'\"); break;\n"
" default: strncat(dst,src+1,1);\n"
" }\n"
" }\n"
" strcat(dst,\"\\\";\\n\");\n"
" strcat(dst,src);\n"
" return;\n"
"}\n"
"\n"
"int main( int argc, char** argv) {\n"
" char arg[MAX_PRG];\n"
"\n"
" arg[0] = \'\\0\';\n"
"\n"
" cat(\"eatArr\",arg,eatArr);\n"
" \n"
" cat(\"program\",arg,program);\n"
"\n"
" Eat(arg);\n"
" exit(0);\n"
"\n"
"";

void print(char *name, char *arr) {
cout << "\nchar " << name << "[] = \"";
for(int i=0; arr; i++) {
switch (arr) {
case '\n': cout << "\\n\"\n \""; break;
case '\\': cout << "\\\\"; break;
case '\"': cout << "\\\""; break;
case '\'': cout << "\\\'"; break;
default: cout << arr; break;
}
}
cout << "\";\n" << arr;
return;
}

void ACM(char *eat)
{
print("eatArr",eat);
print("program",program);
}

int main(int argc, char **argv) {
char PROGRAM[MAX_PRG+1];
char c;
int i=0;

while((i < MAX_PRG) && (c = getchar()) != EOF) {
PROGRAM[i++] = c;
}
PROGRAM = '\0';

ACM(PROGRAM);
exit(0);
}
 

Chu

Banned
Jan 2, 2001
2,911
0
0

>>

Blargh, I am tired. A little revision to the challenge. The purpose of this challenge is to get a *COMPILED* program that will print out its source. The trivial answer, to print out a file containg the source, does not satisfy this. BTW, if you look at a book on compilers, this problem is related to the 'acid test' of mature languges, which is to get a compiler to compile itself.

<<

I guess compiled is a bad word here since interpreted languages like perl arn't exactly compiled (though they are . . . blargh, you know what I mean). Basically a solution that does this in some way other then just reading a file with the source in it. Oh yeah, here are a whole lot of solutions for those of you who give up :D When i was in intro CS, the solution took me about a week to figure out though, which I guess is more time then most people who do not have to do it are willing to spend if they have more important things to do.

-Chu
 

Chu

Banned
Jan 2, 2001
2,911
0
0
>> Alright Chu, how's this? This is in C -- compiled under gcc

Wondering, what version of gcc? 2.95.2 gives a slew of error messages. Forte Workshop was a little more forgiving, but it still gave two errors and some warnings:

"test.cc", line 44: Error: Switch selection expression must be of an integral type.
"test.cc", line 58: Warning: String literal converted to char* in formal argument name in call to print(char*, char*).
"test.cc", line 59: Warning: String literal converted to char* in formal argument name in call to print(char*, char*).
"test.cc", line 70: Error: The operand "*PROGRAM" cannot be assigned to.
2 Error(s) and 2 Warning(s) detected.

I assume you ment switch(*arr) for line 44, but that for loop looks funky (until arr is not null?). For line 70, well, if it didn't give an error I would read that as 'Set the pointer PROGRAM to NULL' which I really don't think is what you mean.