iraxeje
iraxeje

Reputation: 33

Initializing a 2D Array C++

My goal is to read in a file at the command line, which is to be an adjacency matrix representation of a directed graph, and store the values in a 2D array. My plan is to read the number of integers on a line and use that as the size for my array(i.e. a 5 line input, with 5 integers per line would mean i would create a 5 x 5 array). However, to initialize an array, a constant value is needed, and counting the integers per line and storing it in a variable to use as my size parameter, does not allow me to create the array.

Sample input:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Code:

#include <iostream> 
#include <sstream>
#include <fstream>      

using namespace std;

int main(int argc, char **argv) 
{
    string currentLine;
    int i, m = 0;
    int count;
    ifstream input(argv[1]);
    int storage[10000];

    printf("Original matrix: \n" );
    if(input.is_open())
    {
        while(getline(input, currentLine))
        {
            istringstream iss(currentLine);
            count = 0;
            while(iss >> i)
            {
                if(iss.eof())//at end of each line, ends loop
                {
                    count++;
                    storage[m] = i;
                    m++;
                    printf("%d \n", i);
                    break;
                }
                else
                {
                    count++;
                    storage[m] = i;
                    m++;
                    printf("%d  ", i);
                }
            }
        }
    }

int **matrix;
    matrix = new int*[count]; 

    for(int y = 0; y < count; y++)
        matrix[y] = new int[count];

    for(int r = 0; r < count; r++)
        for(int c = 0; c < count; r++)
            matrix[r][c] = storage[r+c];

    printf("first = %d ", matrix[0][0]);


    system("PAUSE");
    return 0;
}

Based on my input, I should create a 5 x 5 array. But the line

int matrix[count][count];

Gives me an error saying that the "count" size parameter should be a constant. Is my way of counting the size of the input to use an invalid way of doing this, or is there a way to create a constant to use as my size param?

Upvotes: 1

Views: 2113

Answers (3)

Ferruccio
Ferruccio

Reputation: 100648

Instead of using 2D native C++ arrays, consider using a vector of vectors.

#include <vector>
#include <iostream>

int main() {
    using namespace std;

    int count = 5;

    // create a matrix of 'count' rows by 0 columns
    vector<vector<int>> matrix(count);

    // resize the column count for each row
    for (auto& r : matrix)
        r.resize(count);

    // use it just like an array
    for (int i = 0; i < count; ++i)
        matrix[i][i] = i;

    for (int r = 0; r < count; ++r) {
        for (int c = 0; c < count; ++c)
            cout << matrix[r][c] << ' ';
        cout << endl;
    }
}

Upvotes: 1

KalyanS
KalyanS

Reputation: 527

You are not going to get a 2-D array. You will have to work with Array of Array.

If you are willing to work with dynamic allocations, please do something like this:

int *myArray = new int [count];  // variable length array can get you into trouble here,
                                 // if you are not careful as the inner dimension needs
                                 // to be freed before the array goes out of scope.
for (/*iterate from 0 count*/) {
   myArray[i] = new int [count];
}


myArray[m][n] to give you a semblance of what you wanted.

Upvotes: 0

FFF
FFF

Reputation: 791

you will need to use a dynamic array

int *myArray;                //Declare pointer to type of array
myArray = new int[x];   //use 'new' to create array of size x
myArray[3] = 10;          //Use as normal (static) array
...
delete [] myArrray;       //remeber to free memeory when finished.

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

http://www.cplusplus.com/doc/tutorial/dynamic/

Upvotes: 0

Related Questions