
#include<stdio.h>
#include<conio.h>
void create(int **t,int n,int r);
void print(int *t,int n);
main()
{
int *a,ele,r;
printf("how many rows you want in tridiagnal matrix :");
scanf("%d",&r);
ele=3*r-2;
create(&a,ele,r);
print(a,r);
getch();
}
void create(int **t,int n,int r)
{
int p,q,i,j,k,*a,*b,m=0,f,c=0;
printf("\nEnter the element of tridiagnal matrix");
a=(int*)malloc(n*sizeof(int));
b=(int*)malloc(n*sizeof(int));
for(p=0;p<n;p++)
{
f=0;
printf("\nEnter the postion row and column of element:");
scanf("%d%d",&i,&j);
if(j<i-1||i>=r||j>i+1)
{
printf("\nYou have entered the wrong poition of element.Try again");
p=p-1;
continue;
}
k=2*i+j;
for(q=0;q<m;q++)
{
if(b[q]==k)
{
f=1;
break;
}
}
if(f==1)
{
printf("\nAt the given postion you already entered the data. Do you want to override it? if YES press 1 if NO press 0");
scanf("%d",&c);
p=p-1;
if(c==1)
{
printf("\nEnter the value: ");
scanf("%d",&a[k]);
continue;
}
else
continue;
}
b[m]=k;
m++;
printf("\nEnter the value: ");
scanf("%d",&a[k]);
}
*t=a;
}
void print(int *t,int n)
{
int i,j,k,s;
for(i=0;i<n;i++)
{
printf("\n");
for(s=0;s<i-1;s++)
printf(" ");
for(j=i-1;j<=i+1;j++)
{
if(j==-1||j==n)
continue;
k=2*i+j;
printf("%d ",t[k]);
}
}
}