#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(); }

# multiplying matrices

Page 1 of 1## 10 Replies - 806 Views - Last Post: 23 December 2010 - 01:18 AM

### #1

# 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?

##
**Replies To:** multiplying matrices

### #2

## 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|

### #3

## Re: multiplying matrices

Posted 22 December 2010 - 02:08 PM

Aphex19, 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

### #4

## Re: multiplying matrices

Posted 22 December 2010 - 02:12 PM

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

in general Matrix Multiplication is a little more complicated than you think.

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

in general Matrix Multiplication is a little more complicated than you think.

### #5

## Re: multiplying matrices

Posted 22 December 2010 - 02:27 PM

I know about matrix multiplication, I'm having problem at coding it

### #6

## 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(); }

### #7

## 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

### #8

## 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:

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

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

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]?

### #9

## Re: multiplying matrices

Posted 22 December 2010 - 05:32 PM

NickDMax, 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:

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

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

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]

NickDMax, 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

### #10

## 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

### #11

## 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

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

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**,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

Page 1 of 1