Sunday, July 12, 2009

C++ user defined arrays?

Ok, last week my problem was getting two 3x3 matrices (whose elements were defined by the user) multiplied, and thanks to the help from here I got it figured out. This week, we're just supposed to stretch this out a bit, by letting the user state the size of his matrices. I thought it would be as simple as removing the 3 from:





const int row = 3;


const int column =3;





and just using a cin to let the user define these variables, but this small change spat back 22 errors at me. I take it you can't do this with an array, but then how do you let a user define it's size?





a snippit:


int main( )


{





const int row;


const int column;





int i, j;





cout %26lt;%26lt;"How many rows will this matrix have?" %26lt;%26lt; endl;


cin%26gt;%26gt;row;





cout %26lt;%26lt;"How many columns will this matrix have?" %26lt;%26lt; endl;


cin%26gt;%26gt;column;





cout %26lt;%26lt; "FIRST MATRIX (A):" %26lt;%26lt; endl;


for (i = 0; i %26lt; row; i++)


for (j = 0; j %26lt; column; j++)


{





cout %26lt;%26lt; "Enter value for row " %26lt;%26lt; i+1 %26lt;%26lt; " column " %26lt;%26lt; j+1 %26lt;%26lt; ": " %26lt;%26lt; endl;


cin %26gt;%26gt; matrixOne[i][j];


.


.


.

C++ user defined arrays?
Something like this should do








int main( )


{


int row;


int column;





int i, j;





cout %26lt;%26lt;"How many rows will this matrix have?" %26lt;%26lt; endl;


cin%26gt;%26gt;row;





cout %26lt;%26lt;"How many columns will this matrix have?" %26lt;%26lt; endl;


cin%26gt;%26gt;column;





int matrix[row][column];
Reply:You can't cin %26gt;%26gt; row; or cin %26gt;%26gt; column; if it defined as a const.


Remove "const" before int row and int column and it should start working.

artificial flowers

No comments:

Post a Comment