I am writing code for a program that will take user input selection of columns and determine an array based on that.
The number of columns will be user selected.
The number of rows equals 3^(columns) <--exponent not XOR
- This is because each column has the possibility of having the numbers 0,1,or 2
For example, if the user were to select "4" columns, then the array would have 4 columns and 3^4=81 rows.
Then I would like to populate this with all of the possible combinations of 0,1,2
i.e.
0000
0001
0002
0010
0011
0012
0020
0021
0022
0100
....
2220
2221
2222
Any idea how I would create the "For" Loop for this?
int main()
{
//User input
int cols;// (columns in array)
cout<<"\n How many columns would you like to have? :\t";
cin>>cols;
//(rows in array dependent on columns)
int rows = ( int ) pow( 3.0 , cols) ;
cout<<"\n The Number of possible combinations for the given columns are :\t"<<rows;
//Declare array via vector of vectors
vector< vector<int> > MyArray(rows, vector<int>(cols));
int i,j,k = 0;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
OK. First off, you don't need the inner loop here - we're going to be going to be going a whole row at a time.
The right-most column will be set to i%3, so it goes 0,1,2,0,1,2,0,1,2 over and over.
The next column to the left will be (i/3)%3 so it goes 0,0,0,1,1,1,2,2,2,0,0,0,etc over and over.
The next coumn to the left will be (i/9)%3, and then the next column over will be (i/27)%3.
I assume you see the pattern now - for each digit where 0 is the right-most digit, the value is (i/(3digit)%3. Basically we're converting the loop index to a base 3 number digit by digit.
#include <iostream>
#include <math.h>
#include <vector>
usingnamespace std;
int main()
{
//User input
int cols;//(columns in array)
cout<<"\n How many columns? :\t";
cin>>cols;
int rows = ( int ) pow( 3.0 , cols) ;
cout<<"\n The Number of possible combinations for the given columns are :\t"<<rows;
vector< vector<int> > MyArray(rows, vector<int>(cols));
int i,j,k = 0;
for (i=1;i<rows;i++) //Start with row 1
{
bool carry = true;
for (j=0;j<cols;j++)
{
int value = MyArray[i - 1][j]; //Value from previous row
if(carry)
{
value += 1;
if(value < 3)
{
MyArray[i][j]=value;
carry = false;
}
else
{
MyArray[i][j]=0;
}
}
else
{
MyArray[i][j]=value;
}
}
}
return 0;
}
This is what I have so far, but it is no outputting what I want.
Note that this replaces lines 27-44, and you also don't need k or carry. Please tell me if you don't understand how it works (or if it didn't work - I can't test atm)
@ajh32 that's insanely over-complicated. Why did you do it that way?
Over complicated? No, it just splits each task into a self contained function, which is good practtice. The only mistake I made is that I should have used base 3 rather than base 2, I just missunderstood the question!
#include <sstream>
#include <string>
#include <iostream>
#include <cmath>
#include <algorithm>
usingnamespace std;
int main()
{
constint base = 3;
int cols;
cout<<"\n How many columns? :\t";
cin>>cols;
int rows = (int)pow((double)base, cols);
for (int n = 0; n < rows; ++n)
{
int num = n;
stringstream ss;
do
{
ss << (int)num % base;
num = num/base;
} while (num > 0);
string s = ss.str();
reverse(s.begin(), s.end());
cout.width(cols);
cout.fill('0');
cout << s << endl;
}
system("pause");
return 0;
}
#include <iostream>
#include <math.h>
#include <vector>
usingnamespace std;
int main()
{
//User input
int cols;//(columns in array)
cout<<"\n How many columns? :\t";
cin>>cols;
int rows = ( int ) pow( 3.0 , cols) ;
cout<<"\n The Number of possible combinations for the given columns are :\t"<<rows;
vector< vector<int> > MyArray(rows, vector<int>(cols));
int i,j,k = 0;
for (i=1;i<rows;i++) //Start with row 1
{
bool carry = true;
for (j=cols-1;j>=0;j--) // Note: reversed order
{
int value = MyArray[i - 1][j]; //Value from previous row
if(carry)
{
value += 1;
if(value < 3)
{
MyArray[i][j]=value;
carry = false;
}
else
{
MyArray[i][j]=0;
}
}
else
{
MyArray[i][j]=value;
}
}
}
cout << endl;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
cout << MyArray[i][j];
}
cout << endl;
}
return 0;
}
How many columns? : 3
The Number of possible combinations for the given columns are : 27
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222