# Re: brute force

Discussion in 'C++' started by Stuart Golodetz, Aug 22, 2003.

1. ### Stuart GolodetzGuest

"rosty" <> wrote in message
news:bi4vce\$591i2\$-berlin.de...
> does anybody know how a brute force algorithm looks like? the result

should
> be something like this a, b, c; etc then aa, ab, ac; etc and so on....
> i imagine there should be a vector of char holding the chars that are

going
> to participate in the brute forcing,
> vector<char> vc(3);
> vc[0]='a';
> vc[1]='b';
> vc[2]='c';//only three char for simplicity
>
> then a string that is going to be the pass itself. then an iteration
> through the vector and adding those chars to the pass string, and an int
> holding the actual size of the pass (untill the pass reaches its maximum)
> and then... i just cannot put it all together, is it so difficult, or am i
> just so stupid?
> any code example?
> tia

Here's code which does what you want for a given width, it won't be very
hard to modify it to increase the width by one after each pass:

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string characters = "abc";
int characterCount = characters.length();

int width;
std::cin >> width;
std::string output(width, characters[0]);
std::vector<int> indices(width);

bool done = false;
while(!done)
{
std::cout << output << '\n';
for(int i=width-1; i>=0; --i)
{
if(indices == characterCount - 1)
{
indices = 0;
output = characters[0];
if(i == 0) done = true;
}
else
{
++indices;
output = characters[indices];
break;
}
}
}
return 0;
}

If there's anything you don't understand, feel free to ask.

HTH,

Stuart.

P.S. This is just one example of a "brute force" algorithm, there are
infinitely many others. Brute force just implies trying every possible thing
(in this case, every sequence of characters) until you get it right.
P.P.S. Next time, post your attempt at the actual code, it will encourage
Stuart Golodetz, Aug 22, 2003

2. ### rostyGuest

your code is very interesting, that's all i have to say, i would never think
of testing an uninitialized value, i mean if(indices == characterCount -
1). and it works...
i certanly own you a beer, i have some reading for this weekend
another beer goes to Karl, still trying to make it in a way of recursion

ps. i'm certainly not a good programmer, but! only god knows how much fun i
have doing this things
"Stuart Golodetz" <> wrote in message
news:3f463a46\$0\$13641\$...
> "rosty" <> wrote in message
> news:bi4vce\$591i2\$-berlin.de...
> > does anybody know how a brute force algorithm looks like? the result

> should
> > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
> > i imagine there should be a vector of char holding the chars that are

> going
> > to participate in the brute forcing,
> > vector<char> vc(3);
> > vc[0]='a';
> > vc[1]='b';
> > vc[2]='c';//only three char for simplicity
> >
> > then a string that is going to be the pass itself. then an iteration
> > through the vector and adding those chars to the pass string, and an int
> > holding the actual size of the pass (untill the pass reaches its

maximum)
> > and then... i just cannot put it all together, is it so difficult, or am

i
> > just so stupid?
> > any code example?
> > tia

>
> Here's code which does what you want for a given width, it won't be very
> hard to modify it to increase the width by one after each pass:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> int main()
> {
> std::string characters = "abc";
> int characterCount = characters.length();
>
> int width;
> std::cin >> width;
> std::string output(width, characters[0]);
> std::vector<int> indices(width);
>
> bool done = false;
> while(!done)
> {
> std::cout << output << '\n';
> for(int i=width-1; i>=0; --i)
> {
> if(indices == characterCount - 1)
> {
> indices = 0;
> output = characters[0];
> if(i == 0) done = true;
> }
> else
> {
> ++indices;
> output = characters[indices];
> break;
> }
> }
> }
> return 0;
> }
>
> If there's anything you don't understand, feel free to ask.
>
> HTH,
>
> Stuart.
>
> P.S. This is just one example of a "brute force" algorithm, there are
> infinitely many others. Brute force just implies trying every possible

thing
> (in this case, every sequence of characters) until you get it right.
> P.P.S. Next time, post your attempt at the actual code, it will encourage
>
>
rosty, Aug 23, 2003

3. ### Stuart GolodetzGuest

"rosty" <> wrote in message
news:bi7gir\$62flm\$-berlin.de...
> your code is very interesting, that's all i have to say, i would never

think
> of testing an uninitialized value, i mean if(indices ==

characterCount -
> 1). and it works...

That's because it's initialized to 0.

Cheers,

Stuart.

> i certanly own you a beer, i have some reading for this weekend
> another beer goes to Karl, still trying to make it in a way of recursion
>
> ps. i'm certainly not a good programmer, but! only god knows how much fun

i
> have doing this things
> "Stuart Golodetz" <> wrote in message
> news:3f463a46\$0\$13641\$...
> > "rosty" <> wrote in message
> > news:bi4vce\$591i2\$-berlin.de...
> > > does anybody know how a brute force algorithm looks like? the result

> > should
> > > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
> > > i imagine there should be a vector of char holding the chars that are

> > going
> > > to participate in the brute forcing,
> > > vector<char> vc(3);
> > > vc[0]='a';
> > > vc[1]='b';
> > > vc[2]='c';//only three char for simplicity
> > >
> > > then a string that is going to be the pass itself. then an iteration
> > > through the vector and adding those chars to the pass string, and an

int
> > > holding the actual size of the pass (untill the pass reaches its

> maximum)
> > > and then... i just cannot put it all together, is it so difficult, or

am
> i
> > > just so stupid?
> > > any code example?
> > > tia

> >
> > Here's code which does what you want for a given width, it won't be very
> > hard to modify it to increase the width by one after each pass:
> >
> > #include <iostream>
> > #include <string>
> > #include <vector>
> >
> > int main()
> > {
> > std::string characters = "abc";
> > int characterCount = characters.length();
> >
> > int width;
> > std::cin >> width;
> > std::string output(width, characters[0]);
> > std::vector<int> indices(width);
> >
> > bool done = false;
> > while(!done)
> > {
> > std::cout << output << '\n';
> > for(int i=width-1; i>=0; --i)
> > {
> > if(indices == characterCount - 1)
> > {
> > indices = 0;
> > output = characters[0];
> > if(i == 0) done = true;
> > }
> > else
> > {
> > ++indices;
> > output = characters[indices];
> > break;
> > }
> > }
> > }
> > return 0;
> > }
> >
> > If there's anything you don't understand, feel free to ask.
> >
> > HTH,
> >
> > Stuart.
> >
> > P.S. This is just one example of a "brute force" algorithm, there are
> > infinitely many others. Brute force just implies trying every possible

> thing
> > (in this case, every sequence of characters) until you get it right.
> > P.P.S. Next time, post your attempt at the actual code, it will

encourage