Jump to content

Programming Challenge 1


cold_snipe

Recommended Posts

Hey everyone!

 

I've been reading many posts about people not knowing what to program, and decided to make this topic. Anyone who wants can submit their code about a program idea, which someone gives. As I made this topic I will chose now the topic of this "challenge".

The program you write must make a text file with all letter combinations possible, you enter how many letters it will have. So one example of what it would look like would be:

Welcome to someone's word thingie generator.
Please enter the number of letters in the words: 8
Thank you, working...
10%..20%..30%..40%..

 

The text file it will generate will be:

aaaaaaaa
aaaaaaab
aaaaaaac
...
aaabhsod
...
zzzzzzzzy
zzzzzzzzz

 

So you get it. Of course 8 would take forever, but lets say it should work with 3. It will be a contest as the best, cleanest, fastest, most commented, and smartest (etc.) code will be the winner. They will win a cookie, and respect :D.

Here are some rules I would have:

1. No code stealing, all of it must be written by you.

2. Include all custom headers/etc.

3. You can use ANY programming language, but C++ is preferred (by me ^_^).

4. Include how long it took you to write, if you can.

5. Have fun!

 

 

 

So this is my idea, please feel free to comment or participate in it, I hope im not breaking any rules with this, i <3 you mods. I'm still not sure how I(?) will chose a winner, maybe people will vote. Oh yea, you can submit code only once, and this contest will end in 1 month, September 6th.

 

-cold_snipe

Edited by cold_snipe

Share this post


Link to post
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

Wow. Couldnt we have started with something a tad bit easier....

 

 

and a quick question before I get started on this. You want us to write a program taht will create a .txt file that contains all of the possible 3 letter combination words?

 

Right.

Share this post


Link to post
Share on other sites

is there a prize? :D

523895[/snapback]

Mhmm, a cookie. :D

 

 

And archerzz, it is not THAT hard, and with my example i think i already made one way clear. Yes you have the question right :D I myself might write one too, but I wont enter this contest (Hmm I wonder why, lol).

Well have fun writing code, everyone!

Edited by cold_snipe

Share this post


Link to post
Share on other sites

Too bad markie's the only one here that I know of to be good with file I/O.

523876[/snapback]

 

C++ file I/O is a LOT easier than you might think. All you have to do is declare an fstream object (named something like fout, whatever you like really). The declaration determines if it's read/write and whether it's append or insert style. Then you write to it just like you would cout. Then instead of going to the screen output, it goes to the file.

Share this post


Link to post
Share on other sites

Well, considering that I just started learing by teaching myself afew days ago and have not even figured out the functions for each library. This will kinda be hard for me... lol. But I am trying neways.

 

Never Give up Never Surrender!!11!111!!!!

Share this post


Link to post
Share on other sites

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
 
  ofstream fout("words.txt", ios::trunc);
 
  if (!fout) // Always test file open
       {
       cout << "Error opening output file" << endl;
       getchar (); // wait for input
       return -1;
       }
  
  for(int i=97;i<123;i++)
  {
       for(int j=97;j<123;j++) 
       {
               for(int k=97;k<123;k++)
               {
                       fout << char(i) << char(j) << char(k) << endl;
               }
       }
  }
 
  fout.close();
  
  cout << ("Press ENTER to continue.\n");
  getchar (); // wait for input
  
  return 0;
  
}

 

There you have it. That does three letters by default, to a file called "words.txt" in the program directory. Still working on more features :P

 

EDIT#

Added .exe in zip file

Share this post


Link to post
Share on other sites

Ok, I've got it so it asks you for the number of letters when it runs. Also, it reports the % complete, since when you run it for more than 5, you start wondering if it froze because it takes so long :P

 

I had to use dynamically allocated integer arrays as well as a recursive function call. Definately not easy, but one hell of a good challenge. That was actually pretty fun.

 

### Be warned, opening the resulting text file with 5 or more letters usually doesn't go well. They're huge files. :P

 

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <new>

using namespace std;

int main()
{
  int len;
  int count;
  int* dynword;
  int timer;
  int oldtimer=1;
  
  ofstream fout("words.txt", ios::trunc);
  int checkfunc(int*,int,int&);  
 
  if (!fout) // Always test file open
       {
       cout << "Error opening output file" << endl;
       getchar (); // wait for input
       return -1;
       }
    
  cout << "Enter the length of the words to create: ";   
  cin >> len;
  count = len - 1;          //set count to point at last slot in dyn array
   
  dynword = new int[len];
   
  for(int i=0;i<len;i++)       //Initialize "all a's" array
          dynword[i] = 97;     //
                                
  len = len -1; // subtract 1 for working with arrays (0 -> len-1)
  
  while (count >= 0)
  {
        
          for(int i=0;i<(len+1);i++)
                  fout << char(dynword[i]);         
          
          if (dynword[len] == 122)
          {
                  timer = checkfunc(dynword,len,count);         
                  if (timer > oldtimer)
                  {
                          cout << (timer - 1) * 4 << "% done.\n";
                          oldtimer = timer;  
                  }
                    
          }                     
          else
                  dynword[len]++;

 fout << endl;
 
 } // end while
 
  fout.close();
  
  system ("pause");
  return 0;
  
}

int checkfunc(int* dynword, int len, int& count)
{
           
     if (dynword[(len)] == 122)     
     {    
     dynword[len] = 97;     
     checkfunc(dynword,(len-1),count);  
     
              if (count == len)
                        count--;
                                   
     }     
     else
     {
              dynword[0];
              dynword[(len)]++;
     }        
            
return dynword[0] - 96;  //returns 1 -> 26 (fraction completed)
    
}

 

Here's the .exe

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×
×
  • Create New...