Thread: Matrix multiplication!!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Matrix multiplication!!

    I have created a class of 3*3 matrix. In which i have defined an operator for matrix multiplication.

    the code goes like this:
    Code:
    matrix operator * (matrix m)
    {
    matrix x;
    	for (int i=0; i<3; i++)
    		for (int j=0; j<3; j++)
    	x.array[i][j]=array[1][j] * m.array[i][1];
            x.array[i][j]=array[2][j] * m.array[i][2];
            x.array[i][j]=array[3][j] * m.array[i][3];
    
    return x;
    }


    can anybuddy help me this code is wrong

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Easy start: If you intend all of those statements to be inside a loop, you will need braces.

    You seem to have looked up matrix multiplication, but you forgot about the additions.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here's a hint:
    result[i][j] += array [i][k] * array[k][j]
    Last edited by VirtualAce; 10-18-2009 at 04:22 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Here's another important hint: Arrays start at zero. An array with three elements has valid indexes [0], [1], and [2]. You got the loops right, bt not your hard-coded indexes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Check your curly brackets, your code isn't doing what you think it is doing.

    You need to put brackets after the second for statement

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, 01:30 PM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM