Thread: Matrices multiplication

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    33

    Matrices multiplication

    I am unable to input the correct form for matrices multiplication. I have an exam tomorrow in which I need to use this. Please help me.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        float a[10][10], b[10][10], c[10][10];
        int i, j, k, l, n=0, m=0, x=0, y=0, sum=0;
        printf("Enter the number of rows and collumns of the first matrix");
        scanf("%d %d", &n, &m);
        printf("What are the elements of the first matrix?\n");
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                scanf("%f", &a[i][j]);
            }
        }
        printf("Enter the number of rows and collumns of the second matrix");
        scanf("%d %d", &k, &l);
        if (m != l)
        {
            printf("these matrices cannot be multiplied");
        }
        else
        {
            printf("What are the elements of the second matrix?\n");
            for (i = 0; i < k; i++)
            {
                for (j = 0; j < l; j++)
                {
                    scanf("%f", &b[k][l]);
                }
            }
            printf("Your matrices are \n");
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < m; j++)
                {
                    printf("%f ", a[i][j]);
                    
                }
                printf("\n");
            }
            printf("\n");
            for (i = 0; i < k; i++)
            {
                for (j = 0; j < l; j++)
                {
                    printf("%f ", b[k][l]);
                }
                printf("\n");
            }
            printf("\n");
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < k; j++);
                {    sum = 0;
                    for (x = 0; x < l; x++)
                    {
                        sum += a[i][x] * b[x][j];
    
                    }
                    c[i][j] = sum;
                    sum = 0;
                }
            }
            printf("\n");
            printf("The multiplication of the matrices is");
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < l; j++)
                {
                    printf("%f\t", c[i][j]);
                    printf("\t");
                }
                printf("\n");
            }
            printf("\n");
        }
        getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (j = 0; j < k; j++);
    What's that ; at the end?
    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.

  3. #3
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Please be a bit more specific. I'm not that keen on running a program where I have to be sitting around inputting values into matrices all night, and your variable names are pretty off putting, so I really don't want to spend time reading your code either to be honest.

    I would actually advice that you have 3 precomputed matrices, one for the first matrix, one for the the second and one for the answer. Then you define a fourth one, which you use to store the result on running your algorithm on the first 2 matrices. That way you don't have to spend time on inputting values while testing and you can have the third matrix to compare your computed answer with, so you know that you did it right.

    Then add the code for inputting values, when you know that the rest of the code is correct, if you really feel the need to have manual input.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Line 55 in your previous listing. Specific enough?
    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.

  5. #5
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by grumpy View Post
    Line 55 in your previous listing. Specific enough?
    I think you maybe misunderstood my post, which was directed towards the OP and not Salem, or maybe you where giving the OP a nudge?

    Either way, your post was very specific indeed! :-)

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Jimmy View Post
    I think you maybe misunderstood my post, which was directed towards the OP and not Salem, or maybe you where giving the OP a nudge?
    Naah, your and Salem's posts were fine. I just decided to respond with a specific anyway
    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.

  7. #7
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    The more direct approach. I like it! :-)

  8. #8
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    This is what I did. It works now

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int first[10][10], second[10][10], i, j, m, n, l, k, x, sum = 0, multiplication[10][10];
        printf("Enter the number of rows and columns of the first matrix\n");
        scanf("%d %d", &m, &n);
        printf("Enter the elements of the first matrix\n");
        for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &first[i][j]);
        printf("Enter the number of rows and columns of the second matrix\n");
        scanf("%d %d", &l, &k);
        printf("Enter the elements of the second matrix\n");
        for (i = 0; i < l; i++)
        for (j = 0; j < k;j++)
            scanf("%d", &second[i][j]);
        printf("Your matrices are \n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
                printf("%d\t", first[i][j]);
            printf("\n");
        }
        printf("\n");
        for (i = 0; i < l; i++)
        {
            for (j = 0; j < k; j++)
                printf("%d\t", second[i][j]);
            printf("\n");
        }
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < k; j++)
            {
                for (x = 0; x < l; x++)
                {
                    sum = sum + first[i][x]*second[x][j];
                }
    
                multiplication[i][j] = sum;
                sum = 0;
            }
        }
        
    
        printf("The multiplication of the matrices is:\n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < k; j++)
                printf("%d \t", multiplication[i][j]);
            printf("\n");
        }
        
        
        
        
    
    
    
        getch();
        
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c-program on multiplication of matrices using functions
    By dishagarg in forum C Programming
    Replies: 4
    Last Post: 02-28-2013, 11:14 AM
  2. Replies: 9
    Last Post: 04-20-2009, 12:34 PM
  3. Matrices
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2003, 07:10 PM
  4. Matrices
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2002, 10:22 AM
  5. Matrices and Multiplication
    By SyntaxBubble in forum Game Programming
    Replies: 3
    Last Post: 01-07-2002, 09:53 PM

Tags for this Thread