# Simple Bruteforce algorithm

Page 1 of 1

## 8 Replies - 23945 Views - Last Post: 11 September 2015 - 05:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249941&amp;s=df96cbf09432106bdfbb324abe4012db&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jessica_nl

Reputation: 1
• Posts: 20
• Joined: 05-October 11

# Simple Bruteforce algorithm

Posted 05 October 2011 - 04:52 AM

Hello guys,

Im a first year computer science student and i got a assignment to program a little brute force program.
its a very easy assignment, but i just don't know how to solve the problem.

for example:

how many colums: '3'
whats the max number: '15'

the program have to print this:

[0,0,0]
...
[0,0,15]
[0,1,0]
...
[0,15,15]
[1,0,0]

and so on till

[15,15,15]

can somebody give me a hint? or give me an example?

thankss

kiss Jessica from the netherland

Is This A Good Question/Topic? 0

## Replies To: Simple Bruteforce algorithm

### #2 thephpdev

Reputation: 9
• Posts: 59
• Joined: 01-July 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 05:09 AM

Where I started off before I perfected it to use less lines I just did 3 for loops that constantly incremented until it hit the limit. Kinda like this:
```int max = 15;
int cols = 3;
for (int a = 0; a < max; a++) {
for (int b = 0; b < max; b++) {

Where I started off before I perfected it to use less lines I just did 3 for loops that constantly incremented until it hit the limit. Kinda like this:
[code]
int max = 15;
int cols = 3;
int nums[3] = {0, 0, 0};
for (int a = 0; a < max; a++) {
nums[b] = 0;
for (int b = 0; b < max; b++) {
nums[c] = 0;
for (int c = 0; c < max; c++) {
nums[c]++;
}
nums[b]++;
}
nums[a]++;
}

```

That's the long way, you can create another function when you get farther into it.

### #3 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 05:18 AM

Show us what you have tried. Don't just use us to give you your answer so you can cheat on your homework.

@thephpdev: Please do not just give people the code they want if they did not put any effort in the first place.

### #4 amateurcoder

Reputation: 0
• Posts: 2
• Joined: 05-October 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 06:52 AM

```#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
int max;
cout << "The maximum number: ";
cin >> max;
for (int a=0; a<max+1; a++)
{
for (int b=0; b<max+1; b++)
{
for (int c=0; c<max+1; c++)
{
cout << "[" << a << " " << b << " " << c << "]" << endl;
}
}
}
system("pause");
return 0;
}

```

As you understand, there are 3 for loops for each column. You have to find a way to simplify the code for the columns.

This post has been edited by amateurcoder: 05 October 2011 - 06:53 AM

### #5 jessica_nl

Reputation: 1
• Posts: 20
• Joined: 05-October 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 07:13 AM

Hii guys,

dont understand me wrong.. i tried my ass off before i entered this forum.
i understand that for each colum i need a for loop.

as i said in my example if you want 3 colums you will need 3 forloops.
if you need 4, you will need 4 forloops

i want a function with just 1 for loop. as parameter the amount of colums. with that given i can use a while loop.

am i correct?

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11790
• Posts: 44,309
• Joined: 27-December 08

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 07:34 AM

Hint- think about the far-left column. When that column exceeds your max number, you should stop. Look at using the % (modulus) operator as well. If the column%max == 0, then set the column to 0 and increment the next column. Repeat this process until column%max != 0 or lastColumn > max. An array will help as well.

### #7 jessica_nl

Reputation: 1
• Posts: 20
• Joined: 05-October 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 10:50 AM

Hii guyss =)

i finally got it work.. with a single forloop.

@ macosxnerd101 i will try your hint out if i have enough time to reprogram this assignment

Thanks all =)

### #8 amateurcoder

Reputation: 0
• Posts: 2
• Joined: 05-October 11

## Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 11:12 AM

jessica_nl, on 05 October 2011 - 10:50 AM, said:

Hii guyss =)

i finally got it work.. with a single forloop.

@ macosxnerd101 i will try your hint out if i have enough time to reprogram this assignment

Thanks all =)

Could you post your work here? So the others, who need help, may learn something from you.

This post has been edited by amateurcoder: 05 October 2011 - 11:12 AM

### #9 kr.abhinav

Reputation: 0
• Posts: 1
• Joined: 11-September 15

## Re: Simple Bruteforce algorithm

Posted 11 September 2015 - 05:32 AM

Hi there!
I'm a new member,trying to learn bruteforce algorithm
I made a code for the above question whic apparently worked(didn't expect it too /> )
Enclosing my code in c++ in case it helps anyone:-
```#include<iostream>
using namespace std;
long a[1000];
void incnext(int l,int m)
{
if(l==0&&a[l]==(m+1))
{
a[l]++;
return;
}
if(a[l]==(m+1))
{
a[l]=0;
a[l-1]++;
incnext(l-1,m);
}
}
void disp(int x)
{
cout<<"[";
for(int i=0;i<x-1;i++)
cout<<a[i]<<",";
cout<<a[x-1]<<"]\n";
}
int main()
{
int c,m;
cout<<"Enter no. of columns:";
cin>>c;
cout<<"Enter max No:";
cin>>m;
disp(c);
while(1)
{
a[c-1]++;
if(a[c-1]==(m+1))
incnext(c-1,m);
if(a[0]==m+2)
break;
disp(c);

}
}

```