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
Simple Bruteforce algorithm
Page 1 of 17 Replies - 2534 Views - Last Post: 05 October 2011 - 11:12 AM
Replies To: Simple Bruteforce algorithm
#2
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:
That's the long way, you can create another function when you get farther into it.
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.
#4
Re: Simple Bruteforce algorithm
Posted 05 October 2011 - 06:52 AM
Here's something you may start with:
As you understand, there are 3 for loops for each column. You have to find a way to simplify the code for the columns.
#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
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?
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
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
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 =)
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
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 =)
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|