Thread: Multiplying Two Matrix!

  1. #1
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17

    Multiplying Two Matrix!

    I'm Confused! What's the problem?
    Please Help; Thanks.

    Code:
    #include <stdio.h>
    #include <conio.h>
    void input (int [][3], int, int [][4], int);
    int mult (int [][3], int, int [][4], int);
    void out (int [][4], int);
    int main ()
    {
        const int r = 2, c = 3;
        int mat1 [r][4], mat2 [c][4], mat [r][4];
        input (mat1, r, mat2, c);
        mult (mat1, r, mat2, c);
        mat = mult (mat1, r, mat2, c);
        out (mat, r);
        getch ();
        return 0;
    }
    void input (int mat1 [][3], int r, int mat2 [][4], int c)
    {
         for (int i = 0; i < r; i++)
             for (int j = 0; j < 3; j++)
             {
                 printf ("\nEnter mat1 [%d][%d]:\n", i, j);
                 scanf ("%d", &mat1 [i][j]);
                 }
                 for (int i = 0; i < c; i++)
                     for (int j = 0; j < 4; j++)
                     {
                         printf ("\nEnter mat2 [%d][%d]:\n", i, j);
                         scanf ("%d", &mat2 [i][j]);
                         }
                         }
                         int mult (int mat1 [][3], int r, int mat2 [][4], int c)
                         {
                             int mat [2][4];
                             for (int i = 0; i < r; i++)
                                 for (int j = 0; j < 4; j++)
                                     for (int k = 0; k < c; k++)
                                     mat [i][j] = mat1 [i][k] * mat2 [k][j];
                                     return mult;
                                     }
                                     void out (int mat [][4], int r)
                                     {
                                          printf ("\nThe produc of mat1 and mat2:\n");
                                          for (int i = 0; i < r; i++){
                                              for (int j = 0; j < 4; j++)
                                              printf ("%5d", mat [i][j]);
                                              printf ("\n");
                                              }
                                              }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    when you multiply try just using a nested for loop and not a triple nested for loop, take out the 'k' for loop, and replace the matrices with i and j.
    Also your mult function returns mult but what is mult??
    Last edited by camel-man; 01-08-2012 at 12:49 PM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should put more effort into framing your question. What's the problem?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn to format your code right.

    Code:
    #include <stdio.h>
    #include <conio.h>
    void input (int [][3], int, int [][4], int);
    int mult (int [][3], int, int [][4], int);
    void out (int [][4], int);
    int main ()
    {
        const int r = 2, c = 3;
        int mat1 [r][4], mat2 [c][4], mat [r][4];
        input (mat1, r, mat2, c);
        mult (mat1, r, mat2, c);
        mat = mult (mat1, r, mat2, c);
        out (mat, r);
        getch ();
        return 0;
    }
    void input (int mat1 [][3], int r, int mat2 [][4], int c)
    {
        for (int i = 0; i < r; i++)
            for (int j = 0; j < 3; j++)
            {
                printf ("\nEnter mat1 [%d][%d]:\n", i, j);
                scanf ("%d", &mat1 [i][j]);
            }
        for (int i = 0; i < c; i++)
            for (int j = 0; j < 4; j++)
            {
                printf ("\nEnter mat2 [%d][%d]:\n", i, j);
                scanf ("%d", &mat2 [i][j]);
            }
    }
    int mult (int mat1 [][3], int r, int mat2 [][4], int c)
    {
        int mat [2][4];
        for (int i = 0; i < r; i++)
            for (int j = 0; j < 4; j++)
                for (int k = 0; k < c; k++)
                    mat [i][j] = mat1 [i][k] * mat2 [k][j];
        return mult;
    }
    void out (int mat [][4], int r)
    {
        printf ("\nThe produc of mat1 and mat2:\n");
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < 4; j++)
                printf ("%5d", mat [i][j]);
            printf ("\n");
        }
    }
    Learn to compile with warnings on.
    Read the warnings; fix the warnings.

    Code:
    mingw32-gcc.exe -Wall -std=c99  -O2     -c main.c -o obj\Release\main.o
    main.c: In function 'main':
    main.c:10:5: warning: passing argument 1 of 'input' from incompatible pointer type [enabled by default]
    main.c:3:6: note: expected 'int (*)[3]' but argument is of type 'int (*)[4]'
    main.c:11:5: warning: passing argument 1 of 'mult' from incompatible pointer type [enabled by default]
    main.c:4:5: note: expected 'int (*)[3]' but argument is of type 'int (*)[4]'
    main.c:12:5: warning: passing argument 1 of 'mult' from incompatible pointer type [enabled by default]
    main.c:4:5: note: expected 'int (*)[3]' but argument is of type 'int (*)[4]'
    main.c:12:9: error: incompatible types when assigning to type 'int[(unsigned int)r][4]' from type 'int'
    main.c: In function 'mult':
    main.c:39:5: warning: return makes integer from pointer without a cast [enabled by default]
    main.c:34:9: warning: variable 'mat' set but not used [-Wunused-but-set-variable]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One of the poorer attempts at indentation we've seen in a good while.
    SourceForge.net: Indentation - cpwiki

    Referring to the OP post, not stahta01's clean up code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Problem? Your code looks like it's jumping off a cliff, that's the problem. stahta01 has very kindly shown you exactly what your code should look like.

    A real programmer will never stray from indenting their code perfectly in this manner. Make sure you do it always from now on.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a matrix inputed from a file
    By eater in forum C Programming
    Replies: 16
    Last Post: 04-21-2009, 09:34 AM
  2. matrix multiplying
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 02:17 PM
  3. Multiplying tangent and bitangent by scale matrix
    By psychopath in forum Game Programming
    Replies: 6
    Last Post: 02-08-2006, 08:28 AM
  4. multiplying with pointers
    By Isometric in forum C Programming
    Replies: 11
    Last Post: 11-30-2001, 03:03 PM
  5. Multiplying! PLEASE HELP!!!!!!!!!!!!!
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2001, 09:12 AM