Thread: matrix multiplication

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    6

    matrix multiplication

    hello,

    Code:
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
    //freopen("conventionInput.txt","r",stdin);
    long long m1[5][100],i,j,k,m2[100][5],mult[100][100],r1,c1,r2,c2;
    char Manual = 'N';
    printf("Enter Matrix Input Manually? (Y/N)\n");
    Manual = getchar();
    Manual = toupper(Manual);
    printf("Enter variable Y MIN 32 MAX 100\n");
    scanf("%lld",&c1);
    r2 = c1;
    r1 = 5;
    c2 = 5;
    /* initialize random seed: */
    srand ( time(NULL) );
    
    /* generate secret number: */
    
    if(r2==c1)
    {
    printf("Enter rows and columns of First matrix \n");
    printf("Row wise\n");
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++){
    if(Manual == 'Y')
    scanf("%lld",&m1[i][j]);
    else
    m1[i][j] = rand() % 10 + 1;
    }
    }
    printf("You have entered the first matrix as follows:\n");
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++)
    printf("%lld\t",m1[i][j]);
    printf("\n");
    }
    printf("Enter rows and columns of Second matrix \n");
    printf("Again row wise\n");
    for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++){
    if(Manual == 'Y')
    scanf("%lld",&m2[i][j]);
    else
    m2[i][j] = rand() % 10 + 1;
    }
    }
    printf("You have entered the second matrix as follows:\n");
    for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
    printf("%lld\t",m2[i][j]);
    printf("\n");
    }
    
    printf("Now we multiply both the above matrix. Clock Time will start here \n");
    printf("The result of the multiplication is as follows:\n");
    //a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33
    clock_t start = clock();
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c2;j++)
    {
    mult[i][j]=0;
    for(k=0;k<r1;k++)
    {
    mult[i][j]+=m1[i][k]*m2[k][j];
    mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];
    }
    printf("%lld\t",mult[i][j]);
    }
    printf("\n");
    }
    
    printf("Time elapsed: %f ms \n", ((double)clock() - start) / CLOCKS_PER_SEC);
    
    getch();
    }
    else
    {
    printf("Matrix multiplication cannot be done");
    }
    }

    My problem now is this matrix multiplication coding not give the actual result after..can u check which part that cause this problem..

    tq.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A few tips to help you troubleshoot your program.

    1) Show the arrays, and verify the values are correct before any computation
    2) Use arrays with assigned values for troubleshooting - you don't want to have to enter the values, every time you run the program.
    3) Either step through the program and watch some array values change, or use printf to show you how things are going, during the computations.

    4) Start with small arrays first - one it's correct, then use the larger sizes.

    5) Practice your indentation style. It's crappy, as is. That eliminates a programmer from using their eye /brain training that they will have picked up, from long programming practice.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    6

    contd..

    hello,

    i have try with the small value but still not get actual value..the coding problem is for example i multiply the first row with first column i get too small value for the large size matrix..with the simple value input, example: 2^23=45..so the value false.

    1. why the result value too small?, while the multiplication process is ok.

    tq,

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, here's the problem for you:

    1) I'm not going to indent your program code for you. If you want to see your code as it should be seen, and help others to see it easily, you'll do that.

    2) I'm not going to enter 500 numbers for your m1 array. I asked you to make a small assigned array of values, for testing.

    You haven't done that.

    So your program will not get much help. You can't expect people to do the work on your program, just because you won't do it.

    Maybe someone will stumble upon your code error - but you should learn to help yourself (and others), troubleshoot a program.

    Right now, you're not really helping much.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Here is my two cents.

    I would start over from scratch. Your code is incredibly difficult to read without any indentation and it will probably be faster to just rewrite your code than get the spacing correct.

    There is one big problem with your code - you don't check the dimensionality of the matrices (at least not that I could see). You should always do that before multiplying two matrices because if the dimensions don't agree (i.e. the column number of the left matrix equals the row number of the right matrix) multiplication doesn't make sense.

    Your program would be much more flexible if you read your matrix in from a file. Asking for a user to input a matrix is a big pain for anything larger than a 3x3 and my guess is this is where your program is messing up. If you don't know how to read from a file then it will be a great learning experience.

    As for the algorithm - there are tons of different ways to loop through matrix multiplication. It looks like yours is fine (although I only glanced at it since it was hard to read) so your error is probably in a detail somewhere else. Assuming you don't have a special type of matrix (i.e. sparse, symmetric, etc..) your matrix multiplication should take n^3 operations. If it doesn't, something is wrong.

    For future reference, code that looks like this is MUCH more readable.

    Code:
    	for (int i = 0; i < m1; i++) {				/* Matrix multiplication C=AB */
    		for (int j = 0; j < n2; j++) {
    			for (int k = 0; k < n2; k++) {
    				C[i][j] += A[i][k]*B[k][j];
    			}
    		}
    	}
    Good luck

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I dunno, use the modern miracle which is the "indent" program.
    Fantastic for cleaning up code.
    Code:
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    {
      //freopen("conventionInput.txt","r",stdin);
      long long m1[5][100],i,j,k,m2[100][5],mult[100][100],r1,c1,r2,c2;
      char Manual = 'N';
      printf("Enter Matrix Input Manually? (Y/N)\n");
      Manual = getchar();
      Manual = toupper(Manual);
      printf("Enter variable Y MIN 32 MAX 100\n");
      scanf("%lld",&c1);
      r2 = c1;
      r1 = 5;
      c2 = 5;
      /* initialize random seed: */
      srand ( time(NULL) );
    
      /* generate secret number: */
    
      if(r2==c1)
      {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
        {
          for(j=0;j<c1;j++){
            if(Manual == 'Y')
            scanf("%lld",&m1[i][j]);
            else
            m1[i][j] = rand() % 10 + 1;
          }
        }
        printf("You have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
          for(j=0;j<c1;j++)
          printf("%lld\t",m1[i][j]);
          printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Again row wise\n");
        for(i=0;i<r2;i++)
        {
          for(j=0;j<c2;j++){
            if(Manual == 'Y')
            scanf("%lld",&m2[i][j]);
            else
            m2[i][j] = rand() % 10 + 1;
          }
        }
        printf("You have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
          for(j=0;j<c2;j++)
          printf("%lld\t",m2[i][j]);
          printf("\n");
        }
    
        printf("Now we multiply both the above matrix. Clock Time will start here \n");
        printf("The result of the multiplication is as follows:\n");
        //a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33
        clock_t start = clock();
        for(i=0;i<r1;i++)
        {
          for(j=0;j<c2;j++)
          {
            mult[i][j]=0;
            for(k=0;k<r1;k++)
            {
              mult[i][j]+=m1[i][k]*m2[k][j];
              mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];
            }
            printf("%lld\t",mult[i][j]);
          }
          printf("\n");
        }
    
        printf("Time elapsed: %f ms \n", ((double)clock() - start) / CLOCKS_PER_SEC);
    
        getch();
      }
      else
      {
        printf("Matrix multiplication cannot be done");
      }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-18-2010, 11:17 AM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM

Tags for this Thread