Friday 13 December 2013

ADDITION OF TWO SPARSE MATRIX

#include < stdio.h >
#include < conio.h > 

void main()
{
	int sp1[10][3],sp2[10][3],sp3[10][3];
	
	clrscr();
	printf("\nEnter first sparse matrix");
	read_sp_mat(sp1);
	printf("\nEnter second sparse matrix");
	read_sp_mat(sp2);
	add_sp_mat(sp1,sp2,sp3);
	
	printf("\nFirst sparse matrix is");
	print_sp_mat(sp1);
	printf("\nSecond sparse matrix is");
	print_sp_mat(sp2);
	printf("\nThird sparse matrix is");
	print_sp_mat(sp3);

} // main

int read_sp_mat(int sp[10][3])
{
	int r,c,i,j,k,t;	
	
	printf("\nEnter r and c : ");
	scanf("%d %d",&r,&c);
	printf("\nEnter the data \n");
	k=1;
	for(i=0;i < r;i++)
	{
		for(j=0;jc;j++)
		{
			scanf("%d",&t);
			if( t != 0 )
			{
				sp[k][0] = i;
				sp[k][1] = j;
				sp[k][2] = t;
				k++;
			} // if
		} // for
	}
	sp[0][0] = r;
	sp[0][1] = c;
	sp[0][2] = k-1;
	return;
} // read_sp_mat

int print_sp_mat(int sp[10][3])
{
	int r,c,i,j,tot_val,k;
	
	r = sp[0][0];
	c = sp[0][1];
	tot_val = sp[0][2];
	
	for(i=0;ir;i++)
	{
		printf("\n");
		for(j=0;jc;j++)
		{
			for(k=1;k<=tot_val;k++)
			{
				if( sp[k][0] == i && sp[k][1] == j )
				break;
			}
			if( k > tot_val)
				printf("%4d",0);
			else
				printf("%4d",sp[k][2]);
		} // for
	} // for
	return;
} //print_sp_mat

int add_sp_mat(sp1,sp2,sp3)
int sp1[10][3],sp2[10][3],sp3[10][3];
{
	int r,c,i,j,k1,k2,k3,tot1,tot2;
	
	if( sp1[0][0] != sp2[0][0] || sp1[0][1] != sp2[0][1] )
	{
		printf("Invalid matrix size ");
		exit(0);
	}
	tot1 = sp1[0][2];
	tot2 = sp2[0][2];	
	k1 = k2 = k3 = 1;
	while ( k1 <= tot1 && k2 <= tot2)
	{
		if ( sp1[k1][0] < sp2[k2][0] )
		{
			sp3[k3][0] = sp1[k1][0];
			sp3[k3][1] = sp1[k1][1];
			sp3[k3][2] = sp1[k1][2];
			k3++;k1++;
		}
		else
		if ( sp1[k1][0] > sp2[k2][0] )
		{
			sp3[k3][0] = sp2[k2][0];
			sp3[k3][1] = sp2[k2][1];
			sp3[k3][2] = sp2[k2][2];
			k3++;k2++;
		}
		else if ( sp1[k1][0] == sp2[k2][0] )
		{
		if ( sp1[k1][1] < sp2[k2][1] )
		{
			sp3[k3][0] = sp1[k1][0];
			sp3[k3][1] = sp1[k1][1];
			sp3[k3][2] = sp1[k1][2];
			k3++;k1++;
		}
		else
		if ( sp1[k1][1] > sp2[k2][1] )
		{
			sp3[k3][0] = sp2[k2][0];
			sp3[k3][1] = sp2[k2][1];
			sp3[k3][2] = sp2[k2][2];
			k3++;k2++;
		}
		else
		{
			sp3[k3][0] = sp2[k2][0];
			sp3[k3][1] = sp2[k2][1];
			sp3[k3][2] = sp1[k1][2] + sp2[k2][2];
			k3++;k2++;k1++;
		}
		} // else
	} // while
	while ( k1 <=tot1 )
	{
		sp3[k3][0] = sp1[k1][0];
		sp3[k3][1] = sp1[k1][1];
		sp3[k3][2] = sp1[k1][2];
		k3++;k1++;
	} //while
	
	while ( k2 <= tot2 )
	{
		sp3[k3][0] = sp2[k2][0];
		sp3[k3][1] = sp2[k2][1];
		sp3[k3][2] = sp2[k2][2];
		k3++;k2++;
	} // while
	sp3[0][0] = sp1[0][0];
	sp3[0][1] = sp1[0][1];
	sp3[0][2] = k3-1;
	return;
} // add_sp_mat

Thursday 12 December 2013

SORTING TECHNIQUES:- HEAP SORT

#include < stdio.h >
#include < conio.h >

void main()
{
    int a[50];
    int n;
    clrscr();
    printf("\nEnter n: ");
    scanf("%d",&n);
    read_data(a,n);
    printf("\nBefore sort : \n");
    print_data(a,n);
    heap_sort(a,n);
    printf("\nAfter sort : \n");
    print_data(a,n);
}

int read_data(int a[],int max)
{
    int i;
    printf("\nEnter %d values \n",max);
    for(i=1;i<=max;i++)
    {
        scanf("%d",&a[i]);
    }
    return;
}

int print_data(int a[],int max)
{
    int i;
    for(i=1;i<=max;i++)
    {
        printf("%10d",a[i]);
    }
    return;
}

int heap_sort(int a[],int n)
{
    int i,j,t;
    for(i=n/2;i>=1;i--)
    {
        adjust(a,i,n);
    }
    for(i=n-1;i>=1;i--)
    {
        printf("\n");
        print_data(a,n);
        t=a[i+1];
        a[i+1]=a[1];
        a[1]=t;
        adjust(a,1,i);
    }
    return;
}
int adjust(int a[],int cur_pos,int max)
{
    int cur_rec,j;
    cur_rec=a[cur_pos];
    j=cur_pos * 2;
    while(j<=max)
    {
        if(j < max)
        {
        if(a[j] < a[j+1])
        {
            j=j+1;
        }
     }
    if(a[j] > cur_rec)
    {
        a[j/2]=a[j];
        j=j*2;
    }
 else
  break;
 }
 a[j/2]=cur_rec;
 return;
}

SORTING TECHNIQUES:- MERGE SORT

#include < stdio.h >
#include < conio.h > 

void main()
{
        int a1[20],a2[20],a3[40];
        int max1,max2,max3;
        clrscr();
        printf("\nEnter max1: ");
        scanf("%d",&max1);
        read_data(a1,max1);
        printf("\nEnter max2: ");
        scanf("%d",&max2);
        read_data(a2,max2);
        max3=merge_sort(a1,a2,a3,max1,max2);
        printf("\nFirst array is \n");
        print_data(a1,max1);
        printf("\nSecond array is \n");
        print_data(a2,max2);
        printf("\nThird array is \n");
        print_data(a3,max3);
}

int read_data(int a[],int max)
{
        int i;
        printf("\nEnter %d sorted values \n",max);
        for(i=0;i < max;i++)
        {
                scanf("%d",&a[i]);
        }
        return;
}

int print_data(int a[],int max)
{
        int i;
        for(i=0;i < max;i++)
        {
                printf("%4d",a[i]);
        }
        return;
}

int merge_sort(a1,a2,a3,max1,max2)
int a1[],a2[],a3[];
int max1,max2;
{
        int i,j,k;
        i=j=k=0;
        while(i < max1 && j < max2)
        {
                if (a1[i] < a2 [j])
        {
                a3[k++]=a1[i++];
        }
        else
        {
                a3[k++]=a2[j++];
        }
        }
        while (i < max1)
        {
                a3[k++]=a1[i++];
        }
        while(j < max2)
        {
                a3[k++]=a2[j++];
        }
        return(k);
}

SORTING TECHNIQUES:-BUBBLE SORT

//... C Language Program to Sort a Struct type Array by using a Bubble Sort method
#include < stdio.h >
#include < conio.h > 

struct stud
{
 int roll;
 char name[15];
 float per;
};

void main()
{
 struct stud a[50], t;
 int i, j, n;

 clrscr();

 printf("\n C Language Program to Sort Struct type Array by using a Bubble Sort method ");
 printf("\n To sort the Student Records in Dercreasing Order of % (Percentage) \n");
 printf("\n Enter How Many Records [ i.e. Size of Array (n) ] : ");
 scanf("%d", &n);
 read_data(a, n);

 printf("\n %d Records Before Sorting are \n", n);
 print_data(a, n);

 bbl_sort(a, n);

 printf("\n %d Values After Sorting are \n", n);
 print_data(a, n);

} // main

int read_data( struct stud a[], int n )
{
 int i;
 float t;

 printf("\n Enter %d Records \n", n);
 for(i = 0; i < n; i++)
 {
  printf("\n Roll No. : ");
  scanf("%d", &a[i].roll);
  printf("\n Name : ");
  flushall();
  gets(a[i].name);
  printf("\n Percentage (%) : ");
  scanf("%f", &t);
  a[i].per = t;
 } // for
 return;
} // read_data

int print_data( struct stud a[], int n )
{
 int i;
 float t;

 printf("\n Roll No. \t Name \t Percentage (%) \n");
 for(i = 0; i < n; i++)
 {
  printf("\n \t %d \t %s \t %.2f", a[i].roll, a[i].name, a[i].per);
 } // for
 return;
} // print_data

int bbl_sort( struct stud a[], int n )
{
 int i,j, k;
 struct stud t;

 for(k = n - 1; k >= 1; k--)
 {
  for(i = 0,j = 1; j <= k; i++,j++)
  {
   if( a[i].per > a[j].per)
   {
    t = a[i];
    a[i] = a[j];
    a[j] = t;
   } // if
  } // for
 } // for
 return;
} // bbl_sort

SORTING TECHNIQUES:-QUICK SORT

#include < stdio.h >
#include < conio.h > 

struct stack
{
 int low, high;
}; 

void main()
{
 int a[50];
 int n;
 clrscr();
 printf("\nEnter n: ");
 scanf("%d",&n);
 read_data(a,n);
 a[n]=9999;
 printf("\nBefore sort :");
 print_data(a,n);
 qck_srt(a,n);
    printf("\nAfter sort :");
    print_data(a,n);
}

int read_data(int a[],int max)
{
 int i;
 printf("\nEnter %d values \n",max);
 for(i=0; i < max; i++)
 {
  scanf("%d",&a[i]);
 }
 return;
}

int print_data(int a[],int max)
{
 int i;
 for(i=0; i < max; i++)
 {
  printf("%4d",a[i]);
 }
 return;
}

int qck_srt(int a[], int n)
{
 struct stack s[50];
 int top;
 int i, j, k, l, h;
 int t;

 //... Initialize Stack
 top = -1;

 //... Push First Position
 top++;
 s[top].low = 0;
 s[top].high = n - 1;

 //... While Stack is not Empty do the following
 while( top != -1 )
 {
  //... Pop top Partition
  l = s[top].low;
  h = s[top].high;
  top--;
  if(l >= h )
  {
   continue;
        }
  k = a[l];
  i = l;
  j = h + 1;
  while( i < j )
  {
   while( i < h && a[i] <= k )
   {
    i++;
   }
   while( j > l && a[j] >= k )
   {
    j--;
   }
   if( i < j )
   {
    t = a[i];
    a[i] = a[j];
    a[j] = t;
   } // if
  } // while
  if(l != j)
  {
   t = a[l];
   a[l] = a[j];
   a[j] = t;
  } // if

  //... Push Right Partition
  top++;
  s[top].low = j + 1;
  s[top].high = h;

  //... Push Left Partition
  top++;
  s[top].low = l;
  s[top].high = j - 1;
 } // while
 return;
} // qck_srt

SORTING TECHNIQUES:-INSERTION SORTING

#include < stdio.h >
#include < conio.h > 

void main()
{
        int a[50],n;
        clrscr();
        printf("\nEnter n:");
        scanf("%d",&n);
        insert_sort(a,n);
        printf("\n%d values after sorting are ",n);
        print_data(a,n);
}

int insert_sort(int a[],int n)
{
        int bottom,i,j,t;
        bottom=-1;
        for(j=0;j < n;j++)
        {
                printf("\nData :");
                scanf("%d",&t);
                i=bottom;
                while(a[i]>t && i!=-1)
                {
                        a[i+1]=a[i];
                        i--;
                }
                a[i+1]=t;
                bottom++;
        }
        return;
}

int print_data(int a[],int n)
{
        int i;
        for(i=0;i  < n;i++)
        {
                printf("%4d",a[i]);
        }
        return;
}

SEARCH AN ELEMENT IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],x,n,i;
/*
 a - for storing of data
 x - element to be searched
 n - no of elements in the array
 i - scanning of the array
*/
 printf("nEnter no of elements :");
scanf("%d",&n);
/* Reading values into Array */

printf("nEnter the values :");
for(i=0;i < n;i++)
scanf("%d",&a[i]);

/* read the element to be searched */

printf("nEnter the elements to be searched");
scanf("%d",&x);

/* search the element */

i=0; /* search starts from the zeroth location */
while(i < n && x!=a[i])
i++;

/* search until the element is not found i.e. x!=a[i]
search until the element could still be found i.e. i 〈 n */

if(i < n) /* Element is found */
printf("found at the location =%d",i+1);
else
printf("n not found");

getch();
}

IMPLEMENT STACK OPERATIONS USING STACK

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack  {
          int s[size];
          int top;
           }st;
//-------------------------------------
int stfull()
{
 if(st.top>=size-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
void push(int item)
{
 st.top++;
 st.s[st.top] =item;
}
//-------------------------------------
int stempty()
{
 if(st.top==-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
int pop()
 {
 int item;
 item=st.s[st.top];
 st.top--;
 return(item);
 }
//-------------------------------------
void display()
{
 int i;
 if(stempty())
    printf("n Stack Is Empty!");
 else
  {
  for(i=st.top;i>=0;i--)
     printf("n%d",st.s[i]);
  }
}
//-------------------------------------
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("ntt Implementation Of Stack");
do
{
 printf("n Main Menu");
 printf("n1.Pushn2.Popn3.Displayn4.exit");
 printf("n Enter Your Choice");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("n Enter The item to be pushed");
        scanf("%d",&item);
        if(stfull())
            printf("n Stack is Full!");
        else
            push(item);
        break;
  case 2:
        if(stempty())
            printf("n Empty stack!Underflow !!");
        else
          {
          item=pop();
          printf("n The popped element is %d",item);
          }
        break;
  case 3:
        display();
        break;
  case 4:
        exit(0);
 }
 printf("n Do You want To Continue?");
 ans=getche();
}while(ans =='Y'||ans =='y');
getch();
}