A Coding Challenge - Write An Absolutely USELESS Program

Chrysalid

Newbie
Joined
Jun 13, 2004
Messages
160
Reaction score
0
Hi, i'm announcing a small challenge for any coders around the forums. Write some code and post it here. Rules:

The code must be ABSOLUTELY USELESS, and must be written in C or C++. The more '[WARNING] code has no effect' lines, the better :D. It must do SOMETHING, but it must be useless at the same time. Everything else is up to you.

I came up with this idea as i examined the Annual Most Obfusecated C Code contest lately, and i hope you are as eager to try things as i am. There is no specific winner, however, consider this as a challenge. I'm sure writing completely useless code is definitely as hard as writing effective code.

Let the game begin!
 
heh well let me offer up the classic bit of useless code:


printf("Hello World!");

And in C++

cout << "Hello World!";
 
Code:
int shouldLoop = 0;
while ( shouldLoop )
	cout << "Never get here..." << endl;

for ( int x = 10; x < 0; x--; )
	cout << "x = " << x << ", but this won't show." << endl;

if ( !!!!shouldLoop )
	cout << "Compounding negatives is fun!" << endl;

Is this the type of stuff you're looking for?
 
hre's a uselesss bit of code:
Code:
	int x = 2, y = 1, z = 4;

	while (x < z)
	{
		z--;
		
		while(y < 1)
		{
			if ((z > x + y) && (true))
			{
				for (int i = 12; i <= y*z+(y*x/z*y); i += (z - y))
				{
					if (!!!true || !!false)						
						cout << "woops!" << endl;
				}
			}
			z++;
			y *= x + (z + 12/4);
		}

		z++;
		x+=y;
		y = z / 2;

	}
 
is it fair to submit homework assignments? I tend to hink 99% of them are totally useless
 
I just finished a new program of mine that automatically crawls porn sites and download all their images/movies. But I guess that's not pointless is it? :E

EDIT - removed attachment. feel free to post a new pic, but scribble out the porn links first please :)
 
Bah, cba now! Sufficed to say, I'm quite proud of it. I intend on selling it to people at my university. I'm gonna be RICH!
 
well this could go forever ..
Code:
struct mpt
{
	char a;
	int b;
	float c;
	double d;
};

class noop
{
public:
	noop();
	~noop();

	int a;
	int b;
	int c;
	int x;
	int y;
	int z;
	double h;
	double k;

	char *nul;
	float fl1;

	mpt erw;
	mpt yuRg5;

	void wt1();
	int ckt(int e, char i);

};

noop::noop()
{
	int drf = 10;
	double egf = 13.234;

}

noop::~noop()
{
	z = y + (b = 10);

}

void noop::wt1()
{
	c = 10;
	z = y = a = x = c = b = h = fl1 = 12;

	nul = "what a waste of memory .. no really, it is";
}

int noop::ckt(int e, char i)
{
	char *d = (char *)int( (void *)(nul + 5) );

	return int(d + 2);
}
 
Code:
    void Endian( wchar_t* charBuffer, unsigned int numChars )
    {
        char* byteBuffer = reinterpret_cast<char*> ( charBuffer );
    
        for ( register unsigned int i = 0; i < numChars; i++ )
        {
            char temp = byteBuffer[i * 2];
            byteBuffer[i * 2] = byteBuffer[(i * 2) + 1];
            byteBuffer[(i * 2) + 1] = temp;
        }

        for ( register unsigned int i = 0; i < numChars; i++ )
        {
            char temp = byteBuffer[i * 2];
            byteBuffer[i * 2] = byteBuffer[(i * 2) + 1];
            byteBuffer[(i * 2) + 1] = temp;
        }
    }
 
I once wrote a program to compute the number of telephone numbers you can have in a area code.. was gonna do the same for license plates but never got around to it...

-Limb
 
Here's my uber useful program (gotta love __declspec(naked)):
Code:
[FONT=Courier New]
[COLOR=Blue]#include[/COLOR] <stdio.h>
[COLOR=Blue]#include[/COLOR] <tchar.h>
[COLOR=Blue]#include[/COLOR] <malloc.h>

[COLOR=Blue]char[/COLOR] *str_1 = "The string has a length of %d %s\n";
[COLOR=Blue]char[/COLOR] *str_2 = "characters!";
[COLOR=Blue]char[/COLOR] *str_3 = "Press any key to continue...\n";
[COLOR=Blue]char[/COLOR] *str_4 = " ";
[COLOR=Blue]int __declspec[/COLOR]([COLOR=Blue]naked[/COLOR]) _tmain([COLOR=Blue]int[/COLOR] argc, _TCHAR* argv[])
{
	[COLOR=Blue]_asm[/COLOR]{
		[COLOR=Green]//Epilog[/COLOR]
		push		ebp
		mov		ebp, esp
		sub		esp, __LOCAL_SIZE
		
		xor		eax, eax
		[COLOR=Green]//jump if eax is 0[/COLOR]
		test		eax, eax
		jz		loc_jz_1
        
		[COLOR=Green]//get the string length[/COLOR]
		push		str_2
		call		strlen
		add		esp, 4
		mov		edx, eax
		add		edx, 1 [COLOR=Green]//termination character[/COLOR]
		[COLOR=Green]//and allocate memory for string copy[/COLOR]
		push		edx
		call		malloc
		pop		edx
		test		eax, eax
		mov		ebx, eax
		jz		loc_jz_1

		[COLOR=Green]//copy the string to the allocated memory if pointer != NULL[/COLOR]
		mov		esi, str_2
		mov		edi, ebx
		mov		ecx, edx
		rep		movs
        
	[COLOR=Green]	//print the text using the format string,
		//strlen result and the pointer with the
		//copied string[/COLOR]
		push		ebx
		sub		edx, 1 [COLOR=Green]//termination character [/COLOR]
		push		edx
		push		str_1
		call		printf
		add		esp, 0ch
        
		[COLOR=Green]//print "Press any key to continue..."[/COLOR]
		push		str_3
		call		printf
		add		esp, 4
        
		[COLOR=DarkGreen]//wait for user input (enter key)[/COLOR]
		push		str_4
		call		scanf
		add		esp, 4

		[COLOR=Green]//free the allocated memory[/COLOR]
		push		ebx
		call		free
		add		esp, 4

	[COLOR=Green]	//jump here on error[/COLOR]
loc_jz_1:		

		[COLOR=Green]//prolog[/COLOR]
		mov		esp, ebp
		pop		ebp
		sub		esp, 10h
		add		esp, 4
		retn		0ch
	}
}[/FONT]
 
Useless code, eh?

*thinks*

Code:
int x = 1;

while(x=1)
{
	x = 0;

	while(x=0)
	{
		x=1;
	}
}

Does absolutely bugger-all, but it still runs.

-Angry Lawyer
 
that's not useless, it fills the CPU usage in the al+ctrl+delete graph.
 
Really? By what I see, all it does is enter an endless loop, using only one variable. A simpler way would be something like

Code:
int x = 1;

while (true != false)
{
x = 0;
x = 1;
}

Does (practically) the same thing - switch the value of x between 1 and 0.

-Angry Lawyer
 
I know ;) just being picky. :)

it's not so useless if you think of it as some malware.
 
THIS code is so useless! it wont even WORK!!

Code:
int end
quit
//      exit
half
life
int   int
< 2
int
 
*ponders* But with that piece of code compiled as a program, it'd simply bring up a DOS prompt, and sit there. You could easily press the 'cross' button at the top of the window. See? Useless as Malware too. Unless you run it in actual DOS - then you're stuffed.

-Angry Lawyer
 
Code:
{
	char duh = 0;

		cout << "Enter a character: ";
		cin >> duh;

		cout << endl
			 << "You entered a: " << duh
			 << endl;

return 0;
}

Phear.
 
Code:
/*  Programmer: Gorgon
	functions to find the greatest common divisor of 2
	integers. The test program should read pairs of numbers 
	from the standard input and call gcd each time.
*/

#include <iostream.h>
	using namespace std;

/*This function procude the GCD without the calculations output*/
void GCD(int a, int b)
{
	int r;
	cout<<"-----------------------"<<endl;
	cout<<"GCD("<<a<<","<<b<<")"<<"= ";
	while(r!=0)
	{
		r = a % b;
		a = b;
		b = r;
	}
	cout<<a<<endl;
}

/*This funtion output the calcution of the GCD numbers*/
void GCDoutput(int a, int b)
{
	int r, q;

	while(r!=0)
	{
	    q = a / b;
		r = a % b;
		cout<<a<<"= "<<q<<" * "<<b<<" + "<<r<<endl;
		a = b;
		b = r;
	}
}

//Main programe
int main()
{
	int n1=0, n2=0;
	
	cout<<"Enter the First Number: ";
	cin>>n1;
	cout<<"Enter the Secon Number: ";
	cin>>n2;
	cout<<"-----------------------"<<endl;
	GCDoutput(n1,n2);
	GCD(n1,n2);
}

the above is a working code I made to get the GREATEST COMMON DIV of 2 numbers

took me 10 mins to do it, I hop you find it usefull. :D
 
Nah, that's something wrong with his link. There is a C++ program if can manage to download it.(had to get someone to dcc to me in IRC).
 
Strange - when there is a referrer it returns you to that page, if you start up a new instance of your browser and go to the link, it works.

edit:

Code:
case 97:
    cout<<"A is for: Another great achievement by Dan\n";
    break;

err, why not case 'a' ?????
 
I wrote a Visual Basic (not C, I'm afraid) program called "Gangsta Speak". It's a little 2-second job designed to mirror the linguistic patterns of a typical gagsta. I'll upload it if anyone is interested, I guess.
 
When I take a look at my friend's code at least 40% is redundant. :lol:
 
simple counter...

Code:
#include<iostream>

using namespace std;

void main(void) {

	int x = 0;
	char ch = 'n';

	while (ch == 'n') {
		x++;
		cout << x << endl;
		cout << "quit?";
		cin >> ch;
	}

}
 
I'll have a try at a useless prog

Code:
#include <iostream.h>

void main()
{
    char answer;
    cout << "How are you today?";
    cin >> answer;
    cout << "I don't care!";
}
 
^ the answer would no doubt be a string, not a character, hence you would need to make it
char answer(100) and
cin.getline (answer, 100, '\n')


EDIT: granted itd work your way to, since its not meant to actually do anything, im just being picky :p
 
Saw this idea in one of the appendices to Stroustrup's C++ book, and I was on the floor laughing:
Code:
bool n = !     !  !   !  !     !
        ! !   !  !   !  !     !
       !   ! !  !   !  !     !
      !     !  !!!!!  !!!!  !!!1; 

if (!n)
    // (do something useful here)
This gives the words "self-documenting code" an entirely new meaning. :)
 
the ultimate cpu resource waster:
Code:
void main()
{
	while(1)
	{
		 ;
	}
}

kewl?
 
this thread really isn't a coding challenge.. anyone can make a program do nothing.
 
Code:
int mystery(int a, int b){
    if((a % b) == 0){
        printf("%i", b);
    }
    else{
        mystery(b, (a % b));
    }
}

so here's a challenge, what does this print out when given 2 integers?

and here's my useless submission, it's a tree, that can hold...characters.
Code:
#include <string.h>
#include <stdio.h>

typedef struct binaryTree{  //This is our binary tree structure.
        char c;
        struct binaryTree *left;
        struct binaryTree *right;
} tree;
void insert(tree *, char);
void print(tree *);
void reset(tree *);
tree* makeNode(char);

int main(void){
    FILE *fp;
    char *string = (char *) malloc(80*sizeof(char));
    int i=0, j=0, k;
    tree *t = makeNode(NULL);  //This creates the root of our tree.
    while(1){
        printf("Choose an option(integer):\n1. Read in a string.\n2. Read from a file.\n3. Print the tree.\n4. Reset the tree.\n5. Quit.\n");
        scanf("%i", &i);  //This takes in the user's selection.
        while(getc(stdin) != '\n');  //This flushes out stdin.
        if(i == 1){  //Did the user select to read in a string?
            printf("Input a string to be read in.\n");
            fgets(string, 80, stdin);  //Get the string from user and store in string.
            while(*(string+j) != '\n'){
                insert(t, *(string+j));  //Insert each character into the tree.
                j++;
            }
            j=0; //Reset j to 0.
        }
        else if(i == 2){ //Did the user select to read in from a file?
            printf("Input the file name to be read in.\n");
            scanf("%s", string);  //Scan the file name into string.
            while(getc(stdin) != '\n');
            if((fp = fopen(string, "r")) == NULL){ //Was the file able to be opened?
                printf("\nUnable to open file.\n\n");
                continue;
            }
            while(fgets(string, 80, fp)){
                for(k=0; k<(strlen(string)-1); k++){
                    insert(t, *(string+k));  //Insert each character in the tree.
                }
            }
            close(fp);  //Close the file.
        }
        else if(i == 3){  //Did the user select to print the tree?
            printf("\n");
            print(t);  //Prints each character in the tree.
            printf("\n");
        }
        else if(i == 4){  //Did the user select to reset the tree?
            reset(t); //Resets the tree.
            t = makeNode(NULL);  //This creates our new root.
        }
        else{  
            break; //Quit.
        }
        i=0;  //Reset i to avoid some possible errors.
        printf("\n");
    }
    reset(t); //reset is used to free up all our allocated space at program termination.
    return 0;
}

//insert takes in a pointer to a tree, and a character, then inserts that
//character into the proper spot in the tree by using recursion.
void insert(tree *p, char i){
    tree *q;
    if(p->c == NULL || p->c == i){  //Is the tree empty, or does the character already exist at this node?
        p->c = i;
    }
    else if(p->c > i){  //Is the character to insert less than the character at this node?
        if(p->left == NULL){ //Is this spot available?
            q = makeNode(i);
            p->left = q;
        }
        else{  //Then repeat process on the left tree.
            insert(p->left, i);
        }
    }
    else{ //Is the character to insert greater than the character at this node?  
        if(p->right == NULL){
            q = makeNode(i);
            p->right = q;
        }
        else{
            insert(p->right, i);
        }
    }
}

//print takes in a pointer to a tree, then prints each character stored
//in the tree by using recursion.
void print(tree *p){
    if(p == NULL){ }
    else{
        print(p->left);
        printf("%c", p->c);
        print(p->right);
    }
}

//reset takes in a pointer to a tree, then frees up(deletes) all of the
//nodes of the tree by using recursion.
void reset(tree *p){
    if(p == NULL){ }
    else{
        reset(p->left);
        reset(p->right);
        free(p);
    }
}

//makeNode takes in a character, then creates a new tree structure using c
//as its stored character, and initializes right and left to NULL, then
//returns its pointer.
tree* makeNode(char c){
    tree *newNode = (tree *) malloc(1*sizeof(tree));
    newNode->c = c;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}


sorry for the mess.
 
I'm guessing it's either Euclidean's algorithm or the lowest common factor (or something like that). Been a while since I did any maths at all so...
 
#ifndef NODE_H
#define NODE_H

template<class nodetype>
class node
{
public:
node(const nodetype value = NULL);
~node();
nodetype getData(void)const;
node<nodetype> *getNextPtr(void)const;
node<nodetype> *getPrevPtr(void)const;
void setData(const nodetype value);
void setNextPtr(node<nodetype> *nPtr);
void setPrevPtr(node<nodetype> *pPtr);
private:
node<nodetype> *prevPtr;
node<nodetype> *nextPtr;
nodetype dataValue;
};

template<class nodetype>
node<nodetype>::node(const nodetype value = NULL)
{
dataValue = value;
nextPtr = NULL;
prevPtr = NULL;
}

template<class nodetype>
node<nodetype>::~node()
{
//Destructor ... does nothing so far.
}

template<class nodetype>
nodetype node<nodetype>::getData(void)const
{
return dataValue;
}

template<class nodetype>
node<nodetype> *node<nodetype>::getNextPtr(void)const
{
return nextPtr;
}

template<class nodetype>
node<nodetype> *node<nodetype>::getPrevPtr(void)const
{
return prevPtr;
}

template<class nodetype>
void node<nodetype>::setData(const nodetype value)
{
dataValue = value;
}

template<class nodetype>
void node<nodetype>::setNextPtr(node<nodetype> *nPtr)
{
nextPtr = nPtr;
}

template<class nodetype>
void node<nodetype>::setPrevPtr(node<nodetype> *pPtr)
{
prevPtr = pPtr;
}

#endif
/*
was my homework assignment so i deem it pretty darn useless! yea!! ^_^
*/

int main()
{
node<int> useless(0);
return 0;
}
 
Ok, beat this :)

(Its not hard by the way ;) )

1MB Zipped of code and images in a working program with no errors that does sod all in Java.

I added 3 lines of code to make it do nothing, Its pretty much totaly uncommented and realy does nothing :)

Ill give a Mars Bar to anyone that can get the 3 lines of code out of it and make it work :)

http://lamppost.mine.nu/ahclan/files/Pointless.zip
 
Silent Kane said:
Here's my uber useful program (gotta love __declspec(naked)):
Code:
[FONT=Courier New]
[COLOR=Blue]#include[/COLOR] <stdio.h>
[COLOR=Blue]#include[/COLOR] <tchar.h>
[COLOR=Blue]#include[/COLOR] <malloc.h>

[COLOR=Blue]char[/COLOR] *str_1 = "The string has a length of %d %s\n";
[COLOR=Blue]char[/COLOR] *str_2 = "characters!";
[COLOR=Blue]char[/COLOR] *str_3 = "Press any key to continue...\n";
[COLOR=Blue]char[/COLOR] *str_4 = " ";
[COLOR=Blue]int __declspec[/COLOR]([COLOR=Blue]naked[/COLOR]) _tmain([COLOR=Blue]int[/COLOR] argc, _TCHAR* argv[])
{
	[COLOR=Blue]_asm[/COLOR]{
		[COLOR=Green]//Epilog[/COLOR]
		push		ebp
		mov		ebp, esp
		sub		esp, __LOCAL_SIZE
		
		xor		eax, eax
		[COLOR=Green]//jump if eax is 0[/COLOR]
		test		eax, eax
		jz		loc_jz_1
        
		[COLOR=Green]//get the string length[/COLOR]
		push		str_2
		call		strlen
		add		esp, 4
		mov		edx, eax
		add		edx, 1 [COLOR=Green]//termination character[/COLOR]
		[COLOR=Green]//and allocate memory for string copy[/COLOR]
		push		edx
		call		malloc
		pop		edx
		test		eax, eax
		mov		ebx, eax
		jz		loc_jz_1

		[COLOR=Green]//copy the string to the allocated memory if pointer != NULL[/COLOR]
		mov		esi, str_2
		mov		edi, ebx
		mov		ecx, edx
		rep		movs
        
	[COLOR=Green]	//print the text using the format string,
		//strlen result and the pointer with the
		//copied string[/COLOR]
		push		ebx
		sub		edx, 1 [COLOR=Green]//termination character [/COLOR]
		push		edx
		push		str_1
		call		printf
		add		esp, 0ch
        
		[COLOR=Green]//print "Press any key to continue..."[/COLOR]
		push		str_3
		call		printf
		add		esp, 4
        
		[COLOR=DarkGreen]//wait for user input (enter key)[/COLOR]
		push		str_4
		call		scanf
		add		esp, 4

		[COLOR=Green]//free the allocated memory[/COLOR]
		push		ebx
		call		free
		add		esp, 4

	[COLOR=Green]	//jump here on error[/COLOR]
loc_jz_1:		

		[COLOR=Green]//prolog[/COLOR]
		mov		esp, ebp
		pop		ebp
		sub		esp, 10h
		add		esp, 4
		retn		0ch
	}
}[/FONT]

:eek: I am sorry to say I don't understand a part of this code.
int __declspec(naked) _tmain(int argc, _TCHAR* argv[])
How does this work? Note that I don't have too much practical C++ experience, only theoretical. And what does declspec do?
 
Kozak said:
:eek: I am sorry to say I don't understand a part of this code.
int __declspec(naked) _tmain(int argc, _TCHAR* argv[])
How does this work? Note that I don't have too much practical C++ experience, only theoretical. And what does declspec do?

half of it is assembly
 
Code:
import java.util.Random;

public class SelectionSort          
            
{       
            int max = 10;        
            Random generator;     
            private int[] x;    
   
   
public SelectionSort()
            
        {   
            x = new int[10];     
        }
   
   
   
   
public void checkruntime()  

        {
            long start, stop;
            start = System.currentTimeMillis();
            display();
            stop = System.currentTimeMillis();
            System.out.println("Runtime: " +(stop-start)+ " milliseconds" );  
        } 
  
   
   
public void display()                 

        { 
            int temp;                 
            int select;
            for (int i =0; i <= 9; i ++)   
        {
            x[i]=generator.nextInt(max);           
            System.out.println("Value:"+ x[i]);    
            System.out.println();                  
        }
         
            for (int i = 0; i <= 9; i++) {          
            select = i;                                 
            for (int j = i + 1; j < 10; j++)        

        {
             if (x[j] < x[select]) 
             {                                       
               select = j;                              
             }
         }
        
        temp = x[select];                       
        x[select] = x[i];                       
        x[i] = temp;                            
        System.out.println("Value:"+ x[i]);

        }
    
   }

}



that's a bit of java code that does a selection sort. it generates a set of random numbers between 0-10, and sorts them in numerical order and gives you a runtime in milliseconds. behold it's ultimate usefulness!
 
Back
Top