Thread: matrix

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    30

    Thumbs up matrix

    Got a program to multiply 2 matrices. Below is my source code:


    Code:
    #include "stdio.h"
    #include "conio.h"
    
    void main()
    {
       static int a[10] [10];
       static int b[10] [10];
       static int c[10] [10];
    
       int i,j,k,l,m,n,p,q;
    
       clrscr();
    
       printf ("Put the dimension for array A ,i.e. row x column \n");
       scanf ("%d %d",&m,&n);
    
       printf ("Put the dimension for array B ,i.e. row x column \n");
       scanf ("%d %d",&p,&q);
    
       if (n!=p)
       {
          printf("Multiplication of matrices is not possible \n");
    
          printf ("Dimension of matrices is not valid");
    
       }
    
       else
       {
    
         
          printf ("Input the element in the array A \n");
    
    
          for (i=0; i<m; i++)
          {
    
    	 for (j=0; j<n; j++)
    	 {
    	    scanf ("%d",&a[i] [j]);
    
    	 }
    
          }
    
          printf("\nThe array  A is as follows \n");
    
    
          for(i=0; i<m; i++)
          {
    
    	 for(j=0;j<n;j++)
    	 {
    
    	
    	    printf("%d ", a[i][j]);
    
    	 }
    	 printf("\n");
    
         }
    
          
          
          printf ("\nInput the elements in the array B \n");
    
    
          for (k=0; k<p; k++)
          {
    
    	 
    	 for (l=0; l<q; l++)
    	 {
    
    	    scanf ("%d",&b[k] [l]);
    	 }
          }
    
          printf("\nThe array b is as follows: \n");
    
          for(k=0;k<p;k++)
          {
    
    	 for(l=0;l<q;l++)
    	 {
    	    printf("%d ", b[k][l]);
    
    	 }
                     printf("\n");
           }
    
          for (i=0; i<m; i++)
          {
    	 for (l=0; l<q; l++)
    	 {
    	    for (j=0; j<n; j++)
    	    {
    	       c[i] [l] = c[i] [l] + a[i] [j] * b[j] [l];
    	    }
    
    	
    	 }
    
          }
          printf("\n");
          printf("The result is as follows: \n");
    
          for (i=0; i<m; i++)
          {
    	   l=0;
    	   while (l<q)
    	   {
    	      printf (" %d ",c[i] [l]);
    	      l++;
    
    	   }
                       printf ("\n");
          
          }
       
       }
    
       getch();
    }


    Do u think I can make it simpler?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    for (i=0; i<m; i++)
    {
    	 for (l=0; l<q; l++)
    	 {
    	    for (j=0; j<n; j++)
    	    {
    	       c[i] [l] = c[i] [l] + a[i] [j] * b[j] [l];
                        }
    
    	
    	 }
    }
    The inner j loop can be unrolled. In this for/next structure the inner j loop will take longer than the m and l loops. You could also use a one dimensional array instead of a 2 to represent a matrix.

    0 3 6
    1 4 7
    2 5 8

    or

    01 11 21
    02 12 22
    03 13 23

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM