Tuesday, July 28, 2009

C++, printing 2d arrays.?

Alright, this is part of a larger program, however I'm making sure I get each step done right, at a time. *addition details for further explanation of problem*





#include %26lt;iostream%26gt;


#include %26lt;math.h%26gt;


#include %26lt;string%26gt;


using namespace std;








char quit;





int main( )


{


//declaring the 2d matrix arrays


float matrixOne[2][2];


int matrixTwo[3][3];





cout %26lt;%26lt; "Week 5 Assignment, A-5B of two 2d 3x3 matrixes by Dustin Roach. " %26lt;%26lt;endl;





//assigning numbers to the first matrix, and displaying the output to user.





cout %26lt;%26lt;"Enter the first row of numbers, starting from the top" %26lt;%26lt; endl;


cin%26gt;%26gt;matrixOne[0][0];


cin%26gt;%26gt;matrixOne[1][0];


cin%26gt;%26gt;matrixOne[2][0];


cout %26lt;%26lt;"Enter the second row of numbers, starting from the top" %26lt;%26lt; endl;


cin%26gt;%26gt;matrixOne[0][1];


cin%26gt;%26gt;matrixOne[1][1];


cin%26gt;%26gt;matrixOne[2][1];


cout %26lt;%26lt;"Enter the third row of numbers, starting from the top" %26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[0][2];


cin%26gt;%26gt;matrixOne[1][2];


cin%26gt;%26gt;matrixOne[2][2];





cout %26lt;%26lt;"you have entered " %26lt;%26lt; matrixOne %26lt;%26lt; endl;


}

C++, printing 2d arrays.?
First of all, u need not do the task of getting the input by repeating the code. try this"





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


{


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


{


printf("enter the value for %d",matrix[i][j]);


scanf("%d",%26amp;matrix[i][j]);


}


}








Then while printing





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


{


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


{


printf("%d\t",matrix[i][j]);


}


printf("\n");


}








if u still need help, drop me a mail...
Reply:Try this.... (you should really use for loops)





#include "stdafx.h"


#include %26lt;iostream%26gt;


using namespace std;








int _tmain(int argc, _TCHAR* argv[])


{


int matrixOne[3][3];





cout%26lt;%26lt;"enter a number (row 0, col 0)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[0][0];


cout%26lt;%26lt;"enter a number (row 0, col 1)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[0][1];


cout%26lt;%26lt;"enter a number (row 0, col 2)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[0][2];


cout%26lt;%26lt;"enter a number (row 1, col 0)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[1][0];


cout%26lt;%26lt;"enter a number (row 1, col 1)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[1][1];


cout%26lt;%26lt;"enter a number (row 1, col 2)"%26lt;%26lt;endl;


cin%26gt;%26gt;matrixOne[1][2];





cout%26lt;%26lt;matrixOne[0][0]%26lt;%26lt;" "%26lt;%26lt;matrixOne[0][1]%26lt;%26lt;" "


matrixOne[0][2]%26lt;%26lt;endl;


cout%26lt;%26lt;matrixOne[1][0]%26lt;%26lt;" "%26lt;%26lt;matrixOne[1][1]%26lt;%26lt;" " matrixOne[1][2]%26lt;%26lt;endl;





return 0;


No comments:

Post a Comment