Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to write a C++ program, that will take the user defined number of columns at run time and create a two dimensional array that will have

Columns = given at run time
Rows = 3Columns

How should I initialize a 2D array where the rows and columns are not initially constants. Any link or tutorial help will be appreciated.

share|improve this question
    
Are you using Windows ? –  Đēēpak Shãrmã Aug 28 '13 at 13:15
    
Are you using visual c++ on visual studio –  Đēēpak Shãrmã Aug 29 '13 at 4:28

2 Answers 2

I think you are looking for dynamic multi-dimensional arrays. Googlefu should help, leads to this article:

http://www.cplusplus.com/forum/beginner/63/

Look near the bottom.

share|improve this answer
    
Thank you Patrick. I'll take a look. –  GCODE Aug 28 '13 at 13:00

You provided us a little information, I guess you are asking for a C++ code that first takes column number from user and then calculates rows on the basis of formula and finally creates a two dimensional array, right ?
Formula given : rows = 3columns.

I've written a C++ code for you

I've tested this code on my linux( ubuntu ) platform , I hope this is what you are asking.


#include <iostream>
#include<math.h>
#define kary 3 // Your k-ary truth table - 3 for ternary truth table(0,1,2), 2 for binary truth table(1,2)

using namespace std;
int main()
{
    int rows,cols,i,j;  // Variable declaration
    cout<<"\n Enter required number of columns =  ";
    cin>>cols;
    rows = pow(kary,cols);
    cout<<"\n Calculated number of rows =  "<<rows;
    int arr[rows][cols]; // Array declaration
    for (i=0; i<rows; i++)
    {
        for (j=0; j<cols; j++)
           arr[i][j]=((i/(int) pow(kary, j)) % kary); //logic
    }
    cout<<"\n Your array holds this data \n";
    for(i = 0; i < rows; i++)
    {
        for (j=cols-1; j>=0; j--)
            cout<<"\t"<<arr[i][j];
        cout<<"\n";
    }
    return 0;
}
share|improve this answer
    
Thank you Vegetarian Vulture! However, the: "int arr[rows][cols];" brings several errors including C2057: expected constant expression, C2133: 'arr':unknown size, and C2466: cannot allocate an array of constant size 0. Any idea around that? That is what I was getting before. –  GCODE Aug 28 '13 at 12:57
    
When I try to run your code, those ^^ are the errors that I get. It works fine for you? –  GCODE Aug 28 '13 at 13:04
    
I got it to work. Thank you for your help Deepak. I am using Visual Studio. What happened with POW is I needed to declare it a double and not int. However, still having problems with declaring the array. –  GCODE Aug 28 '13 at 18:16
    
Nvm I figured that part out. –  GCODE Aug 28 '13 at 22:08
1  
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? –  GCODE Aug 29 '13 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.