Tuesday, July 19, 2011

Program to print tridiagonal matrix

/*Serial No.131     [swami110.cpp]*/

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

printf("\nTridiagnal matrix:\n\n");
printf("%d\t%d\n",mat[0][0],mat[0][1]);
for(i=1;i<5;i++)
    {
    for(k=1;k<i;k++)
        {
        printf("\t");
        }
    if(i==4)
        printf("%d\t%d\n",mat[4][3],mat[4][4]);
    else
        {
        for(j=i;j<5;j++)
            {
            if(i==j)
            printf("%d\t%d\t%d",mat[i][j-1],mat[i][j],mat[i][j+1]);
            }
        printf("\n");
        }
    }
getch();
}

11 comments:

  1. sir,please tell me how to write tridiagonal program using thomas algorithm

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. #include
      #include

      void main()
      {
      int i, j, c;
      clrscr();
      int a[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
      int b[5][5];
      for(i=0;i<5;i++)
      {
      for(j=0;j<5;j++)
      {
      c=i-j;
      if(c<0)
      {
      c=j-i;
      }
      if(c>1)
      {
      b[i][j]=0;
      }
      else
      {
      b[i][j]=a[i][j];
      }
      }
      }

      for(i=0;i<5;i++)
      {
      for(j=0;j<5;j++)
      {
      printf("%d\t",b[i][j]);
      }
      printf("\n");
      }

      getch();
      }





      header files are stdio.h ad conio.h

      Delete
    3. sir please output used to add output will also help to understand easily.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Sir can u give me program to check if the matrix is tridiagonal or not

    ReplyDelete
  4. i think the code is a fucked. It just doesn't provide the required output.

    ReplyDelete
  5. es ko aur achhe code likha ja skta tha ...

    ReplyDelete

If you have any doubt, feel free to ask...