Read a 4x4 matrix from user input

This is a discussion on Read a 4x4 matrix from user input within the C++ Programming forums, part of the General Programming Boards category; I need to read in a user input matrix in the following format: Please enter a 4x4 matrix one row ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Read a 4x4 matrix from user input

    I need to read in a user input matrix in the following format:

    Please enter a 4x4 matrix one row at a time.

    Row 1: 1 3 -1 0

    Row 2: 2 4 1 0

    Row 3: 3 2 -2 2

    Row 4: 4 0 1 5

    I need to use the function:
    void getmatrix(double A[][4]);

    I then have to call it from the function main().

    I have other things to do but I think I can figure that stuff out if I can figure out how to read in the matrix.

    Any help would be appreciated.

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Search google for "matrix c++" or just matrices, or any combo of the three. Theres tons of tutorials and code. Its pretty simple too.

    As for void getmatrix(double A[][4]);, I'm not sure why its called getmatrix, instead of setmatrix, but you could just create a temp array (matrix) in main() and when you're done use that as the parameter for getmatrix().
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm not sure why its called getmatrix
    ...beause the function gets the matrix from the user.

    JHaney,

    Pretend the function prototype is this instead:

    void getmatrix(double A[4]);

    and you have to read in this data:

    Row 1: 1 3 -1 0

    How would you do it? Write the code, and get it to work.

    Once you get that working, you should be able to put that in a for loop and do it 4 times by letting the leftmost index value in the original function prototype vary with the loop index.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    ...beause the function gets the matrix from the user.
    A right, I was looking at it as if it were in a class.

    Edit: Ignore my first post. I was so out of it I assumed this function was part of a class and you were trying to set the matrix in that class by calling a function, but could only put it in one line at a time.
    Last edited by Dae; 11-29-2005 at 03:14 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    void getmatrix(double A[][4])
    {	
    	int row=0;
    	
    	for(int i=0;i<4;i++)
    	{
    		cout << "Row " << i+1 << ": ";
    		for(int j=0;j<4;j++)
    			cin >> A[i][j];
    	}	 	 
    }
    Is this something that you want to do? I had one sitting around, maybe it is useful for you to get the idea of how arrays are passed by reference

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hey jlf029,

    What do you think you're doing? Knock it off.
    Last edited by 7stud; 11-29-2005 at 02:52 AM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Thanks J. Yours is what I'm looking for. thanks

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Alright I'm stuck again. I now have to find the minor of a 4x4 matrix.

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    That link is good ^ except I had a good think about how I would find the minor but I can't seem to find a general solution. You would have to somehow use recursion. It's really hard to use loops as to find the minor you have to follow the rules of multiplication

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 07:10 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  5. Replies: 4
    Last Post: 04-21-2004, 05:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21