Matrices

This is a discussion on Matrices within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on multiplying matrices for homework and I'm stuck for a long time. This is my assignment: {x}(n+1) ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    Matrices

    Hi,

    I'm working on multiplying matrices for homework and I'm stuck for a long time. This is my assignment:

    {x}(n+1) = [K] {x}(n), n = 0,1,2, ... ,8,9.

    In other words, the next vector {x} is equal to the product of [K] and
    the current vector {x}.


    I have the basic program already but I couldn't get it to multiply the current matrix. Instead, it would only multiply the initial matrix.

    Part I'm stuck on:

    Code:
    for(n=1; n<=10; n++)
    { 
    printf("\nNew Vector:\n");
    printf("y[%d] : [", n);
    matrix_mult(K, x, y, 4);
    printf(" ]\n"); 
    
    }
    K and x are the matrices I have to multiply together (they're already defined), y is the resulting matrix. I don't know how to make it so that the program will take K*y next instead of K*x(0). Please help?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    So, basically, you need to assign y to x for the next iteration of your loop?

    Well, doing that depends on what type x and y are (assuming they're the same type, of course). If they're arrays, you can use memcpy() like so:
    Code:
    int newarray[3][3], array[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
    memcpy(newarray, array, sizeof(array));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    14
    Thanks for helping.

    Yes! I need to assign y to x for the next loop. And yes, they're arrays. But is there another way to do it? I haven't seen memcpy() before so I don't know if that's allowed on the homework.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Well, you could always just use a for loop, or a nested for loop for a two dimensional array, which is what I'm assuming your array is:
    Code:
    int newarray[3][3], array[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
    int x, y;
    
    for(x = 0; x < 3; x ++) {
        for(y = 0; y < 3; y ++) newarray[x][y] = array[x][y];
    }
    or even
    Code:
    int newarray[3][3], array[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
    int x, y;
    
    for(x = 0; x < sizeof(array)/sizeof(*array); x ++) {
        for(y = 0; y < sizeof(*array)/sizeof(**array); y ++) newarray[x][y] = array[x][y];
    }
    But wait, let me guess. You haven't covered sizeof() . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    14
    I've seen sizeof() already but sorry I forgot to tell you that x and y are both one dimensional array. I tried to modify what you gave me, however, it's not working. I'm still getting the same answer before--K*x(0) instead.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Well, for one dimensional arrays, it's quite simple.
    Code:
    int newarray[9], array[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int x;
    
    for(x = 0; x < sizeof(array)/sizeof(*array); x ++) {
        newarray[x] = array[x];
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 08:51 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  4. Comparing Matrices (D3D9)
    By MicroFiend in forum Game Programming
    Replies: 2
    Last Post: 10-12-2005, 08:36 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

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