Wednesday, 27 August 2014

SPARSE MATRIX


#include<stdio.h>
#include<conio.h>
int main()
{
 int a[3][3],i,j;
 printf("\nEnter the element of the sparse matrix mostly zero");
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     printf("\n%d row %d column",i,j);
     scanf("%d",&a[i][j]);              
    }              
  }
  printf("\nSparse matrix is given by\n");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     printf("%d\t",a[i][j]);             
    }    
    printf("\n");
  }
 
  printf("\nRight diagonal elements...\n");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i==j)
     printf("%d\t",a[i][j]);
     else
     printf("\t");             
    }    
    printf("\n");
  }
  printf("\nLeft diagonal elements...\n");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i+j==2)
     printf("%d\t",a[i][j]);
     else
     printf("\t");             
    }    
    printf("\n");
  }
  printf("\nUpper left tringular matrix...\n");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i+j<=2)
     printf("%d\t",a[i][j]);
     else
     printf("\t");             
    }    
    printf("\n");
  }
 
  getch();
}
 
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,*p,num;
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");
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");
        }
    }
        printf("enter a element of matrix to find that location ");
        scanf("%d",&num);
    for(i=0;i<5;i++)
{
   for(j=0;j<5;j++)
  {
      if(mat[i][j]==num)
       p=mat+2*(5*j+i);
        }
  }
printf("location of element=%d",p);
    getch();
    return 0;
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,*p,num;
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");
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");
        }
    }
        printf("enter a element of matrix to find that location ");
        scanf("%d",&num);
    for(i=0;i<5;i++)
{
   for(j=0;j<5;j++)
  {
      if(mat[i][j]==num)
       p=mat+2*(5*j+i);
        }
  }
printf("location of element=%d",p);
    getch();
    return 0;
}

A TRIDIAGNAL MATRIX AND FIND THE ADDRESS OF A GIVEN ELEMENT IN THAT MATRIX

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,*p,num;
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");
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");
        }
    }
        printf("enter a element of matrix to find that location ");
        scanf("%d",&num);
    for(i=0;i<5;i++)
{
   for(j=0;j<5;j++)
  {
      if(mat[i][j]==num)
       p=mat+2*(5*j+i);
        }
  }
printf("location of element=%d",p);
    getch();
    return 0;
}

//Address of element a[ij] for diagonal matrix of column major order
#include<stdio.h>
 #include<conio.h>
 main()
 {
  int a,b
  int n=0,m,row,col,row1,col1,add=0;
  printf("\n Enter rows of the matrix:");
  scanf("%d",&row);
  printf("\n Enter column of the matrix:");
  scanf("%d", &col);
  if(row==col)
  {
   printf("\n Enter row of the element:");
   scanf("%d",&row1);
   printf("\n Enter the column of the element:");
   scanf("%d",&col1);
   printf("\n Enter the base address:");
   scanf("%d", &m);
   for(a=1;a<=row1;a++)
   {
    for(b=1;b<=col1;b++)
    {
      n=n+2+2*(b-2)+a;
    }
   } 
   add=n+m-1;
   printf("\n The address of a[%d][%d] in column major order of a tridiagonal matrix is = %d",row1,col1,add);
  }
  else
  {
   printf("\n Please enter the order of a square matrix");
  }
  getch();
  return 0;
 }
Output:
 Enter rows of the matrix:10
Enter column of the matrix:10
Enter row of the element:9
Enter column of the element:8
Enter base address:255
The address of a[9][8] is 1118

SPARSE MATRIX

                            /* sparse matrix */

#include<stdio.h>
#include<conio.h>
int main()
{
int A[10][10],B[10][3],m,n,s=0,i,j;
printf("\nEnter the order m x n of the sparse matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\n%d row and %d column: ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("The given matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf("\nThe sparse matrix is given by");
printf("\n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
getch();
}



output
                                       
 Enter the order m*n of the sparse matrix
3
3
Enter the elements in the sparse matrix(mostly zeros)
0 row and 0 column 1
0 row and 1 column 1
0 row and 2 column 0
1 row and 0 column 0
1 row and 1 column 0
1 row and 2 column 10
2 row and 0 column 2
2 row and 1 column 2
2 row and 2 column 0
The given matrix is:
1 1 0
0 0 10
2 2 0
The sparse matrix is given by
1 0 0
1 0 1
10 1 2
2 2 0
2 2 1

                        sparse matrix



#include <stdio.h>

int main()
{
    int i, j, r, c, array[10][10];

    printf("Enter the r and c value:");
    scanf("%d%d", &r, &c);
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= c; j++)
        {
            printf("array[%d][%d] = ", i, j);
            scanf("%d", &array[i][j]);
        }
    }
    printf("matrix is");
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= c; j++)
        {
            printf("%d", array[i][j]);
        }
        printf("\n");
    }
    for (i = 1; i <= r; i++)
    {
        printf("\n");
        for (j = 1; j <= c; j++)
        {
            if (i >= j)
            {
                printf("%d", array[i][j]);
            }
            else
            {
                printf("\t");
            }
        }
    }
    printf("\n\n");
    for (i = 1; i <= r; i++)
    {
        printf("\n");
    for (j = 1; j <= c; j++)
    {
        if (j >= i)
        {
            printf("%d", array[i][j]);
        }
        else
        {
            //printf("\t");
        }
        // printf("\n");
    }
    
}
getch();
}


output



 

SPARSE MATRIX(this prog is done by me and arnab)

SPARSE MATRIX




#include<stdio.h>
#include<conio.h>
int main()
{
     typedef struct sparse
     {
             int row;
             int col;
             int value;
      }stype;
           
             stype s[10];
             int i,j,k=1,x;
             printf("\n enter matrix elements one by one\n");
             for(i=0;i<4;i++)
             {
                             for(j=0;j<4;j++)
                             {
                                             scanf("%d",&x);
                                             if(x!=0)
                                             {
                                                     s[k].row=i;
                                                     s[k].col=j;
                                                     s[k].value=x;
                                                     k++;
                                              }
                              }
               }
                                                     printf("\n ROW \t COL \t VALUE ");
                                                     for(i=1;i<k;i++)
                                                     printf("\n %d \t %d \t %d",s[i].row,s[i].col,s[i].value);
                                                     getch();
                                                     return 0;
     }

     /*SPARSE MATRIX*/

#include<stdio.h>
#include<conio.h>
int main()
{
     typedef struct sparse
     {
             int row;
             int col;
             int value;
      }stype;
            
             stype s[10];
             int i,j,k=1,x;
             printf("\n enter matrix elements one by one\n");
             for(i=0;i<4;i++)
             {
                             for(j=0;j<4;j++)
                             {
                                             scanf("%d",&x);
                                             if(x!=0)
                                             {
                                                     s[k].row=i;
                                                     s[k].col=j;
                                                     s[k].value=x;
                                                     k++;
                                              }
                              }
               }
                                                     printf("\n ROW \t COL \t VALUE ");
                                                     for(i=1;i<k;i++)
                                                     printf("\n %d \t %d \t %d",s[i].row,s[i].col,s[i].value);
                                                     getch();
                                                     return 0;
     }

PROGRAM

     /*SPARSE MATRIX*/

#include<stdio.h>
#include<conio.h>
int main()
{
     typedef struct sparse
     {
             int row;
             int col;
             int value;
      }stype;
           
             stype s[10];
             int i,j,k=1,x;
             printf("\n enter matrix elements one by one\n");
             for(i=0;i<4;i++)
             {
                             for(j=0;j<4;j++)
                             {
                                             scanf("%d",&x);
                                             if(x!=0)
                                             {
                                                     s[k].row=i;
                                                     s[k].col=j;
                                                     s[k].value=x;
                                                     k++;
                                              }
                              }
               }
                                                     printf("\n ROW \t COL \t VALUE ");
                                                     for(i=1;i<k;i++)
                                                     printf("\n %d \t %d \t %d",s[i].row,s[i].col,s[i].value);
                                                     getch();
                                                     return 0;
     }

                             sparse matrix



#include <stdio.h>

int main()
{
    int i, j, r, c, array[10][10];

    printf("Enter the r and c value:");
    scanf("%d%d", &r, &c);
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= c; j++)
        {
            printf("array[%d][%d] = ", i, j);
            scanf("%d", &array[i][j]);
        }
    }
    printf("matrix is");
    for (i = 1; i <= r; i++)
    {
        for (j = 1; j <= c; j++)
        {
            printf("%d", array[i][j]);
        }
        printf("\n");
    }
    for (i = 1; i <= r; i++)
    {
        printf("\n");
        for (j = 1; j <= c; j++)
        {
            if (i >= j)
            {
                printf("%d", array[i][j]);
            }
            else
            {
                printf("\t");
            }
        }
    }
    printf("\n\n");
    for (i = 1; i <= r; i++)
    {
        printf("\n");
    for (j = 1; j <= c; j++)
    {
        if (j >= i)
        {
            printf("%d", array[i][j]);
        }
        else
        {
            //printf("\t");
        }
        // printf("\n");
    }
   
}
getch();
}


output