Jump to content
Sign in to follow this  
cold_snipe

Programming Challenge 1

Recommended Posts

Great entries so far!

I personally like Verran's one better, but remember, its all about functionality. The "open c:/results.txt" line did not work for me, i had to browse there and open it. I will not give any ideas on how to make these better, but there are plenty of other things you could include to win :D

And other people who dont know so much / etc., sumbit your entry anyway!

I didnt think this contest would be such a success :D I thikn i might shorten the date when it ends, yes.

 

The Contest will end September 1, 2005.

 

Happy coding! :D

Share this post


Link to post
Share on other sites

Yea, ummmm........ I think I'm going to throw in the towel on this one. I belive this program is a tad out of my league. Here is what I got so far (please refrain from laughing at me) as I am quite new at this.

 

//code by Jordan Shuhart

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
   srand(NULL(time));
   string alphabet[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
   int number;
   int y;
   int x;
   
   cout << "How many letters do you want in the words? ";
   cin >> number;
   
   for (y = 0; y <= (number * 26); y++)
   {
         cout << alphabet[(] << alphabet[] << alpahbet[] << "\n";
   } 
   
   
   
   system("pause");
   return 0;
   
}

 

 

I didn't even get to the creating the .txt file yet.

Share this post


Link to post
Share on other sites
make one that includes numbers too so i can use it to generate passwords plzz

524431[/snapback]

 

Here's your bruteforce :P (I put the source in the zip)

 

I will make one that has more options for an "all in one" feel when I get back from surgery on Tuesday or Wednesday, no time now :)

 

Also, Archerzz: A recommendation. Check out a function called "type casting". You see how I do:

 

fout << char(dynword[i]);

 

My array is not filled with characters (a -> z), it's filled with integers (97 -> 122). Those are the ASCII codes for the letters a -> z. If you do a

 

cout << char(97);

 

you will see an "a" in the output, because the char() forces it into character format. Just like if you do

 

cout << int(9.765);

 

you will see "9" in the output. Make sense?

 

It's easier to work with the integers (imo) than the characters. Feel free to steal any bits of my code to compile and check out or to test stuff. I plan to make a version with better comments when I get back (I'm actually having a lot of fun with this little project). I always found it easier to learn from a working example, so if you do too, you can use mine to toy around. Good luck! And if you have any questions, just post 'em up, and I or someone else will surely help you out.

Share this post


Link to post
Share on other sites

shame i wish i could program. Where did u guys learn to program in C?

 

i'd love to be able to teach myself i think it might come in handy

Share this post


Link to post
Share on other sites

Well, here's mine. Kinda similar to Verrans I suppose... but non recursive and C not C++ ( I think? :) ).

 

main.c

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/* xx\n\0 */
#define BUF_LENGTH 4
#define LETTERS 93
#define START_CHAR 33

void WriteIt(FILE *, int *, int);
void Add(int *, int);

/*
* Write out character(s) and calculate (very rough) percentage done.
*/
void WriteIt(FILE *fp, int *offsets, int len) {
   int i = 0;
   static int prev = 0, cur = 0;

   /* set current "percentage" */
   cur = offsets[0];

   /* write out characters, using offsets from START_CHAR (ASCII 33), to file */
   for (i = 0; i < len; i++) {
       fprintf(fp, "%c", START_CHAR + offsets[i]);
   }
   fprintf(fp, "\n");

   /*
    * if current percentage is bigger than previous, print out new percentage
    * to the screen!
    */
   if (cur > prev) {
       printf("%d%% done\n", (int)(((double)offsets[0] / LETTERS) * 100));
       prev = cur;
   }
}

/*
* Increment letter and do all the rollover array stuff
* Example: For 2 characters the offset array would be from
* (0, 0) -> (LETTERS, LETTERS)
*
* A bit like a clock, when the minutes reach 60 the hour gets incremented and
* the minutes get set back to zero.
*/
void Add(int *offsets, int len) {
   int i = 0;

   /* fix length passed in to work with arrays */
   len--;

   /* increment last slot in array */
   offsets[len]++;

   /*
    * Work *backwards* through array checking when to roll over in to the
    * previous slot.
    */
   for (i = len; i >= 0; i--) {
       /*
        * An array element needs "rolling over" when the offset in that array
        * is bigger than LETTERS. Since we're working backwards we'll need to
        * check to see if we'r already on the first element or not too!
        */
       if (offsets[i] > LETTERS) {
           offsets[i] = 0;
           if (i > 0) offsets[i - 1]++; else offsets[i]++;
       }
   }
}

int main(void) {
   int numLetters = 0, i = 0, done = 0;
   char *buf = NULL;
   int *offsets;
   FILE *fp = NULL;

   /* Allocate memory for input buffer and zero it */
   buf = (char *)malloc(BUF_LENGTH * sizeof(char));
   assert(buf != NULL);
   memset(buf, 0, BUF_LENGTH * sizeof(char));

   printf("Welcome the markiemrboo's word thingy generator\n\n");

   /* Ask how many letters, convert string to int, check if range is valid */
   do {
       /* Clear anything from stdin (keyboard input please!) left over */
       fflush(stdin);
       printf("Please enter the number of letters: ");
       fgets(buf, BUF_LENGTH, stdin);
       numLetters = atoi(buf);
   } while (numLetters < 1 || numLetters > 99);

   /* free input buffer */
   free(buf);
   buf = NULL;

   /*
    * Allocate memory for offset array, 1 slot for each character, and zero it
    */
   offsets = (int *)malloc(numLetters * sizeof(int));
   assert(offsets != NULL);
   memset(offsets, 0, numLetters * sizeof(int));

   /* Open file to write output to */
   fp = fopen("worddump.txt", "w");
   if (!fp) {
       perror("fopen");
       exit(1);
   }

   /*
    * My crappy coding skills means I have to write out either the first or
    * last series of letters outside of the loop.
    *
    * Example: 2 characters would be from 0,0 to LETTERS,LETTERS. If I didn't
    * have the WriteIt() outside of the loop here it would actually end up
    * writing out the characters from 0,1 to LETTERS,LETTERS because of the
    * Add() inside the loop. Get it?!
    */
   WriteIt(fp, offsets, numLetters);
   while (!done) {
       Add(offsets, numLetters);
       WriteIt(fp, offsets, numLetters);

       /* 
        * if all the offsets are the maximum they can possibly be we must have
        * finished! If just one of them *isn't*, then don't finish (done = 0)
        * and stop checking (break out of the loop)
        */
       for (i = 0; i < numLetters; i++) {
           if (offsets[i] == LETTERS) {
               done = 1;
           } else {
               done = 0;
               break;
           }
       }
   }

   /* free offsets array */
   free(offsets);
   offsets = NULL;

   /* We're done with the file so.. close that now! */
   fclose(fp);

   system("pause");

   return 0;
}

 

EDIT: ---

 

If you'd like a speed comparison between mine and verran's I done one.

 

Used some Windows timer thinger, QueryPerformanceFrequency() and QueryPerformanceCounter(). On my system QueryPerformanceFrequency() gives 3,579,545 ticks per second.

 

Generating 3 characters on both programs, here's the times.

 

Verran: done in 7,391,775 ticks (around 2 seconds)

Mine: done in 1,942,204 ticks (under a second)

 

7391775 / 1942204 = 3.8 times faster?

 

Additionally Verrans exe (compiled on my machine, not the one used from the zip) still appears to be ~400K where as mine is 6K (both without debugging). If anyone wants to go investigate if the massive size difference and speed difference is just due to C++ vs C... well, be my guest!

 

Top window = verran

Bottom window = mine

 

 

EDIT: ---

 

Ok, another speed comparison so you can see just how much it does make a difference lol. 3 characters is all of, what, 1.5 seconds difference between mine and verrans, if that? 1 second maybe?

 

Check out a 4 character comparison lol (~440MB text file anyone?)

 

Verran:

3579545 ticks per second

done in 589405015 ticks (164.65 seconds)

 

Mine:

3579545 ticks per second

done in 230617279 ticks (64.42 seconds)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×