Thread: adding matrices, help with switches

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    adding matrices, help with switches

    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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are your 4 matrices (A, B, C, D) the same size ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    reply

    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    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!

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  3. Adding Arrays and Matrices
    By dlf in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2005, 02:36 PM
  4. Java: Sorting and Adding a Row in Table
    By alphaoide in forum Tech Board
    Replies: 4
    Last Post: 08-12-2005, 08:22 PM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM