Transposing a Sparse Matrix Using Array [Part 1-Long]


 

#include<stdio.h>
#include<conio.h>
// transpose for the sparse matrix
main()
{
//clrscr();
int a[10][10],b[10][10];
int m,n,p,q,t,col;
int i,j;

printf(“enter the no of row and columns :\n”);
scanf(“%d %d”,&m,&n);

// assigning the value of matrix

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf(“a[%d][%d]= “,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n\n”);

//displaying the matrix

printf(“\n\nThe matrix is :\n\n”);

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
t=0;

printf(“\n\nthe non zero value matrix are :\n\n”);

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{

// accepting only non zero value
if(a[i][j]!=0)
{
t=t+1;
b[t][1]=i;
b[t][2]=j;
b[t][3]=a[i][j];
} }
}

// displaying the matrix of non-zero value

printf(“\n”);
printf(“a[0 %d %d %d\n”,m,n,t);

for(i=1;i<=t;i++)
{
printf(“a[%d %d %d %d \n”,i,b[i][1],b[i][2],b[i][3]);
}

b[0][1]=n; b[0][2]=m; b[0][3]=t;
q=1;

// transpose of the matrix

printf(“\n\nthe transpose of the matrix :\n “);

if(t>0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=t;j++)
{
if(b[j][2]==i)
{
a[q][1]=b[j][2]; a[q][2]=b[j][1];
a[q][3]=b[j][3]; q=q+1;
} }
} }

printf(“\n\n”);
printf(“a[0 %d %d %d\n”,b[0][1],b[0][2],b[0][3]);

for(i=1;i<=t;i++)
{
printf(“a[%d %d %d %d\n”,i,a[i][1],a[i][2],a[i][3]);
}
getch();
}

\n

String Pattern Matching in C using pointers


 

Block diagrams of (Left) pattern matching and ...
Block diagrams of (Left) pattern matching and substitution method and (Right) soft pattern matching method. (Photo credit: Wikipedia)

 

String Pattern Matching Program in C using Pointers

 

Its an application of Arrays

 

 

#include<stdio.h>
#include<conio.h>
int match(char*, char*);

main()
{
char a[100], b[100];
int position;

printf(“Enter some text\n”);
gets(a);

printf(“Enter a string to find\n”);
gets(b);

position = match(a, b);

if(position!=-1)
printf(“Found at location %d\n”, position+1);
else
printf(“Not found.\n”);

getch();
}

int match(char *a, char *b)
{
int c;
int position = 0;
char *x, *y;

x = a;
y = b;

while(*a)
{
while(*x==*y)
{
x++;
y++;
if(*x==”||*y==”)
break;
}
if(*y==”)
break;

a++;
position++;
x = a;
y = b;
}
if(*a)
return position;
else
return -1;
}

 

 

 

\n

 

Array Complete Program using C[DATA STRUCTURE]


 

Real (Laplace) spherical harmonics for to (top...
Real (Laplace) spherical harmonics for to (top to bottom) and to (left to right). The negative order harmonics are rotated about the axis by with respect to the positive order ones. (Photo credit: Wikipedia)

 

Here is what I present Before you is a complete array program where all operations of arrays are included eg:-

 

  1. Insertion

  2. Deletion

  3. Searching

  4. Sorting

  5. Dispay

 

#include <stdio.h>
#include <conio.h>
int n; //size of array

int BSearch(int *a,int ITEM)
{
int beg=0,end=n-1;
int mid;

while( beg<=end )
{
mid=(beg+end)/2;
if(a[mid]==ITEM)
return mid;
else if( a[mid] < ITEM )
beg=mid+1;
else
end=mid-1;
}

return -1;
}

void SSort(int *a)
{
int i,j,temp;

for(i=0; i
{
for(j=i+1; j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

}

void DEL(int *a, int ITEM)
{
int i;
int pos=BSearch(a, ITEM);

if(pos!=-1)
{
for(i=pos; i
a[i]=a[i+1];
n–;
printf(“\n Item Deleted. “);
}
else
printf(“\n Item not found. “);
}

void INS(int *a, int ITEM)
{
int pos,i;

//Finding the proper position for Sorted Array
if(ITEM>a[n-1])
pos=n;
else if(ITEM<a[0])
pos=0;
else
{
for(i=0; i
if(a[i]<=ITEM && a[i+1]>=ITEM)
{
pos=i+1;
break;
}
}

//Shifting of elements to Insert the element.
for(i=n; i>pos; i–)
a[i]=a[i-1];

a[pos]=ITEM;
n++;
printf(“\n Item Inserted. “);
}

void Display(int *a)
{
int i;
for(i=0; i<n; i++)
printf(” %d”,a[i]);
}

int main()
{
int a[100],ch,pos,ITEM,i;

printf(“\n Enter the number of Elements: “);
scanf(“%d”,&n);

printf(“\n Enter the Elements: “);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);

do
{
printf(“\n ______________________________________ “);
printf(“\n MENU:\n”);
printf(“\n 1. Insert an element. “);
printf(“\n 2. Delete an element. “);
printf(“\n 3. Search for an element. “);
printf(“\n 4. Display elements. “);
printf(“\n 0. Exit. “);
printf(“\n Your Choice: “);
scanf(“%d”,&ch);

switch(ch)
{

case 1: //INSERT
printf(“\n Enter the Item to Insert: “);
scanf(“%d”,&ITEM);

SSort(a);
INS(a,ITEM);
Display(a);

break;

case 2: //DELETE
printf(“\n Enter the Item to Delete: “);
scanf(“%d”,&ITEM);

SSort(a);
DEL(a,ITEM);
Display(a);

break;

case 3: //SEARCH
printf(“\n Enter the Item to Search: “);
scanf(“%d”,&ITEM);

SSort(a);
pos=BSearch(a,ITEM);

if(pos==-1)
printf(“\n Item not Found. “);
else
printf(“\n Item found at %d position. “,pos+1);

break;

case 4: //DISPLAY
printf(“\n The Elements are: “);

SSort(a);
Display(a);

break;

case 0:
printf(“\n Exiting… “);
break;
}

}while(ch!=0);
getch();
}