Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
plz help me matrix multiplication 7 * 7 ! Posted by vahidpage on 3 Jun 2009 at 7:21 AM
plz help me matrix multiplication 7 * 7 !

Report
Re: plz help me matrix multiplication 7 * 7 ! Posted by Atex on 3 Jun 2009 at 8:48 PM
This converts a 2D square matrix into a spiral surveyed 1D one, hope it helps
program _matrix_;

uses crt; { needed for "clrscr" only }

const max_size=7; { <-- side size for matrix (i.e 7x7) }

type matrix_element=integer; { <-- type of an element }
     { matrix in 2D }
     matrix=array[1..max_size,1..max_size] of matrix_element;
     { matrix in 1D, surveyed }
     msurveyed=array[1..max_size*max_size] of matrix_element;


var i,j,k:byte;
    mx,my,dir,step:byte;
    m2d:matrix;
    m1d:msurveyed;
    trigs:array[1..max_size*max_size] of boolean; { direction triggers }


begin
 writeln;
 for i:=1 to max_size do  { get user to input matrix elements }
  for j:=1 to max_size do begin
   write('Enter element [',j,',',i,']  > ');readln(m2d[j,i]);
  end;

 clrscr;
 writeln('Matrix in 2D :');
 for i:=1 to max_size do         { display 2D matrix }
  for j:=1 to max_size do begin
   gotoxy(j*3,i*2);
   write(m2d[j,i]);
  end;
 writeln;
 writeln(#13#10,'Matrix in 1D :');

 mx:=max_size div 2+1;   { calculate midpoint }
 my:=mx;
 dir:=3;                 { initial direction }
 if not(odd(max_size)) then begin dec(mx);dir:=1;end;{ adjust for even numbers }

 fillchar(trigs,0,sizeof(trigs)); { init triggers with false ("0") }

 step:=1;
 k:=1;
 for i:=1 to max_size do begin    { calculate direction triggers }
   inc(k,step);
   if k<=max_size*max_size then trigs[k]:=true;
   inc(k,step);
   if k<=max_size*max_size then trigs[k]:=true;
   inc(step);
 end;

 k:=max_size*max_size;

 m1d[k]:=m2d[mx,my];  { get first element based on the calculations above }

 for i:=2 to k do begin { do the spiral survey }
   case dir of
    1:{right}inc(mx);
    2:{up   }dec(my);
    3:{left }dec(mx);
    4:{down }inc(my);
   end;
   m1d[k-pred(i)]:=m2d[mx,my];
   if trigs[i] then  {direction trigger}
    if dir=4 then dir:=1 else inc(dir);
 end;

 for i:=1 to k do    { display 1D matrix }
  write(m1d[i],', ');
 writeln;
 readln;
end.

Report
Re: plz help me matrix multiplication 7 * 7 ! Posted by Actor on 4 Jun 2009 at 1:42 PM
There are times when brute force is the best option.
Type
   Matrix = array [1 .. 7, 1 .. 7] of Integer ;
   Survey = array [1 .. 49] of Integer ;

   Procedure DoSurvey (Var S : Survey ; M : Matrix) ;
   begin
      S[1]  := M[1,1] ;
      S[2]  := M[2,1] ;
      S[3]  := M[3,1] ;
      S[4]  := M[4,1] ;
      S[5]  := M[5,1] ;
      S[6]  := M[6,1] ;
      S[7]  := M[7,1] ;
      S[8]  := M[7,2] ;
      S[9]  := M[7,3] ;
      S[10] := M[7,4] ;
      S[11] := M[7,5] ;
      S[12] := M[7,6] ;
      S[13] := M[7,7] ;
      S[14] := M[6,7] ;
      S[15] := M[5,7] ;
      S[16] := M[4,7] ;
      S[17] := M[3,7] ;
      S[18] := M[2,7] ;
      S[19] := M[1,7] ;
      S[20] := M[1,6] ;
      S[21] := M[1,5] ;
      S[22] := M[1,4] ;
      S[23] := M[1,3] ;
      S[24] := M[1,2] ;
      S[25] := M[2,2] ;
      S[26] := M[3,2] ;
      S[27] := M[4,2] ;
      S[28] := M[5,2] ;
      S[29] := M[6,2] ;
      S[30] := M[6,3] ;
      S[31] := M[6,4] ;
      S[32] := M[6,5] ;
      S[33] := M[6,6] ;
      S[34] := M[5,6] ;
      S[35] := M[4,6] ;
      S[36] := M[3,6] ;
      S[37] := M[2,6] ;
      S[38] := M[2,5] ;
      S[39] := M[2,4] ;
      S[40] := M[2,3] ;
      S[41] := M[3,3] ;
      S[42] := M[4,3] ;
      S[43] := M[5,3] ;
      S[44] := M[5,4] ;
      S[45] := M[5,5] ;
      S[46] := M[4,5] ;
      S[47] := M[3,5] ;
      S[48] := M[3,4] ;
      S[49] := M[4,4]
   end ;

Although crude, this solution is straightforward and easy to understand, a very important consideration. It may even be faster and more efficient than a more complex solution. You have to measure to be sure.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.