# adding matrices, help with switches

• 09-03-2007
quiet_forever
I'm writing a program in which I read in four matrices (A, B, C, D) and four commands and operate those commands. One of the commands is to add two matrices. The command reads in like this :
a A B
The 'a' tells the program it will be adding together two matrices and the letters following tell which matrices it will be adding from the four matrices. I wrote a switch to read in the matrices letters after the command. This is the switch:

Code:

```switch (matrix1)         {                 case 'A' :                         readfileA (fin, matrixA);                         break;                 case 'B' :                         readfileB (fin, matrixB);break;                 case 'C' :                         readfileC (fin, matrixC);break;                 case 'D' :                         readfileD (fin, matrixD);break;                 default :                         cout<<"Not a valid Matrix"<<endl;         }         switch (matrix2)         {                 case 'A' :                         readfileA (fin, matrixA);break;                 case 'B' :                         readfileB (fin, matrixB);break;                 case 'C' :                         readfileC (fin, matrixC);break;                 case 'D' :                         readfileD (fin, matrixD);break;                 default :                         cout<<"Not a valid Matrix"<<endl;         }```
In the command 'a A B' it goes to case 'A' for matrix1 and case 'B' for matrix two. So now I need to add the two together, which I know how to do. The problem I'm having is how to say in each case that what ever matrix the command lists first needs to change to matrix1 and whatever the command lists second needs to change to matrix2 so that I can add them together using these names. I need to do this so that I can easily add together the matrices that the command lists no matter what they are. I know that I could use if/else statements but then I would have to go through every possibility of combinations and that would be a lot of extra code.
So if the command is 'a A B' I need case 'A' to change matrixA to matrix1 and case 'B' to change matrixB to matrix2.
Please bear with me, I am a beginner programmer so this may be an easy fix for some but I just can't figure it out! Any kind of info would help. If you don't understand what I'm trying to do then ask and I can try to clarify. Thank You!
• 09-03-2007
Salem
Are your 4 matrices (A, B, C, D) the same size ?
• 09-04-2007
matsp
Ehm, are you talking about a function call where the arguments to the function are called matrix1 and matrix2?

In the code you've supplied, it looks like matrix1 and matrix2 are a variable of type char. So something isn't as it should be.

--
Mats
• 09-04-2007
quiet_forever
Yes the matrices are all the same size, 4 by 4.

What I'm trying to do is instead of listing all the possibilites of combinations such as, AA,AB,AC,AD,BB,BC,BD,CC,CD, I want to be able to use the switch to determine which matrices need to be added. So if the matrices to be added were to be matrixA and matrixB then when it would choose those matrices from the switches then it would add together those two matrices. matrix1 and matrix2 were just chars I used to get the commands from the file.
• 09-04-2007
matsp
I take it that you read the matrix each time? If so, why not have one variable for the first matrix (no matter which file it comes from), and a second variable for the second matrix (again, no matter which file it comes from). Something like this:

Code:

```switch (matrix1)         {                 case 'A' :                         readfileA (fin, matrixX);                         break;                 case 'B' :                         readfileB (fin, matrixX);break;                 case 'C' :                         readfileC (fin, matrixX);break;                 case 'D' :                         readfileD (fin, matrixX);break;                 default :                         cout<<"Not a valid Matrix"<<endl;         }         switch (matrix2)         {                 case 'A' :                         readfileA (fin, matrixY);break;                 case 'B' :                         readfileB (fin, matrixY);break;                 case 'C' :                         readfileC (fin, matrixY);break;                 case 'D' :                         readfileD (fin, matrixY);break;                 default :                         cout<<"Not a valid Matrix"<<endl;         }```
Now you can add matrixX with marixY.

--
Mats
• 09-04-2007
quiet_forever
Thanks
I'm not at home right now to try it but it definitly looks like it would work. This was exactly what I was trying to do and now that I look at it looks so simple : )
Thanks matsp!
• 09-04-2007
hk_mp5kpdw
Are the readfileX functions significantly different from each other that you really need them or could you drop all the various versions and go with a single function that reads into whatever matrix is passed in as the argument?
• 09-04-2007
Salem
Well if they're all the same, then I guess all the read functions are the same as well.

Some random snippets which should clue you in...
Code:

```int matrices[4][4][4]; matrix1 -= 'A';  // convert to 0, 1, 2, 3 matrix2 -= 'A'; readfile (fin, matrix[matrix1] ); readfile (fin, matrix[matrix2] ); switch ( command ) {   case 'a':     matAdd( matrix[matrix1], matrix[matrix2] );     break; }```