Thread: Matrix - Logic error?

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Matrix - Logic error?

    I need to create a new matrix by multiplying matrix B with matrix C.
    B is:
    1 2 3
    4 5 6
    7 8 9


    C is:

    9 8 7
    6 5 4
    3 2 1


    I'm doing:
    1 * 9 + 2 * 6 + 3 * 3;
    1 * 8 + 2 * 5 + 3 * 2; etc

    It should print:
    30 24 18
    84 69 54
    138 114 90

    But for some reason the last row results in 144, 119, 94.

    Here is the code:

    Code:
    #include <stdio.h>
    
    int main(void) {
    
        int i = 0, j = 0;
    
        /* h) matriz produto base na B e C. {{{ */
        int B[3][3] = {{1, 2, 3},
                       {4, 5, 6},
                       {7, 9, 9}};
    
        int C[3][3] = {{9, 8, 7},
                       {6, 5, 4},
                       {3, 2, 1}};
    
        /* Inicializa todos os elementos da matriz
         * com 0 como valor default. */
        int M_PRODUTO[3][3] = {{0}};
    
        int counter = 0;
        /* Itera sobre as linhas. */
        for (i = 0; i < 3; i++) {
            /* Itera sobre as colunas. */
            for (j = 0; j < 3; j++) {
    
                for (counter = 0; counter < 3; counter++) {
                    M_PRODUTO[i][j] += ((B[i][counter]) * (C[counter][j]));
                }
    
                printf("\t%d", M_PRODUTO[i][j]);
            }
            printf("\n");
        } 
    
        printf("\n\n");
    
        return 0;
    
    }
    I'm compiling it with:
    Code:
    gcc -std=c99 -Wall mult_matr.c -o ./Bin/mult_matr
    If I compute it on the paper or using a calculator the result is fine. Only when running the C program I get that wrong last row. If the logic is wrong, why do do program work fine for the first two rows?

    Any insights are welcome.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    int B[3][3] = {{1, 2, 3},
            {4, 5, 6},
            {7, 9, 9}};
    Look at the last row. Shouldn't that be an 8?

  3. #3
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by anduril462 View Post
    Code:
    int B[3][3] = {{1, 2, 3},
            {4, 5, 6},
            {7, 9, 9}};
    Look at the last row. Shouldn't that be an 8?
    Of course it should. Thank you very much.

    I really need the be more careful in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 06:42 PM
  2. Error in logic
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-28-2010, 08:02 AM
  3. I think it's logic error
    By c++ in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2009, 08:02 PM
  4. Error in logic
    By mesmer in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 06:42 AM
  5. logic error
    By sscook69 in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 06:00 PM