Transpose an arbitrarily sized rectangular Matrix.




DEFINE PTR="CARD"
 
TYPE Matrix=[
  BYTE width,height
  PTR data] ;BYTE ARRAY
 
PROC PrintB2(BYTE b)
  IF b<10 THEN Put(32) FI
  PrintB(b)
RETURN
 
PROC PrintMatrix(Matrix POINTER m)
  BYTE i,j
  BYTE ARRAY d
 
  d=m.data
  FOR j=0 TO m.height-1
  DO
    FOR i=0 TO m.width-1
    DO
      PrintB2(d(j*m.width+i)) Put(32)
    OD
    PutE()
  OD
RETURN
 
PROC Create(MATRIX POINTER m BYTE w,h BYTE ARRAY a)
  m.width=w
  m.height=h
  m.data=a
RETURN
 
PROC Transpose(Matrix POINTER in,out)
  BYTE i,j
  BYTE ARRAY din,dout
 
  din=in.data
  dout=out.data
  out.width=in.height
  out.height=in.width
  FOR j=0 TO in.height-1
  DO
    FOR i=0 TO in.width-1
    DO
      dout(i*out.width+j)=din(j*in.width+i)
    OD
  OD
RETURN
 
PROC Main()
  MATRIX in,out
  BYTE ARRAY din(35),dout(35)
  BYTE i
 
  FOR i=0 TO 34
  DO
    din(i)=i
  OD
  Create(in,7,5,din)
  Create(out,0,0,dout)
  Transpose(in,out)
 
  PrintE("Input:")
  PrintMatrix(in)
  PutE() PrintE("Transpose:")
  PrintMatrix(out)
RETURN