Thread: multiplying two matrices

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    multiplying two matrices

    I'm trying to use a function to multiply two matrices. But I am not supposed to use brackets []. Also the function only accept single pointers as arguments. How can I rewrite the algorithm to use pointer arithmetic?

    Code:
    void mult_matrices(int *A, int *B, int *C, int m, int n, int p){
            int i, j, k;
    
            for(i=0; i < m; i++){
                    for(j=0; j < p; j++) {
                            C[i][j] = 0;
                            for(k=0; k < n; k++){
                                    C[i][j] += A[i][k] * B[k][j];
                            }
                    }
            }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, it would help if you started with code that worked in a valid way on the arrays. The code you have supplied will not even compile (A, B, and C are pointers to int, so cannot be used with two subscripts viz A[i][k]).

    Once you get that sorted out, I'm not going to solve your problem for you, but will give you two hints. I have deliberately crafted those hints so you need to think, but they do point you in the right general direction.

    If x is a one-dimensional array, or a pointer to the first element in a one-dimensional array, then x[i] is equivalent to *(x + i).

    Things break down a little after that (a 2D array is not quite equivalent to a pointer-to-pointer): however, a 2D array is a 1D array of 1D arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 06:53 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. multiplying matrices
    By RenderedAwake in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2005, 11:36 PM
  4. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 06:07 AM
  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

Tags for this Thread