Saturday, August 20, 2011

Create Upper triangular matrix.

//Create Upper triangular matrix.
#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 upper triangular matrix :");
      scanf("%d",&r);
      ele=(r*(r+1))/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 upper triangular 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||j>=r)
      {
             printf("\nYou have entered the wrong poition of element.Try again");
             p=p-1;
             continue;
      }
      k=i*(r-1)-((i*(i-1))/2)+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;s++)
       printf("  ");
      for(j=i;j<n;j++)
      {
       k=i*(n-1)-(i*(i-1))/2+j;
       printf("%d ",t[k]);
      }
     }
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More