Search This Blog

Thursday, October 13, 2011

The trace of a square is given by the sum of the diagonal elements. Write a program to evaluate the trace of a matrix. Read the elements of matrix from keyboard and pass on its address to a function to evaluate and return the traces

   1:   #include<stdio.h>
   2:   #include<conio.h>
   3:   float trace(int n,float **x);
   4:   void main()
   5:   {
   6:   int i,j;
   7:   float *xx[10];
   8:   float t,x[10][10],num;
   9:   int n;
  10:   printf("Enter the size of matrix:\n");
  11:   scanf("%d",&n);
  12:   printf("Enter the elements:\n");
  13:   for(i=0;i<n;i++)
  14:   {
  15:      for(j=0;j<n;j++)
  16:      {
  17:      scanf("%f",&num);
  18:      x[i][j]=num;
  19:      }
  20:   }
  21:   for(i=0;i<n;i++)
  22:   xx[i]=x[i];
  23:   t=trace(n,xx);
  24:   printf("trace of matrix is: %f",t);
  25:   getch();
  26:   }
  27:   
  28:   float trace(int n,float **x)
  29:   {
  30:   int i;
  31:   float t=0.0;
  32:   for(i=0;i<n;i++)
  33:   t=t+*(*(x+i)+i);
  34:   return(t);
  35:   }

No comments:

Post a Comment