10 Replies - 628 Views - Last Post: 23 December 2010 - 01:18 AM Rate Topic: -----

#1 shahrooz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 19-December 10

multiplying matrices

Posted 22 December 2010 - 01:40 PM

I'm tryin to write a code for multiplying matrices but I'm stuck, can anyone help me?
#include <stdio.h>
#include <conio.h>
         int main()
             {
              int m , n , s , p , j = 0;
              float sum = 0 , x[20][20] , y[20][20] , z[20][20];
              do
               {
                printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n");
                scanf(" %d %d %d %d", &m , &n , &s , &p );
               }
              while ( (m < 0 || n < 0) || ( s < 0 || p < 0 ) || ( m != p || n!= s ));
              {
              for ( int t = 0 ; t < m ; t++ )
                      {
                       for( int u = 0 ; u < n ; u++ )
                            {
                             printf(" x[%d][%d] ", t + 1 ,u + 1 );
                             scanf("%f", &x[t][u] );
                            }
                      }
               for ( int t = 0 ; t < s ; t++ )
                      {
                       for( int u = 0 ; u < p ; u++ )
                            {
                             printf(" y[%d][%d] ", t + 1 , u + 1 );
                             scanf("%f", &x[s][p] );
                            }
                      }
              }
              for ( int t = 0 ; t < n ; t++ )
                   {
                    sum = 0;
                    j = 0;
                    while ( j < p )
                     {
                      for( int u = 0 ; u < m ; u++ )
                           sum += x[u][t] * y[t][u + j];
                           z[t][j] = sum ;
                      j++ ;
                     }
                   }
              for ( int t = 0 ; t < p ; t++ )
                    for ( int u = 0 ; u < m ; u++ )
                         { 
                          printf("%f\t", z[u][t] );
                          if ( u == p - 1 )
                               printf("\n");
                         }
              getch();
             }
                                          



Is This A Good Question/Topic? 0
  • +

Replies To: multiplying matrices

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: multiplying matrices

Posted 22 December 2010 - 01:48 PM

Just multiple each element with the correspoending other element, eg.

|1,2|   |9,3|   |9,6|
|3,4| * |1,2| = |1,8|

Was This Post Helpful? -3
  • +
  • -

#3 shahrooz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 19-December 10

Re: multiplying matrices

Posted 22 December 2010 - 02:08 PM

View PostAphex19, on 22 December 2010 - 12:48 PM, said:

Just multiple each element with the correspoending other element, eg.

|1,2|   |9,3|   |9,6|
|3,4| * |1,2| = |1,8|


are you sure? I think rows should be multiplied by columns but I can't write the code correctly

This post has been edited by shahrooz: 22 December 2010 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiplying matrices

Posted 22 December 2010 - 02:12 PM

@Aphex19 sorry... that is not how to multiply matrices.

|1,2|   |9,3|   |11, 7|
|3,4| * |1,2| = |31,17|


Posted Image


in general Matrix Multiplication is a little more complicated than you think.
Was This Post Helpful? 0
  • +
  • -

#5 shahrooz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 19-December 10

Re: multiplying matrices

Posted 22 December 2010 - 02:27 PM

I know about matrix multiplication, I'm having problem at coding it
Was This Post Helpful? 0
  • +
  • -

#6 shahrooz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 19-December 10

Re: multiplying matrices

Posted 22 December 2010 - 02:36 PM

this is my last try with a slight difference from the first code, but it's not working yet

#include <stdio.h>
#include <conio.h>
         int main()
             {
              int m , n , s , p , j = 0;
              float sum = 0 , x[20][20] , y[20][20] , z[20][20];
              do
               {
                printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n");
                scanf(" %d %d %d %d", &m , &n , &s , &p );
               }
              while ( (m < 0 || n < 0) || ( s < 0 || p < 0 ) || ( m != p || n!= s ));
              {
              for ( int t = 0 ; t < m ; t++ )
                      {
                       for( int u = 0 ; u < n ; u++ )
                            {
                             printf(" x[%d][%d] ", t + 1 ,u + 1 );
                             scanf("%f", &x[t][u] );
                            }
                      }
               for ( int t = 0 ; t < s ; t++ )
                      {
                       for( int u = 0 ; u < p ; u++ )
                            {
                             printf(" y[%d][%d] ", t + 1 , u + 1 );
                             scanf("%f", &x[s][p] );
                            }
                      }
              }
              for ( int t = 0 ; t < n ; t++ )
                   {
                    sum = 0;
                    j = 0;
                    while ( j < p )
                     {
                      for( int u = 0 ; u < m ; u++ )
                          {
                           sum += x[u][t] * y[t][u + j];
                           z[t][j] = sum ;
                          }                  
                      j++ ;
                     }
                   }
              for ( int t = 0 ; t < p ; t++ )
                    for ( int u = 0 ; u < m ; u++ )
                         { 
                          printf("%f\t", z[u][t] );
                          if ( u == p - 1 )
                               printf("\n");
                         }
              getch();
             }

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiplying matrices

Posted 22 December 2010 - 03:08 PM

Well for one... you never input y values.. you have x rather than y there -- line 27
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiplying matrices

Posted 22 December 2010 - 03:33 PM

my problem with this is that I can't never seem to figure out the matrix orientation.

so I if I have the matrix:

A = | a b c |
    | d e f |


element a is in A[0][0], where is element f?

A[2][1] or A[1][2]?
Was This Post Helpful? 0
  • +
  • -

#9 shahrooz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 19-December 10

Re: multiplying matrices

Posted 22 December 2010 - 05:32 PM

View PostNickDMax, on 22 December 2010 - 02:33 PM, said:

my problem with this is that I can't never seem to figure out the matrix orientation.

so I if I have the matrix:

A = | a b c |
    | d e f |


element a is in A[0][0], where is element f?

A[2][1] or A[1][2]?


I think f is A[3][2]

View PostNickDMax, on 22 December 2010 - 02:08 PM, said:

Well for one... you never input y values.. you have x rather than y there -- line 27

omg, mistyped it, thanks. I'll give it another shot now

not working yet
#include <stdio.h>
#include <conio.h>
         int main()
             {
              int m , n , s , p , j = 0;
              float sum = 0 , x[20][20] , y[20][20] , z[20][20];
              do
               {
                printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n");
                scanf(" %d %d %d %d", &m , &n , &s , &p );
               }
              while ( (m < 0 || n < 0) || ( s < 0 || p < 0 ) || ( m != p || n!= s ));
              {
              for ( int t = 0 ; t < m ; t++ )
                      {
                       for( int u = 0 ; u < n ; u++ )
                            {
                             printf(" x[%d][%d] ", t + 1 ,u + 1 );
                             scanf("%f", &x[t][u] );
                            }
                      }
               for ( int t = 0 ; t < s ; t++ )
                      {
                       for( int u = 0 ; u < p ; u++ )
                            {
                             printf(" y[%d][%d] ", t + 1 , u + 1 );
                             scanf("%f", &y[s][p] );
                            }
                      }
              }
              for ( int t = 0 ; t < n ; t++ )
                   {
                    sum = 0;
                    j = 0;
                    while ( j < p )
                     {
                      for( int u = 0 ; u < m ; u++ )
                          {
                           sum += x[u][t] * y[t][u + j];
                           z[t][j] = sum ;
                          }                  
                      j++ ;
                     }
                   }
              for ( int t = 0 ; t < p ; t++ )
                    for ( int u = 0 ; u < m ; u++ )
                         { 
                          printf("%f\t", z[u][t] );
                          if ( u == p - 1 )
                               printf("\n");
                         }
              getch();
             }
                                          


This post has been edited by shahrooz: 22 December 2010 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: multiplying matrices

Posted 22 December 2010 - 09:30 PM

it should be &y[t][u] as t & u are your loop variables. But that still does not fix the multiplication
Was This Post Helpful? 0
  • +
  • -

#11 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: multiplying matrices

Posted 23 December 2010 - 01:18 AM

Ah, reminds me of my last assignment. We use pointer though.

For starters, I suggest that you use specific matrices with small size[2x2 or 3x3] in order to get the multiplication correct. Now. When we do a matrix multiplication, there is a pattern. From the given link by NickDMax,
Posted ImagePosted ImagePosted Image

If we make further observation for each element, it seems the first matrix have the same row and second matrix have the same column. In the normal nested for loop, maybe we more variable to deal with it[hint: maybe 2 instead of one, each dealing with row and column]. Calculate the sum in form of for loop.

This post has been edited by shad0wk1: 23 December 2010 - 01:21 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1