two dimensional array

This is a discussion on two dimensional array within the C Programming forums, part of the General Programming Boards category; given below is a code about multiplication of two matrices m1(m*n) and m2(n*p) and the multilplied matrix is stored in ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    two dimensional array

    given below is a code about multiplication of two matrices m1(m*n) and m2(n*p) and the multilplied matrix is stored in m3.

    for(i=0;i<m;i++)
    for(j=0;j<p;j++)
    sum=0;
    for(k=0;k<n;k++)
    sum=sum+*(*(m1+i)+k)*(*(*(m2+k)+j))
    *(*(m3+i)+j)=sum;
    this code is basically is in a function where i took the base addresses of the all three matrixes.
    The compiler shows error for the last two code lines.
    I will be thankful to every help.

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    my matmult solution

    using pointers makes it a little difficult to read/write..

    this is what i wrote for my matrix multiplication program:

    Code:
    void multmatrix (float a[100][100], float b[100][100], float c[100][100], int rowsa, int rowsb, int colsb) {
    int i,j,k;
        for (i=1; i<=rowsa;i++){
        for (j=1; j<=colsb;j++){
        c[i][j]=0;
         for (k=1; k<=rowsb; k++){
          c[i][j]= c[i][j]+a[i][k]*b[k][j];
         }
        }
      }
    }
    oh and...

    typedef float matrix[50][50];
    Last edited by moonwalker; 08-04-2002 at 10:53 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    that's true moonwalker that pointer makes the program difficult,but the question actually was to write a fuction of multiplication of matrixes and above all i had to print the multilpied matrix in the main.So, i had to do it by using pointers,by returning the address of the third matrix.

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    >the question actually was to write a fuction of multiplication of matrixes
    Didn't I show you a function that multiplies the matrices???
    it is very easy to tweak it to work directly from main..
    is there a problem?

    >and above all i had to print the multilpied matrix in the main.
    well, figure out your multiplication thing properly first...
    printing from main is fairly simple..
    Does using the above function obstruct you from printing
    the matrix from main ????

    the point is... you need not complicate the simple thing...
    especially with pointers, when it is intimidating.
    anyway, here's my whole program..

    Code:
    //Matrix Calculator//
    
    #include <stdio.h>
    #include <conio.h>
    
    typedef float matrix[50][50];
    
    void takematrix (char name, matrix box, int rows, int cols)
    {
    	int i , j;
    	printf("For Matrix %c :\n", name);
    	for (i=1; i<=rows; i++)
    	{
    		for (j=1; j<=cols; j++)
    		{
    			printf("Enter %c(%dx%d) :", name, i, j);
    			scanf("%f", &box[i][j]);
    		}
    	}
    }
    
    void showmatrix (char name, matrix box, int rows, int cols)
    {
    	int i, j;
    	printf("Matrix %c :\n", name);
    
    	for (i=1; i<=rows; i++)
    	{
    		for (j=1; j<=cols; j++)
    		{
    			printf("%6.2f ", box[i][j]);
    		}
    
    		printf("\n");
    	}
    
    }
    
    void addmatrix(matrix box1, matrix box2, matrix box3, int rows, int cols )
    {
    	int i, j;
    
    	for (i=1; i<=rows; i++)
    	{
    		for (j=1; j<=cols; j++)
    		{
    			box3[i][j]=box1[i][j]+box2[i][j];
    		}
    	}
    }
    
    
    void submatrix(matrix box1, matrix box2, matrix box3, int rows, int cols)
    {
    	int i, j;
    
    	for (i=1; i<=rows; i++)
    	{
    		for (j=1; j<=cols; j++)
    		{
    			box3[i][j]=box1[i][j]-box2[i][j];
    		}
    	}
    }
    
    
    void multmatrix(matrix box1, matrix box2, matrix box3, int rows1, int rows2, int cols2)
    {
    	int i, j, k;
    
    	for (i=1; i<=rows1; i++)
    	{
    		for (j=1; j<=cols2; j++)
    		{
    			box3[i][j]=0;
    			for (k=1; k<=rows2; k++)
    			{
    				box3[i][j]=box3[i][j]+(box1[i][k])*(box2[k][j]);
    			}
    		}
    	}
    }
    
    
    main()
    {
     
     clrscr();
      int rowsa, colsa, rowsb, colsb, i, j;
      matrix A, B, C, D, E;
      char a, b, c, d, e;
      a='A';
      b='B';
      c='C';
      d='D';
      e='E';
    
      clrscr();
      printf("Matrix Calculator\n");
      printf("Programmed By Mahurshi Akilla\n");
      printf("http://mahurshi.tripod.com\n\n\n");
    
      printf("Enter order of matrix A (e.g. 3x5) : ");
      scanf("%dx%d", &rowsa, &colsa);
      printf("Enter order of Matrix B (e.g. 5x4) : ");
      scanf("%dx%d", &rowsb, &colsb);
    
      printf("\n");
    
      takematrix(a, A, rowsa, colsa);
      printf("\n");
    
      takematrix(b, B, rowsb, colsb);
      printf("\n");
    
      clrscr();
      showmatrix(a, A, rowsa, colsa);
      showmatrix(b, B, rowsb, colsb);
    
      if (rowsa==rowsb && colsa==colsb)
      {
    	addmatrix(A, B, C, rowsa, colsa);
    	showmatrix(c, C, rowsa, colsa);
    	printf("Where Matrix C = A+B\n");
    
    	submatrix(A, B, D, rowsa, colsa);
    	showmatrix(d, D, rowsa, colsa);
    	printf("Where Matrix D = A-B\n");
      }
      else
      {
    	printf("\n");
    	printf("Addition and Subtraction Could Not Be Carried Out.\n");
    	printf("Matrices 'C=A+B' and 'D=A-B' Do Not Exist\n");
    	printf("\n");
      }
    
    
      if (colsa==rowsb)
      {
    	multmatrix(A, B, E, rowsa, rowsb, colsb);
    	showmatrix(e, E, rowsa, colsb);
    	printf("Where Matrix E = A x B\n");
      }
    
      else
      {
    	printf("\n");
    	printf("Multiplication Could Not Be Carried Out.\n");
    	printf("Matrix 'E = A * B' Does Not Exist.\n");
      }
    
    
      getchar();
      getchar();
    
    }
    Hope this helps!

    I prefer writing seperate functions instead of cramming it up
    in main .. if you want to put the whole thing in main, you can
    do it with small changes..

    good luck
    Last edited by moonwalker; 08-04-2002 at 11:26 AM.

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Yes but moonwalker, your function only multiplies matrices of fixed size... saurav's version looks as if it's meant for matrices of dynamic size

  6. #6
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    huh

    what ?

    This one? :
    Code:
    for(i=0;i<m;i++) 
    for(j=0;j<p;j++) 
    sum=0; 
    for(k=0;k<n;k++) 
    sum=sum+*(*(m1+i)+k)*(*(*(m2+k)+j)) 
    *(*(m3+i)+j)=sum;

    The thing already has 2 errors... .. dont even know
    where it goes from here...

    by the way, you can easily change the array thing into pointer if you want from the code i just posted
    Last edited by moonwalker; 08-04-2002 at 12:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21