Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matrices multiplication
12-30-2009, 06:08 AM
Post: #1
matrices multiplication
here's the code for matrices multiplication in Pascal :

Code:
Program two_matrices_cross(input,output);
  (*programmer s-t,number:8811021 Programmer:alireza mahallati date:1.8.88
  This program will input two matrices and will output both of them and
  their cross.*)
  Uses CRT;
  Var
    Matrix1:Array[1..30,1..30] of real;
    Matrix2:Array[1..30,1..30] of real;
    Matrix3:Array[1..30,1..30] of real;
    Rows,Columns1,Columns2,Counter1,Counter2,Counter3:integer;
    Element,Sum:real;
Begin
  CLRSCR;
  Textcolor(Yellow);
  Writeln('How many rows does your first matrix have?');
  Readln(Rows);
  Writeln('How many Columns does your first matrix have?');
  Readln(Columns1);
  Writeln('Choose Columns for second matrix ..');
  Readln(Columns2);
  Writeln('Your first matrix is ',Rows,' x ',Columns1);
  Writeln('Your second matrix is ',Columns1,' x ',Columns2);
  Writeln('-----------------');
  Textcolor(Green);
  Writeln;
  Writeln('Now enter the elements of your first ',Rows,' X ',Columns1,' matrix');
  For Counter1:=1 To Rows Do
    Begin
      For Counter2:=1 To Columns1 Do
        Begin
          Read(Element);
          Matrix1[Counter1,Counter2]:=Element;
        End;
    End;
  Writeln('Now enter the elements of your Second ',Columns1,' X ',Columns2,' matrix');
  For Counter1:=1 To Columns1 Do
    Begin
      For Counter2:=1 To Columns2 Do
        Begin
          Read(Element);
          Matrix2[Counter1,Counter2]:=Element;
        End;
    End;
  Writeln;
  Writeln('----------------------');
  Counter2:=1;
  Counter3:=1;
  For Counter1:=1 To Rows Do
    Begin
      Counter3:=1;
      Repeat
        Begin
          While Counter2 <= Columns1 Do
            Begin
              Sum:=Sum+Matrix1[Counter1,Counter2]*Matrix2[Counter2,Counter3];
              Counter2:=Counter2+1;
            End;
          Matrix3[Counter1,Counter3]:=Sum;
          Sum:=0;
          Counter2:=1;
          Counter3:=Counter3+1;
        End;
      Until Counter3>Columns2;
    End;
  Textcolor(White);
  Writeln;
  Writeln('Matrix1 is -> ');
  Writeln;
  For Counter1:=1 To Rows Do
    Begin
      For Counter2:=1 To Columns1 Do
        Begin
          Write(Matrix1[Counter1,Counter2]:6:2,' ');
        End;
      Writeln;
    End;
  Writeln('Matrix2 is -> ');
  Writeln;
  For Counter1:=1 To Columns1 Do
    Begin
      For Counter2:=1 To Columns2 Do
        Begin
          Write(Matrix2[Counter1,Counter2]:6:2,' ');
        End;
      Writeln;
    End;
  Writeln('-------------------');
  Writeln;
  Textcolor(brown);
  Writeln('Matrix1 X Matrix2 is  ->');
  Writeln;
  For Counter1:=1 To Rows Do
    Begin
      For Counter2:=1 To Columns2 Do
        Begin
          Write(Matrix3[Counter1,Counter2]:6:2,' ');
        End;
      Writeln;
    End;
  repeat
    begin
      delay(1000);
    end;
  Until Keypressed;
End.

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
12-30-2009, 06:15 AM
Post: #2
RE: matrices multiplication
Very good i must say. People usually think that multiplication of matrix is a(i,j)*b(i,j) but it isn't.
It is a bit more complex and that's why this code is a bit long.
More about matrix multiplication you can find here :
http://en.wikipedia.org/wiki/Matrix_multiplication

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-30-2009, 05:31 PM
Post: #3
RE: matrices multiplication
Thanks,and one more thing for the length of my codes is that i frequently use extra lines,for example when I want to write : here's your matrix maybe four or five lines I use as example:
Code:
textcolor(something);
writeln;
writeln;
writeln('here's your matrix');
write('---------------------------');
and that's because I respect users,I'm code for them,not for myself or my teacher assistant
these things trough the program becomes more than 30-40 lines for me,Lol , cuz of this my teacher assistant always nagging to me that what the code is this?!I think I have to shorten the length of my codes.

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
12-30-2009, 06:47 PM (This post was last modified: 12-30-2009 06:49 PM by drdebcol.)
Post: #4
RE: matrices multiplication
It is okay, if you are coding for users than okay. You can make it shorter, but the point is that you write it clear with moving to the right when needed.

BTW one more thing, i have only one single tip on how to shorten your code in this case.
You declared matrix 3 times :
Code:
Matrix1:Array[1..30,1..30] of real;
    Matrix2:Array[1..30,1..30] of real;
    Matrix3:Array[1..30,1..30] of real;
But you could declare them in one line like variables :
Code:
Matrix1,Matrix2,Matrix3:Array[1..30,1..30] of real;

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-30-2009, 07:26 PM
Post: #5
RE: matrices multiplication
(12-30-2009 06:47 PM)drdebcol Wrote:  It is okay, if you are coding for users than okay. You can make it shorter, but the point is that you write it clear with moving to the right when needed.

BTW one more thing, i have only one single tip on how to shorten your code in this case.
You declared matrix 3 times :
Code:
Matrix1:Array[1..30,1..30] of real;
    Matrix2:Array[1..30,1..30] of real;
    Matrix3:Array[1..30,1..30] of real;
But you could declare them in one line like variables :
Code:
Matrix1,Matrix2,Matrix3:Array[1..30,1..30] of real;
thanks friend Wink

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: