Thread: Matrix Product

  1. #1
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15

    Matrix Product

    Hello (again) guys! Since I finished my last project (thanks to you, who helped me!) I started a new one and I'm stuck again ) The perks of being a newbie...

    Here is what I want to do:

    1.Insert number of rows & columns for matrix A
    2.Check If the no. of rows and columns are higher than 10, 10 being the maximum allowed number of rows/columns.
    3.Insert values for each position
    4.Idem 1&2&3 for matrix B
    5.Check if the rows of first matrix(A) equals the columns of the second matrix(B) -> If they are equal proceed. If not "Restart the program"
    Question here: Is there any way to automatically start again the "program" if the rows(A) and columns(B) are not equal ? Like a goto ?
    6.If they are equal set no. of rows for matrix C (the product matrix from A*B) as the no. of rows for matrix A, same for columns for matrix C from matrix B.
    2nd Question here: I don't know how to write the rule for the calculation... I thought of something like C[i][j] = A[i][j] * B[i][j] + A[i+1][j] * B[i][j+1].
    But this way it won't stop (I think) when it reaches the last position in the row/column :-/

    Here is the CODE, thanks for being an wonderful community and for the help!

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
     
     
    int main()
     
     
    {
        int A[10][10],B[10][10],C[10][10],RowA,ColA,RowB,ColB,i,j,RowC,ColC;
     
        printf ("Enter the number of rows for the first matrix: ");
        scanf ("%d", &RowA);
        if ( RowA > 10 )
        {
            printf ("The value is too hight, the maximum size is 10!\nPlease insert new value: ");
            scanf ("%d", &RowA);
        }
        while ( RowA > 10 );
    
    
        printf ("Enter the number of columns for the first matrix: ");
        scanf ("%d", &ColA);
        if ( ColA > 10 )
        {
            printf ("The value is too hight, the maximum size is 10!\nPlease insert new value: ");
            scanf ("%d", &ColA);
        }
        while ( ColA > 10 );
         
        for (i=0 ; i<RowA ; i++)
        for (j=0 ; j<ColA ; j++)
        {
            printf ("Enter the value for A[%d][%d]: ", i+1, j+1);
            scanf ("%d", &A[i][j]);
        }
     
     
        printf ("Enter the number of rows for the second matrix: ");
        scanf ("%d", &RowB);
        if ( RowB > 10 )
        {
            printf ("The value is too hight, the maximum size is 10!\nPlease insert new value: ");
            scanf ("%d", &RowB);
        }
        while ( RowB > 10 );
    
    
        printf ("Enter the number of columns for the second matrix: ");
        scanf ("%d", &ColB);
        if ( ColB > 10 )
        {
            printf ("The value is too hight, the maximum size is 10!\nPlease insert new value: ");
            scanf ("%d", &ColB);
        }
        while ( ColB > 10 );
     
     
        for (i=0 ; i<RowB ; i++)
        for (j=0 ; j<ColB ; j++)
        {
            printf ("Enter the value for B[%d][%d]: ", i+1, j+1);
            scanf ("%d", &B[i][j]);
        }
     
     
        if ( RowA != ColB )
        {
            printf ("The number of rows for matrix A must equal de number of columns for matrix B!\nPlease restart the program!\n");
        }
        else
        {
            RowC = RowA;
            ColC = ColB;
    
    
            for (i=0 ; i<RowC ; i++);
            {
                for (j=0 ; j<ColC ; j++);
            }
        }
        system ("pause");
    }
    Last edited by Cazan Adelin; 04-01-2015 at 08:35 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Cazan Adelin View Post
    5.Check if the rows of first matrix(A) equals the columns of the second matrix(B) -> If they are equal proceed. If not "Restart the program"
    Question here: Is there any way to automatically start again the "program" if the rows(A) and columns(B) are not equal ? Like a goto ?
    Don't use goto! You have several options.

    1. See how you use a "do-while" loop to validate each individual row input for A and B? Why not have those within an outer loop that will check if both are equal - it's the same principle. This will require you to change the prompts around in your code.

    2. Or even easier, just have one row input and apply it to both matrices.

    3. If the rows are not equal, print an error message and exit. The user would have to re-run the program to try again.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Cazan Adelin View Post
    I thought of something like C[i][j] = A[i][j] * B[i][j] + A[i+1][j] * B[i][j+1]
    No, that's not matrix multiplication!

    If you multiply A[n1][n2] by B[n2][n3], the result matrix is C[n1][n3], where n1 is the number of rows in A and C, and n3 is the number of columns in B and C.

    To calculate the entire matrix, you need three nested loops. The two outermost loops loop over each element in C, and the innermost calculates the sum of A[i1][i]*B[i][i3], where 0 <= i < n2.

    See the Wikipedia article on matrix multiplication.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Even your matrix test for compatibility is backwards I'm afraid. If C = A * B then, rC = rA, cC = cB and cA = rB, where the prefix r means number of rows and c number of columns.

    The formula for multiplying a matrix is C[i,k] = sum_j A[i,j] * B[j,k] where sum_j means sum over all valid j indices. i,k is the i'th row and k'th column of the output C matrix.

    If I were teaching you, I'd give you extra credit for solving the matrix multiplication problem using only recursion. No for loops allowed. The idea leads to a clever algorithm for doing sparse matrix multiplication FYI. If you want to torment your teacher I'd solve that way anyway.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    Why thank you :3
    This week I have a lot of projects for other classes so I don't know if I'll work something... I'll keep you updated! I'll try both ways.
    Thanks again for your time & response.

  6. #6
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    Ok, I'm back!
    So I thought of two ways:
    1.I keep the current "verification" with "Please restart the program".
    2.I delay the input of the values in both matrices and I only ask for no. of rows/columns for each of them. After that I use a while (RowA!=ColB) followed up by an error message explaining the problem then the initial lines asking for no. of rows/columns.

    I think both are correct, since my teacher didn't ask for something like 2nd option (to verify while in program and if it's wrong to ask again for the inputs) I will stick to the first option.

    As for the product part I thought of something like this:

    Code:
       for ( i = 0; i < RowA; i++)
       {
          for ( j = 0; j < ColB; j++)
          {
             sum = 0;
             for ( k = 0; k < RowA; k++) // RowA or ColB, I think both are fine, don't know for sure tho...
               {
             sum = sum + a[i][k] * b[k][j];
               }
           c[i][j] = sum;
          }
       }
    What do you think ? Will this work ? Should I try something else ?
    Thanks in advance!

  7. #7
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    Also I talked with a classmate about this and he sent me his file, this is how he solved the product part:

    Code:
        for(i=0; i<row1; ++i)    for(j=0; j<col2; ++j)
        {
           mult[i][j]=0;
        }
    
    
    
    
        for(i=0; i<row1; ++i)
        for(j=0; j<col2; ++j)
        for(k=0; k<col1; ++k)
        {
            mult[i][j]+=a[i][k]*b[k][j];
        }
    where (in my file):
    row1 = RowA
    col2 = ColB
    col1 = ColA

    What do you think of this way ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Matrix product using macro definition
    By ElNoob in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 09:44 PM
  3. Matrix product
    By Cotizo in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2008, 10:10 AM
  4. Dot Product.......Help
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-07-2003, 08:49 AM