What is the secret to becoming the greatest programmer in the whole wide world?

Pages: 1234... 6
yeah kay, but in order for it to specific you would have to dictate more or less whats in it, else i could just go and get something from th net right? but then it has to be within my skill range (i got the hang of beginner style linked lists)

i got something in the pipline, but i still need to think about it lots
Write a program that correctly guesses a combination of 4, 2 digit, numbers/symbols.

Symbols may be from the following set: {!, @, #, $, %, ^, &, *, ?, <, >, ~}.

For example, a combination may be:

4$-79-##-@1

Your program must first generate the random combination. Then your program may make an unlimited amount of guesses. The feedback from each test must be true or false.

Bonus points if your program uses concurrency.
Last edited on
hrmm, kay.
Write a program that reverses a file (byte by byte).
It has to work on both text files and binary files.
It gets the input file and new output file as command line parameters.

For example:
reverseFile.exe inputFile.jpg outputFile.bin


In the above usage example, by reversing outputFile.bin, you must get a new file identical to the original inputFile.jpg.

http://www.cplusplus.com/forum/articles/13355/
http://www.cprogramming.com/tutorial/lesson14.html
http://cplusplus.com/reference/fstream/
bucky talks about that at the end of his youtube thing, i never listened to that one cos thats when i got exited about sdl, im allready working iseeplusplus's one, urs next, i was actually sad when i ran out of challenges; although that was one of the ones i didnt do.

urs is next
is this the definition of spaghetti code cos it aint going well, i proly a bit rusty

can i get tiny tiny hints

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>

using namespace std;

string pair1,pair2,pair3,pair4,total,dash,crack1,crack2,crack3,crack4;

string singles[45] = {"1","2","3","4","5","6","7","8","9","0",
"q","w","e","r","t","y","u","i","o",
"p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"!","£","$","%","^","&","*","(",")"};

string getrandpair ()
{
string pair,p1,p2;
int slctr,slctr2;
slctr = 1+(rand()%44);
slctr2 = 1+(rand()%44);
p1 = singles [slctr];
p2 = singles [slctr2];
return pair = p1+p2;
}

bool crackpair (string *pair, string *crack)
{
string temppair;
cout << temppair;
temppair = getrandpair ();
if (pair == temppair)
{
   temppair = crack;
    return true;
}
if (pair != temppair)
{
    return false;
}
}


int main ()

{
srand(time(0));

dash = "-";
int refresh;
bool quit = true, restart = true;

while (quit != false)
{
    cout << "type 1 to create new code or type 2 to quit "<<endl;
    cin >> refresh;
    switch (refresh)
    {
        case 2:
        quit = false;
        break;
        case 1:
        while (restart != false)
        {
           pair1 = getrandpair();
            pair2 = getrandpair();
            pair3 = getrandpair();
            pair4 = getrandpair();
            cout << pair1<<"-"<<pair2<<"-"<<pair3<<"-"<<pair4<<endl;
            cout << "press 1 to begin decoding and 2 to quit"<< endl;
            cin >> refresh;

                  switch (refresh)
                   {
                   case 2:
                   restart = false;
                   break;
                   case 1:
                   while (restart != false)
                   {
                    bool pairone = true,pairtwo = true,pairthree = true, pairfour = true;
                    while (pairone != false)
                    {
                        crackpair(pair1,crack1);
                    }
                    total=crack1;
                    cout << total;
                    while (pairtwo != false)
                    {
                        crackpair(pair2,crack2);
                    }
                    total=total+dash+crack2;
                    cout << total;
                    while (pairthree != false)
                    {
                        crackpair(pair3,crack3);
                    }
                    total=total+dash+crack3;
                    cout<< total;
                    while (pairfour != false)
                    {
                        crackpair(pair4,crack4);
                    }
                    total=total+dash+crack4;
                    cout << total <<endl;
                   }

                   break;
                   }

        break;
        cout << "the code was; "<< total<<"restart? y/n"<<endl;


    }

}
}
return 0;
}
In solving iseeplusplus' problem, I disregarded the dashes completely. They're aesthetic, and not part of the problem. They're probably there to encourage a concurrent solution.

Anyway, the problem is that you need to brute force the randomly generated combination. And since you have 22 symbols in groups of 8, you get a total of 228 permutations... I think. Which is a lot.

So iseeplusplus, could we see your own solution for now?
Bonus points for concurrency using the C++11 library.

@ devonrevenge: a hint would be to use backtracking to generate each permutation until you stumble upon the right one.
http://en.wikipedia.org/wiki/Backtracking
oh yeah 54875873536 is a big number, best not repeat stuff
devonrevenge wrote:
1
2
3
4
string singles[45] = {"1","2","3","4","5","6","7","8","9","0",
"q","w","e","r","t","y","u","i","o",
"p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"!","£","$","%","^","&","*","(",")"};


Digits and symbols, letters excluded.
At least you forgot about the uppercase ones.
I havn't tested it, nor have I tried doing it in parallel yet, but this is how I would go about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <ctime>
using namespace std;

const char numDigits = 8;
const char numSymbols = 22;
const char symbols[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '^', '&', '*', '?', '<', '>', '~'};

bool testCombination (char *sI, char *com) {

    bool isCracked = true;
    for (int i = 0; i < 8; ++i) {
        if (com[i] != symbols[sI[i]]) {
            isCracked = false;
            break;
        }
    }
    return isCracked;
}


int main() {

    srand(time(NULL));

    char combo[numDigits];
    for (int i = 0; i < numDigits; ++i)
       combo[i] = symbols[rand() % numSymbols];

    char symbolIndex[numDigits];
    for (int i = 0; i < numDigits; ++i)
        symbolIndex[i] = 0;

    unsigned long long numPermutations = 1;
    for (int i = 0; i < numDigits; ++i)
        numPermutations *= numSymbols;
    
    for (unsigned long long i = 0; i < numPermutations; ++i, ++symbolIndex[0]) {
        for  (int j = 0; j < numDigits - 1; ++j) {
            if (symbolIndex[j] == numSymbols) {
                symbolIndex[j] = 0;
                ++symbolIndex[j+1];
            }
        }
        if (testCombination (symbolIndex, combo))
            break;  
    }       

    for (int i = 0; i < numDigits; ++i)
        cout << combo[i];    

    cout << endl;

    for (int i = 0; i < numDigits; ++i)
        cout << symbols[symbolIndex[i]];
 
    return 0;
}


Making it run in parallel shouldn't be too difficult. Just test the first numPermutation / numThreads in the first thread, initialize symbolIndex2[] to start at numPermutations / numThreads, and have it run for the same number of iterations, and so on.

Last edited on
Can someone elaborate on iseeplusplus' task? I'm not sure what it's supposed to do. Generate four pairs of two digits/symbols? Randomly or iteratively? Then the user or the program guesses? What's the correct string? The example one in the post?
@ chrisname: I understand it as:

Randomly generate a permutation (with repetition) of length 8, consisting of decimal digits and the given symbols. Then brute force it.
Yeah, it doesn't really make a difference wether you consider it 4 pairs of characters, or just 8 characters I guess.

You are supposed to brute force it. You need to generate a random combo just so you have something to brute force.
Last edited on
how is this concurrent? i dont see how its possible to get more than one thing at a time running.

any idea how i can escape my crack function once i got the right code??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool crackpair (string &pair, string &crack, bool &pass)
{
string temppair, part1, part2, empty1;
for (int a = 0; a<45 ; a++)
  {
      part1 = singles [a];

     for (int b = 0; b<45 ; b++)
     {
     part2 = singles [b];

    temppair = part1+part2;
    cout<< temppair<<pair;

  if (pair == temppair)
{
   system ("pause");
   crack = temppair;
    pass = false;
    return true;
}


     }

  }

}
Last edited on
Write a program that locks the computer input and does some crazy actions (suggestions) : crazy mouse moving, random messages, and showing crazy characters...
Consider : windows.h
And do some beep sounds...
(Crazy, but good exercise)
What I posted isn't concurrent. To get more than one thing running at a time, you need to spawn a number of threads each testing a different portion of the domain. If one thread finds the correct combination, it needs to notify the other threads so they can terminate.

Think about a combo of 3 numbers from 1 to 3. Makes for 3^3 possible permutations:

111, 112, 113, 121, 122, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 231, 232, 233, 311, 312, 313, 321, 322, 323, 331, 332, 333

In the case of our problem, we have 22 symbols. I have the symbols in an array, so I can pretend that the index which corresponds to it is the symbol as I'm generating permutations. This way you can imagine you have some very large number represented in a base 22 numerical system.

You count up from 1 to 22^8 in this numerical system, and you get all of the combinations.

So based off of my example, you would need to figure out what is numPermutations / numThreads in base 22. Then initialize separate symbolIndex[]'s to start at some offsets, and run the permutation generating loop for numPermutations / numThreads iterations, for each thread, each starting with it's own symbolIndex initialized appropriately.

For example, if I were testing in base 10, the permutations for a 3 digit combo, that would be 10^3 permutations (1000). If I spawn 100 threads, then the first checks 0 through 9, the second thread checks 10 through 19, and so on.
Last edited on
wait wait i am sticking to thing im doing now...

@jackson marie i am trying to stick to sdl api for now, i will have to play with all that later, why dont you do a 'prove your not spoonlicker exercice, they fun XD'

perhaps you could help me actually guys, my functions fine-ish im sure i will eventualy solve it but can you see why im not going on to do the second pair?
the console tells me i found the first pair and then starts again, i was hoping the bool would cancel that one and move on to crack pair two

here 'tis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>

using namespace std;

string pair1,pair2,pair3,pair4,total,dash,crack1,crack2,crack3,crack4;

string singles[45] = {"1","2","3","4","5","6","7","8","9","0",
"q","w","e","r","t","y","u","i","o",
"p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"!","£","$","%","^","&","*","(",")"};

string getrandpair ()
{
string pair,p1,p2;
int slctr,slctr2;
slctr = 1+(rand()%44);
slctr2 = 1+(rand()%44);
p1 = singles [slctr];
p2 = singles [slctr2];
return pair = p1+p2;
}

bool crackpair (string &pair, string &crack, bool &pass)
{
string temppair, part1, part2, empty1;
for (int a = 0; a<45 ; a++)
  {
      part1 = singles [a];

     for (int b = 0; b<45 ; b++)
     {
     part2 = singles [b];

    temppair = part1+part2;
    cout<< temppair<<pair;

  if (pair == temppair)
{
   system ("pause");
   crack = temppair;

    return pass = true;
    break;
}


     }
if (pair == temppair)

    break;

  }

}



int main ()

{
srand(time(0));

dash = "-";
int refresh;
bool quit = true, restart = true;

while (quit != false)
{
    cout << "type 1 to create new code or type 2 to quit "<<endl;
    cin >> refresh;
    switch (refresh)
    {
        case 2:
        quit = false;
        break;
        case 1:
        while (restart != false)
        {
           pair1 = getrandpair();
            pair2 = getrandpair();
            pair3 = getrandpair();
            pair4 = getrandpair();
            cout << pair1<<"-"<<pair2<<"-"<<pair3<<"-"<<pair4<<endl;
            cout << "press 1 to begin decoding and 2 to quit"<< endl;
            cin >> refresh;

                  switch (refresh)
                   {
                   case 2:
                   restart = false;
                   break;
                   case 1:
                   while (restart != false)
                   {
                    bool pairone = true,pairtwo = true,pairthree = true, pairfour = true;
                    while (pairone != false)
                    {
                        cout << "cracking pair one" << endl;
                        pairone = crackpair(pair1,crack1,pairone);
                    }
                    total=crack1;
                    cout << total;
                    while (pairtwo != false)
                    {
                        cout<< "cracking pair two" << endl;
                        crackpair(pair2,crack2,pairtwo);
                    }
                    total=total+dash+crack2;
                    cout << total;
                    while (pairthree != false)
                    {
                        pairthree = crackpair(pair3,crack3,pairthree);
                    }
                    total=total+dash+crack3;
                    cout<< total;
                    while (pairfour != false)
                    {
                       pairfour = crackpair(pair4,crack4,pairfour);
                    }
                    total=total+dash+crack4;
                    cout << total <<endl;
                   }

                   break;
                   }

        break;
        cout << "the code was; "<< total<<"restart? y/n"<<endl;


    }

}
}
return 0;
}


i havnt finnished making it beautiful yet but i need to test it with couts here and there etc
Last edited on
wait i got it :D i love that feeling
when i finish the code could you hint on how i can concurrentify it?
Last edited on
Your testing pairs individually instead of the whole combo at once. That means you only need to test a max of 7744, with your set, times.

If you test the whole combo at once, you need to test a maximum of 54,875,873,536 times, as in the problems specifications, or with your set, 14,048,223,625,216 times.
Last edited on
Proof i not spoonlicker :D can i get some constructive criticism please? (on the code) and if you consider yourself a beginner please try and write a more efficient code :P

EDIT: oh @iseeplusplus, i thought it would be better to break it down :/ kay i get on changing it.
EDIT:would you judge me if i made it flick through combinations in green letters? (you know, red head , blonde brunette)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>

using namespace std;

string pair1,pair2,pair3,pair4,total,dash,crack1,crack2,crack3,crack4;

 bool pairone = true,pairtwo = true,pairthree = true, pairfour = true;

string singles[45] = {"1","2","3","4","5","6","7","8","9","0",
"q","w","e","r","t","y","u","i","o",
"p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"!","£","$","%","^","&","*","(",")"};

string getrandpair ()
{
string pair,p1,p2;
int slctr,slctr2;
slctr = 1+(rand()%44);
slctr2 = 1+(rand()%44);
p1 = singles [slctr];
p2 = singles [slctr2];
return pair = p1+p2;
}

bool crackpair (string &pair, string &crack, bool &pass)
{
string temppair, part1, part2, empty1;
for (int a = 0; a<45 ; a++)
  {
      part1 = singles [a];
      for (int b = 0; b<45 ; b++)
     {
     part2 = singles [b];
     temppair = part1+part2;
     if (pair == temppair)
  {
      crack = temppair;
      return pass =false;
      break;
  }
  }
  if (pair == temppair)

    break;

  }

}



int main ()

{
srand(time(0));

dash = "-";
int refresh;
bool quit = true, restart = true;

while (quit != false)
{
    cout << "type 1 to create new code or type 2 to quit "<<endl;
    cin >> refresh;
    switch (refresh)
    {
        case 2:
        quit = false;
        break;
        case 1:
        while (restart != false)
        {
           pair1 = getrandpair();
            pair2 = getrandpair();
            pair3 = getrandpair();
            pair4 = getrandpair();
            cout << pair1<<"-"<<pair2<<"-"<<pair3<<"-"<<pair4<<endl;
            cout << "press 1 to begin decoding and 2 to quit"<< endl;
            cin >> refresh;
                   switch (refresh)
                   {
                   case 2:
                   restart = false;
                   break;
                   case 1:
                   while (restart != false)
                   {
                    while (pairone != false)
                    {
                        crackpair(pair1,crack1,pairone);
                    }
                    total=crack1;
                    while (pairtwo != false)
                    {
                        crackpair(pair2,crack2,pairtwo);
                    }
                    total=total+dash+crack2;
                    while (pairthree != false)
                    {
                        crackpair(pair3,crack3,pairthree);
                    }
                    total=total+dash+crack3;
                    while (pairfour != false)
                    {
                        crackpair(pair4,crack4,pairfour);
                    }
                    total=total+dash+crack4;
                    restart = false;
                   }
                   break;
                   }
         restart = true;
        break;
        cout << "the code was; "<< total<<"restart? y/n"<<endl;


    }

}
}
return 0;
}

and you can see all my working out too.
how you make that concurrent?
Last edited on
Pages: 1234... 6