Tuesday, July 28, 2009

C++ noob needs help on populating 2d array?

hi, i'm starting out with c++ and have been writing a for-the-hell-of-it piece of code to calculate the product of two matrices. It runs but produced the wrong results. After hours of headaches I narrowed down the problem to the following code:





cout%26lt;%26lt;"enter the elements of matrix A from left to right, top to bottom %26gt;";


for (a=0; a%26lt;y1; a++)


{


for (b=0; b%26lt;x1; b++)


{


cin%26gt;%26gt;mat1[a][b];


}


}





where y1 and x1 are the height and width of matrix "mat1" respectively. The problem is that the numbers I enter get mixed up and the numbers that get put into the array are different.


example: If I declare the array mat1 to be of size x1=2 and y1=2, and input the numbers 1, 2, 3, 4 then when I display the array contents afterwards with cout, it turns out to be 1, 3, 3, 4.





If I enter the numbers 1,2,3,4,5,6,7,8,9 into a 3x3 array then it turns out to be


9 4 7


4 7 8


7 8 9





any help?

C++ noob needs help on populating 2d array?
did i not answer this before? look at your asked questions for my answer





the code is right. i don't see any errors in it. probably the printing, check your code make sure that there are no errors in the variables and the assigning of values.
Reply:One thing you still haven't shown us is the array declaration. I wanted to help you last time you asked, and still do, but I'm not seeing the whole picture here.





There mite be a problem in your declaration of array mat1[].


No comments:

Post a Comment