Friday, July 31, 2009

C++ multiplying 2d 3x3 matrix arrays in a loop?

How do I multiply each element of the matrix, say by 5, to display in output code?





#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[3][3];


float matrixTwo[3][3];








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





//*************assigning numbers to the first matrix*********


for (int row = 0; row %26lt; 3; row++)


for (int column = 0; column %26lt; 3; column++)


{


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


cin %26gt;%26gt; matrixOne[row][column];


}


//************************************...





//calculation goes here ******





//***********





//*********outputting final matrix ***********


int row, column;





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


{


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


cout %26lt;%26lt; matrixOne[row][column];


cout %26lt;%26lt; endl;


}


//************************************...








cout %26lt;%26lt;"Type quit to exit " %26lt;%26lt; endl;


cin%26gt;%26gt;quit;

C++ multiplying 2d 3x3 matrix arrays in a loop?
well...I feel like i am doing your hw for you...but since i'm a bit rusty on my c++, I'm going to...pseudo this for you...the calculation section anyways. There are different ways you can do this, but off the top of my head, I thought of 2 similar ways





======================


1) take each input while its happening and multiply it right there before you put it in the matrix. example: after the cin%26gt;%26gt;matrixOne[row]column]; line, put something like matrixOne[row][column] = matrixOne[row][column] x 5;


=======================


2) if you need the actual calculations in the calculation section, you would need to do the whole nested loop again, [for (int row....) for (int column...) and in the nested loop, do the whole matrixOne[row][column] = matrixOne[row][column] x 5; line again.


=======================





And just a side note, it doesn't really really matter, but if you were to turn this program in as is, you might want to look at the following issues:





1) float matrixOne[3][3]; and float matrixTwo[3][3]; if you are putting in [3][3] in place of row and column, since row and column are both int, the declaration of float isn't necessary, because those values will never be a non whole number. Float takes up more memory in a program, and in bigger programs, you want to cut down on the memory space as much as possible.





2) If you are only going to use the matrixOne[3][3] and matrixTwo[3][3] for the row and column, you might want to consider coding it like this:


int row = 3;


int column = 3;


float matrixOne[row][column];


float matrixTwo[row][column];





that way, if you have to change all the number of rows in the whole program (changing your matrix size), you wouldn't have to go through each line that says float matrixOne[3][3]; and change them, you could just change the line that says int row = 3;.





3) putting float matrix[3][3] is declaring 4 spaces in memory for each row and column (you have 0, 1, 2, 3) and in the program, you only fill up 3 of those spaces with value. - for (int row = 0; row %26lt; 3; row++) notice the row %26lt; 3, and not row %26lt;=3.





4) instead of putting int row and int column in your for loop, and then later in your output, you should just put int once on top, like a global declaration. Besides, wont this give you an error like declaring it twice? I dont remember. It will help later one when you learn about functions and classes.





finally


5) cout type quit to exit, and cin quit. quit is a char, meaning it just takes one letter. so if you type quit, anything inputted after the first character is pointless. If you want to use char for quit, you can just tell the user to type q to exit.





Good luck with your programming!
Reply:hello friend press F7(function key) to trace the path of the program!


No comments:

Post a Comment