Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two answers to this question. One of them is pretty straightforward (and assumes that you know the number of columns and rows of the input file in question).</p> <pre><code>for (int a=0; a&lt;NUMROW; a++) for (int b=0; b&lt;NUMCOL; b++) cin&gt;&gt;TWODARRAY[a][b]; </code></pre> <p>So that was pretty easy. For a variable length of data input, the long and ungainly answer I came up with is a bit more exciting. Still, I've tested it, and it works. Hope everyone's comfortable with pointers to pointers and stringstreams.</p> <pre><code>string in; int** twoDArray=NULL; int numRow=0; int numCol=0; string** inputHold=NULL; int inputSize=10; //Since we're using pointers instead of vectors, //we have to keep track of the end of the array. inputHold=new string*[inputSize]; while (getline(cin,in)){ //The string in now contains a line of input. if (numRow==0) //Assuming that the number of inputs per column is the same in each row, //we can save on computation by only checking the number of columns once. //Performance may be improved here- it might be more efficient to use str.find for (string::iterator iter=in.begin(); iter!=in.end(); iter++){ if (*iter==' ') numCol++; } //Store the string inputHold[numRow]=new string(in); numRow++; if (numRow==inputSize) { //If we're at the end of the array, we create a bigger one and repopulate the data. string** tmpInput=inputHold; int oldsize=inputSize; inputSize=inputSize*2; inputHold=new string*[inputSize]; for (int i=0; i&lt;oldsize; i++){ inputHold[i]=tmpInput[i]; } } } //The method that I used to determine the number of columns is mildly flawed- //it doesn't account for the end of line. So, we increment the counter. numCol=numCol+1; twoDArray=new int*[numRow]; for (int a=0; a&lt;numRow; a++) twoDArray[a]=new int[numCol]; for (int a=0; a&lt;numRow; a++){ istringstream parser(*(inputHold[a])); for (int b=0; b&lt;numCol; b++){ parser&gt;&gt;twoDArray[a][b]; } } for (int a=0; a&lt;numRow; a++) for (int b=0; b&lt;numCol; b++) cout&lt;&lt;twoDArray[a][b]&lt;&lt;endl; </code></pre> <p>Anyone got anything less complicated?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload