Search This Blog

Thursday, October 13, 2011

The trace of the matrix is given by the sum of its elements of its major diagonal. Write a program to evaluate the trace of the above matrix

       



   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,sum=0;
   6:  int x[4][4]={
   7:          {2,4,6,8},
   8:          {3,5,7,9},
   9:          {4,2,6,8},
  10:          {1,3,5,7},
  11:          };
  12:  for(i=0;i<4;i++)
  13:      {
  14:      for(j=0;j<4;j++)
  15:      {
  16:      if(i==j)
  17:      sum+=x[i][j];
  18:      printf("%5d",x[i][j]);
  19:      }
  20:      printf("\n");
  21:      }
  22:  printf("the trace of the matrix is: %d\n",sum);
  23:  getch();
  24:  }

No comments:

Post a Comment