Friday, January 25, 2013

Procedure for Creating Sparse Matrix

Procedure of Sparse Matrix

Procedure createsparematrix(A: array[1..30, 1..30] of Integers; M,N:Integers)
    B: array[0..30, 1..3] of Integers
    i,j,k : Integers
begin
     B[0,1]=M;
     B[0,2]=N;
     K=0;
     For i= 1 to m do
     Begin
        For j= 1 to n do
        Begin
             If A[i, j]<> 0 then
               Begin
                 K=k+1;
                 B[k,1]=i;
                 B[k,2]=j
                 B[k,3]= A[i,,j];
               end
        end
     end
 B[0,3]=k;
End


Program for Sparse Matrix

program
  A= array[1…30, 1…30] of integers;
  B= array[0 …30, 1…3] of integers;
   i,j,m.n; Integers
begin
   Writeln(“enter size of matrix”);
    Read(m,n)
   Writeln(“enter elements into sparse matrix”);
   For i= 1 to n
   For j= 1 to m
    Read(A[i,j]);
  
  Createsparematrix(A,B,m,n);
  Writeln(“3-tuples is as follows”);
  For i= 0 to B[0,3]
  For j = 1 to 3
    Begin
       Write (B[i, j],      “);
    End
    Writeln;
end

No comments:

Post a Comment