7 Replies - 11803 Views - Last Post: 05 October 2011 - 11:12 AM Rate Topic: -----

#1 jessica_nl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 amateurcoder  Icon User is offline

  • New D.I.C Head

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

Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 06:52 AM

Here's something you may start with:

#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

Was This Post Helpful? 0
  • +
  • -

#5 jessica_nl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10769
  • View blog
  • Posts: 40,100
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 jessica_nl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 =)
Was This Post Helpful? 0
  • +
  • -

#8 amateurcoder  Icon User is offline

  • New D.I.C Head

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

Re: Simple Bruteforce algorithm

Posted 05 October 2011 - 11:12 AM

View Postjessica_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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1