Wednesday, 19 October 2011

"C program for Sparse matrix"

/*
  Represent sparse matrix using array and perform matrix addition,simple
  transpose and fast transpose */
#include<stdio.h>
#include<conio.h>
#define MAX 20
void printsparse(int[][3]);
void readsparse(int[][3]);
void transpose(int[][3],int[][3]);
void Fast_transpose(int B1[MAX][3],int B2[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
void main()
{
    int b1[MAX][3],b2[MAX][3],m,n,b3[MAX][3],op;
    clrscr();
    do
      {
        printf("\n1)Read the First Sparse Matrix");
        printf("\n2)Read the second sparse matrix");
        printf("\n3)Display the first matrix");
        printf("\n4)Display the second matrix");
        printf("\n5)Addition of two matrices");
        printf("\n6)Simple transpose of the first matrix");
        printf("\n7)Fast transpose of the first matrix");
        printf("\n8)Quit");
        printf("\nEnter your choice : ");
        scanf("%d",&op);
        switch(op)
         {
        case 1: readsparse(b1);break;
        case 2: readsparse(b2);break;
        case 3: printsparse(b1);break;
        case 4: printsparse(b2);break;
        case 5: addsparse(b1,b2,b3);printsparse(b3);break;
        case 6: transpose(b1,b3);printsparse(b3);break;
        case 7: Fast_transpose(b1,b3);printsparse(b3);break;
         }
     }while(op!=8);
}
void readsparse(int b[MAX][3])
{
    int i,t,m,n;

    printf("\n Enter the size of matrix (rows,columns)");
    scanf("%d%d",&m,&n);
    b[0][0]=m;
    b[0][1]=n;

    printf("\nEnter no. of non-zero elements:");
    scanf("%d",&t);
    b[0][2]=t;
    for(i=1;i<=t;i++)
    {
        printf("\n Enter the next triple(row,column,value) :");
        scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
    }
}
void printsparse(int b[MAX][3])
{
    int i,n;
    n=b[0][2];   //no of 3-triples
    printf("\nrows = %d\tcolumns = %d",b[0][0],b[0][1]);
    printf("\n");
    for(i=1;i<=n;i++)
        printf("%d\t%d\t%d\n",b[i][0],b[i][1],b[i][2]);
}
void transpose(int b1[][3],int b2[][3])
{
    int i,j,k,n;
    b2[0][0]=b1[0][1];
    b2[0][1]=b1[0][0];
    b2[0][2]=b1[0][2];
    k=1;
    n=b1[0][2];
    for(i=0;i<b1[0][1];i++)
        for(j=1;j<=n;j++)
    /* if a column number of current triple == i
       then insert the current triple in b2 */
        if(i== b1[j][1])
        {
            b2[k][0]=i;
            b2[k][1]=b1[j][0];
            b2[k][2]=b1[j][2];
            k++;
        }
}

void Fast_transpose(int B1[MAX][3],int B2[MAX][3])
{
    int m,n,t,i,col_num,location;
    int total[MAX],index[MAX];
    m=B1[0][0];n=B1[0][1];t=B1[0][2];
    B2[0][0]=n;B2[0][1]=m;B2[0][2]=t;
    for(i=0;i<n;i++)
        total[i]=0;
    for(i=1;i<=t;i++)
    {
        col_num=B1[i][1];
        total[col_num]++;
    }
    index[0]=1;
    for(i=1;i<n;i++)
        index[i]=index[i-1]+total[i-1];

    for(i=1;i<=t;i++)
    {
        col_num=B1[i][1];
        location=index[col_num];
        index[col_num]++;
        B2[location][0]=B1[i][1];
        B2[location][1]=B1[i][0];
        B2[location][2]=B1[i][2];
    }
}

void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
    int t1,t2,i,j,k;
    t1=b1[0][2];
    t2=b2[0][2];
    i=j=k=0;
    b3[0][0]=b1[0][0];
    b3[0][1]=b1[0][1];
    while(i<=t1 && j<=t2)
    {
        if(b1[i][0] < b2[j][0])
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
            continue;//go to  end of the loop
        }
        if(b2[j][0] < b1[i][0])
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
            continue;//go to end of loop
        }
        if(b1[i][1] < b2[j][1])
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
            continue;//go to end of loop
        }
        if(b2[j][1] < b1[i][1])
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
            continue; //go to end of loop
        }
//else add the two tuples
        b3[k][0]=b1[i][0];
        b3[k][1]=b1[i][1];
        b3[k][2]=b1[i][2]+b2[j][2];
        k++;
        i++;
        j++;

     }
        while(i<=t1)
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            i++;
            k++;
        }
        while(j<=t2)
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b1[j][1];
            b3[k][2]=b1[j][2];
            j++;
            k++;
        }
        b3[0][2]=k-1;
  }
  /*
  OUTPUT:-

1)Read the First Sparse Matrix
2)Read the second sparse matrix
3)Display the first matrix
4)Display the second matrix
5)Addition of two matrices
6)Simple transpose of the first matrix
7)Fast transpose of the first matrix
8)Quit
Enter your choice : 1

Enter the size of matrix (rows,columns)2 2

Enter no. of non-zero elements:3

Enter the next triple(row,column,value) :2 3 5

Enter the next triple(row,column,value) :0 2 1

Enter the next triple(row,column,value) :2 5 8

Enter your choice : 2

Enter the size of matrix (rows,columns)2 2

Enter no. of non-zero elements:3

Enter the next triple(row,column,value) :4 5 7

Enter the next triple(row,column,value) :3 5 9

Enter the next triple(row,column,value) :3 0 3

Enter your choice : 3

rows=2    columns=2

2      3       5
0      2       1
2      5       8

Enter your choice : 4

rows=2          columns=2
4      5       7
3      5       9
3      0       3

Enter your choice : 5

rows = 2        columns = 2
2       3       5
0       2       1
2       5       8
4       3       5
3       2       1
3       5       8

Enter your choice : 6

rows = 2        columns = 2
2       3       5
0       2       1
2       5       8


Enter your choice : 7

rows = 2        columns = 2
2       0       1
3       2       5
2       5       8
*/


"C program for polynomial usng structure"

/*Various operations on polynomials using structures.
 */

#include<math.h>
#include<stdio.h>
//#include<conio.h>
typedef struct term
{
    int power;
    float coeff;
}term;
typedef struct polynomial
{
    term a[20];
    int n;
}polynomial;
void init(polynomial *ptr);
void read(polynomial *ptr);
void print(polynomial *ptr);
polynomial add(polynomial *p1,polynomial *p2);
polynomial multiply(polynomial *p1,polynomial *p2);
float evaluate(polynomial *p1,float x);
void insert(polynomial *,term);
int main()
{
    polynomial P1,P2,P3;
    int option;
    float x,value;
    clrscr();
    do
    {
        printf("\n1 : create 1'st polynomial");
        printf("\n2 : create 2'nd polynomial");
        printf("\n3 : Add polynomials");
        printf("\n4 : Multiply polynomials");
        printf("\n5 : Evaluate 1'st polynomial");
        printf("\n6 : Quit");
        printf("\nEnter your choice :");
        scanf("%d",&option);
        switch(option)
        {
            case 1:read(&P1);break;
            case 2:read(&P2);break;
            case 3:P3=add(&P1,&P2);
                   printf("\n1'st polynomial -> ");
                   print(&P1);
                   printf("\n2'nd polynomial -> ");
                   print(&P2);
                   printf("\n Sum = ");
                   print(&P3);
                   break;
            case 4:P3=multiply(&P1,&P2);
                   printf("\n1'st polynomial -> ");
                   print(&P1);
                   printf("\n2'nd polynomial -> ");
                   print(&P2);
                   printf("\n Product = ");
                   print(&P3);
                   break;
            case 5:printf("\nEnter the value of x:");
                   scanf("%f",&x);
                   value=evaluate(&P1,x);
                   printf("\nValue of Polynomial =%6.2f",value);
                   break;
        }
    }while(option!=6);
return 0;
}
void read(polynomial *Ptr)
{
    int n, i, power;
    float coeff;
    term t;
    init(Ptr);
    printf("\n Enter number of terms :");
    scanf("%d",&n);
    /* read n terms */
    for (i=0;i<n;i++)
    {       printf("\nenter a term(power  coeff.)");
        scanf("%d%f",&power,&coeff);
        t.power=power;
        t.coeff=coeff;
        insert(Ptr,t);
    }
}
void print(polynomial *Ptr)
{
    int i;
    printf("%5.2fX^%d ",(Ptr->a[0]).coeff,(Ptr->a[0]).power);
    for(i=1;i<Ptr->n;i++)
      printf(" + %5.2fX^%d ",(Ptr->a[i]).coeff,(Ptr->a[i]).power);
}
polynomial add(polynomial *P1, polynomial *P2)
{
    polynomial P3;
    term t;
    int i,j;
    i=j=0;
    init(&P3);
    while(i<P1->n && j<P2->n)
    {
        if(P1->a[i].power==P2->a[j].power)
        {
            t.power=P1->a[i].power;
            t.coeff=P1->a[i].coeff+P2->a[j].coeff;
            insert(&P3,t);
            i++;j++;
        }
        else
            if(P1->a[i].power < P2->a[j].power)
            {
                insert(&P3,P1->a[i]);
                i++;
            }
            else
            {
                insert(&P3,P2->a[j]);
                j++;
            }
    }
    while(i<P1->n)
    {
         insert(&P3,P1->a[i]);
         i++;
    }
    while(j<P2->n)
    {
         insert(&P3,P2->a[j]);
         j++;
    }
    return(P3);
}
polynomial multiply(polynomial *P1,polynomial *P2)
{
    polynomial P3, Ptemp;
    term t;
    int i,j;
    init(&P3);
    for(i=0;i<P2->n;i++)
    {
        init(&Ptemp);
        for(j=0;j<P1->n;j++)
        {
            t.power=P1->a[j].power+P2->a[i].power;
            t.coeff=P1->a[j].coeff*P2->a[i].coeff;                                        insert (&Ptemp, t);
        }
    P3=add(&P3,&Ptemp);
    }
    return(P3);
}
float evaluate(polynomial *P, float x)
{
    float value;
    int i,power;
    value=0.00;
    for(i=0;i<P->n;i++)
     { power=P->a[i].power;
    value=value+(P->a[i].coeff)*pow(x,power);
      }
    return(value);
}
void init(polynomial *Ptr)
{
    Ptr->n=0;
}
void insert(polynomial *Ptr,term t)
{
    int i;
    /* move all higher power term by 1 place.movement
       should start with the last term */
    for(i=Ptr->n-1; (Ptr->a[i]).power > t.power && i>=0;i--)
        Ptr->a[i+1]=Ptr->a[i];
    /* insert the term t */
    Ptr->a[i+1]=t;
    (Ptr->n)++;
}
/*
OUTPUT:-

1 : create 1'st polynomial
2 : create 2'nd polynomial
3 : Add polynomials
4 : Multiply polynomials
5 : Evaluate 1'st polynomial
6 : Quit
Enter your choice :1

Enter number of terms :2

enter a term(power  coeff.)2 3

enter a term(power  coeff.)3 4


Enter your choice :2

Enter number of terms :2

enter a term(power  coeff.)2 4

enter a term(power  coeff.)3 3

Enter your choice :3

1'st polynomial ->  3.00X^2  +  2.00X^3

2'nd polynomial ->  4.00X^2  +  3.00X^3
Sum =  7.00X^2  +  5.00X^3

Enter your choice :4
1'st polynomial ->  3.00X^2  +  2.00X^3
2'nd polynomial ->  4.00X^2  +  3.00X^3
Product = 12.00X^4  + 17.00X^5  +  6.00X^6

Enter your choice :5
Enter the value of x:12
Value of Polynomial =3888.00
*/
/* DIFFERENT SEARCHING TECHNIQUES
1)FIBONACCI SEARCH 2)BINARY SEARCH 3)LINEAR SEARCH 4)INDEX SEQUENTIAL
SEARCH
NAME:-SKAND KUMAR(S.E comp-46 ) */
#include<stdio.h>
#include<conio.h>
int fib(int n);
int fibsearch(int a[],int key,int mid,int p,int q);
int binsearch(int a[],int i,int j,int key);
int seqsearch(int a[],int n,int key);
int index_sequential(int a[],int n,int key);
void main()
{
int a[30],key,mid,p,q,k,n,i,result,op;
clrscr();
do
{
printf("\n1]sequential search 2]binary search 3]fibonaci search");
printf("\n4]index sequential search 5]quit");
printf("\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter a list of %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);
result=seqsearch(a,n,key);
if(result==-1)
printf("\n not found:");
else
printf("\n found at location=%d",result);
break;

case 2:printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter a sorted list of %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);
result=binsearch(a,0,n-1,key);
if(result==-1)
printf("\n not found:");
else
printf("\n found at location=%d",result);
break;

case 3:printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter a sorted list of %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);
for(k=1;fib(k)<=n;k++);
p=fib(k-2);
q=fib(k-3);
mid=n-p+1;
result=fibsearch(a,key,mid,p,q);
if(result==-1)
printf("\n not found:");
else
printf("\n found at location=%d",result);
break;

case 4:printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter a list of %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&key);
result=index_sequential(a,n,key);
if(result==-1)
printf("\n not found:");
else
printf("\n found at location=%d",result);
break;
}
}
while(op!=5);
}
int fib(int n)
{
if(n==0) return(0);
if(n==1) return(1);
return(fib(n-1)+fib(n-2));
}
int fibsearch(int a[],int key,int mid,int p,int q)
{
int temp;
if(key==a[mid])
return(mid);
if(key>a[mid])
{
if(p==1)
return(-1);
mid=mid+q;
p=p-q;
q=q-p;
return(fibsearch(a,key,mid,p,q));
}
else
{
if(q==0)
return(-1);
mid=mid-q;
temp=p-q;
p=q;
q=temp;
return(fibsearch(a,key,mid,p,q));
}
}
int binsearch(int a[],int i,int j,int key)
{
int c;
if(i>j)
return(-1);
c=(i+j)/2;
if(key==a[c])
return(c);
if(key>a[c])
return(binsearch(a,c+1,j,key));
return(binsearch(a,i,c-1,key));
}
int seqsearch(int a[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
return(i);
}
return(-1);
}
int index_sequential(int a[],int n,int key)
{
 int keys[6],index[6],i,j,temp,n1,start;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
n1=0;
for(i=0;i<n;i=i+4)
{
keys[n1]=a[i];
index[n1]=i;
n1++;
}
if(key<keys[0])
return(-1);
for(i=0;i<n1;i++)
if(key<keys[i])
break;
start=index[i-1];
for(i=start;i<start+4 && i<n;i++)
if(a[i]==key)
return(i);
return(-1);
}

/*
1]sequential search 2]binary search 3]fibonaci search
4]index sequential search 5]quit
Enter your choice:1
 Enter no of elements:5
 Enter a list of 5 elements:90 2 60 35 40
 Enter the element to be searched:40
 found at location=4

1]sequential search 2]binary search 3]fibonaci search
4]index sequential search 5]quit
Enter your choice:2
 Enter no of elements:5
 Enter a sorted list of 5 elements:1 2 3 4 5
 Enter the element to be searched:3
 found at location=2

1]sequential search 2]binary search 3]fibonaci search
4]index sequential search 5]quit
Enter your choice:3
 Enter no of elements:5
 Enter a sorted list of 5 elements:1
2
3
4
5
 Enter the element to be searched:0
 not found:

1]sequential search 2]binary search 3]fibonaci search
4]index sequential search 5]quit
Enter your choice:4
 Enter no of elements:5
 Enter a list of 5 elements:1 2 3 4 5
 Enter the element to be searched:2
 found at location=1

1]sequential search 2]binary search 3]fibonaci search
4]index sequential search 5]quit
Enter your choice:5
*/






"C program for sorting operation usig recursion"

/*Implement Quick sort and Mege sort using recursion*/

#include<conio.h>
#include<stdio.h>
void quick_sort(int [],int,int);
int partition(int [],int,int);
void mergesort(int a[] ,int i , int j);
void merge(int a[],int i1, int j1, int i2, int j2);
void main()
{
    int a[30],n,i,op;
    clrscr();
    do
       {
         printf("\n1)Quick Sort\n2)Merge Sort \n3)Quit");
         printf("\nEnter your choice : ");
         scanf("%d",&op);
         switch(op)
          {
        case 1: printf("\nEnter no of elements :");
            scanf("%d",&n);
            printf("\nEnter array elements :");
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            quick_sort(a,0,n-1);
            printf("\nSorted array is :");
            for(i=0;i<n;i++)
                printf("%d  ",a[i]);
            break;

        case 2: printf("\nEnter no of elements :");
            scanf("%d",&n);
            printf("\nEnter array elements :");
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
            mergesort(a,0,n-1);
            printf("\nSorted array is :");
            for(i=0;i<n;i++)
                printf("%d   ",a[i]);
            break;

       }
     }while(op!=3);
}
void quick_sort(int a[],int l,int u){
    int j;
    if(l<u)
    {
        j=partition(a,l,u);
        quick_sort(a,l,j-1);
        quick_sort(a,j+1,u);
    }
}
int partition(int a[],int l,int u)
{
    int v,i,j,temp;
    v=a[l];
    i=l;
    j=u+1;
    do
    {
    do
    {    i++;
    }while(a[i]<v && i<=u);

    do
    {    j--;
    }while(a[j]>v);

    if(i<j)
    {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    }while(i<j);
    a[l]=a[j];
    a[j]=v;
    return(j);
}

 void mergesort(int a[] ,int i , int j)
   {
    int mid;
    if(i<j)
     { mid=(i+j)/2;
       mergesort(a,i,mid);    //left recursion
       mergesort(a,mid+1,j);  //right recursion
       merge(a,i,mid,mid+1,j); //meging of two sorted sub-arrays
     }
    }


void merge(int a[],int i1, int j1, int i2, int j2)
  {
     int temp[50];//array used for merging
     int i,j,k;
     i=i1;//beginning of the first list
     j=i2;//beginning of the second list
     k=0;
     while(i<=j1 && j <=j2) //while elemnts in both lists
      {
    if(a[i]<a[j])
       temp[k++]=a[i++];
    else
       temp[k++]=a[j++];
      }
    while(i<=j1)//copy remaining elements of the first list
       temp[k++]=a[i++];
    while(j<=j2)//copy remaining elements of the second list
       temp[k++]=a[j++];
  //Transfer elemnts from temp[] back to a[]
    for(i=i1,j=0;i<=j2;i++,j++)
       a[i]=temp[j];
  }

/*
OUTPUT:-
1)Quick Sort
2)Merge Sort
3)Quit
Enter your choice : 1

Enter no of elements :5

Enter array elements :6 5 4 3 89

Sorted array is :3  4  5  6  89



Enter your choice : 2

Enter no of elements :4

Enter array elements :89 65 23 48

Sorted array is :23   48   65   89
*/














                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

"C program for Sorting operation using function"

/* DIFFERENT SORTING TECHNIQUES
  1)BUBBLE SORT 2)SELECTION SORT 3)INSERTION SORT 5)SHELL SORT */
#include<stdio.h>
#include<conio.h>
int a[60],n,i,j,k,temp,choice;
void bubble_sort();
void selection_sort();
void insertion_sort();
void shell_sort();
void main()
{
    clrscr();
    do
    {
        printf("\n 1]BUBBLE. \n2]SELECTION..\n3]INSERTION. \n4]SHELL. 5]QUIT.");
        printf("\n ENTER YOUR CHOICE:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: printf("\n ENTER NO OF ELEMENTS:");
            scanf("%d",&n);
            printf("\n ENTER ARRAY ELEMENTS:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            bubble_sort();
            break;

            case 2: printf("\n ENTER NO OF ELEMENTS:");
                  scanf("%d",&n);
                   printf("\n ENTER ARRAY ELEMENTS:");
                  for(i=0;i<n;i++)
                  scanf("%d",&a[i]);
                  selection_sort();
                  break;

            case 3: printf("\n ENTER NO OF ELEMENTS:");
                  scanf("%d",&n);
                   printf("\n ENTER ARRAY ELEMENTS:");
                  for(i=0;i<n;i++)
                  scanf("%d",&a[i]);
                  insertion_sort();
                  break;

            case 4: printf("\n ENTER NO OF ELEMENTS:");
                  scanf("%d",&n);
                   printf("\n ENTER ARRAY ELEMENTS:");
                  for(i=0;i<n;i++)
                  scanf("%d",&a[i]);
                  shell_sort();
                  break;
            }
}
while (choice!=5);
getch();
}

void bubble_sort()
{
for(i=0;i<n;i++)
printf("\n UNSORTED DATA:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n AFTER PASS   %d",i);
for(k=0;k<n;k++)
printf("%5d",a[k]);
}
}
void selection_sort()
{
printf("\n UNSORTED DATA:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[j]<a[k])
k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
printf("\n AFTER PASS   %d",i+1);
for(k=0;k<n;k++)
printf("%5d",a[k]);
}
}
void insertion_sort()
{
printf("\n UNSORTED DATA:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
printf("\n AFTER PASS   %d",i);
for(k=0;k<n;k++)
printf("%5d",a[k]);
}
}

void shell_sort()
{
int step,pass=1;
printf("\n UNSORTED DATA:");
for(i=0;i<n;i++)
printf("%5d",a[i]);
for(step=step/2;step>0;step=step/2)
for(i=step;i<n;i++)
{
temp=a[i];
for(j=i;j>=step;j=j-step)
{
if(temp<a[j-step])
a[j]=a[j-step];
else
break;
}
a[j]=temp;
}
printf("\n AFTER PASS   %d",pass);
for(i=0;i<n;i++)
printf("%5d",a[i]);
pass++;
}

/*
OUTPUT:-
 1]BUBBLE.
2]SELECTION..
3]INSERTION.
4]SHELL. 5]QUIT.
 ENTER YOUR CHOICE:1
 ENTER NO OF ELEMENTS:4

 ENTER ARRAY ELEMENTS:89 56 74 21
 UNSORTED DATA:
 UNSORTED DATA:   89   56   74   21
 AFTER PASS   1   56   74   21   89
 AFTER PASS   2   56   21   74   89
 AFTER PASS   3   21   56   74   89

 ENTER YOUR CHOICE:2

 ENTER NO OF ELEMENTS:3

 ENTER ARRAY ELEMENTS:78 569 84

 UNSORTED DATA:   78  569   84
 AFTER PASS   1   78  569   84
 AFTER PASS   2   78   84  569

 ENTER YOUR CHOICE:3

 ENTER NO OF ELEMENTS:4

 ENTER ARRAY ELEMENTS:41 20 3 64

 UNSORTED DATA:   41   20    3   64
 AFTER PASS   1   20   41    3   64
 AFTER PASS   2    3   20   41   64
 AFTER PASS   3    3   20   41   64

 ENTER YOUR CHOICE:4

 ENTER NO OF ELEMENTS:8

 ENTER ARRAY ELEMENTS:45 87 25 69 74 12 54 98

 UNSORTED DATA:   45   87   25   69   74   12   54   98
 AFTER PASS   1   12   25   45   54   69   74   87   98
*/

"C program for Database operation"

#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct student
{
int rollno;
 char name[20];
 int marks;
}student;
void insert(student st[],int position,int n);
void delete(student st[],int position,int n);
int search(student st[],int rollno,int n);
void print(student st[],int n);
void read(student st[],int n);
void sort(student st[],int n);
void modify(student st[],int n);
void main()
{
student st[30];
 int n,i,op,position,rollno;
 clrscr();
 do{
    flushall();
    printf("\n 1]create 2]insert 3]delete 4] search 5]print 6]sort 7]modify\n");
    printf("\n8]quit");
    printf("\nEnter your choice:");
    scanf("%d",&op);
    switch(op)
    {
     case 1:printf("\nEnter no. of student:");
     scanf("%d",&n);
     read(st,n);
     break;
     case 2:printf("\n enter the position(no. of records=%d):",n);
     scanf("%d",&position);
     if(position<=n+1)
     {
      insert(st,position,n);
      n++;
      print(st,n);
     }
     else
     printf("\n can not insert");
     break;
     case 3:printf("\nenter the roll no:");
     scanf("%d",&rollno);
     position=search(st,rollno,n);
     if(position!=-1)
     {
      delete(st,position,n);
      n--;
      print(st,n);
      }
      else
       {
         printf("\n can not delete");
         }
        break;
     case 4:printf("\nenter roll no:");
     scanf("%d",&rollno);
     position=search(st,rollno,n);
     if(position==-1)
{

     printf("\nnot found");
}
     else
     {
      printf("\n found at location=%d",position+1);
      printf("\n%s\t%d\t%d",st[position].name,st[position].rollno,st[position].marks);
     }

     break;
     case 5:print(st,n);
     break;
     case 6:sort(st,n);print(st,n);break;
     case 7:modify(st,n);break;
 }
 }
  while(op!=8);
 }
  void insert(student st[],int position,int n)
  {
   int i;
   printf("\nenter data(name rollno marks):");
   for(i=n-1;i>=position-1;i--)
   st[i+1]=st[i];
   scanf("%s%d%d",st[position-1].name,&st[position-1].rollno,&st[position-1].marks);
  }
  void delete(student st[],int position,int n)
  {
   int i;
   for(i=position+1;i<n;i++)
   st[i-1]=st[i];
  }
   int search(student st[],int rollno,int n)
    {
      int i;
for(i=0;i<n;i++)
if(rollno==st[i].rollno)
return(i);
return(-1);
}
void print(student st[],int n)
{ int i;
for(i=0;i<n;i++)
printf("\n%20s%5d%5d",st[i].name,st[i].rollno,st[i].marks);
}
void read(student st[],int n)
{
int i;
printf("\nEnter data(name rollno marks):");
for(i=0;i<n;i++)
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}
void modify(student st[],int n)
{
 int rollno,i;
printf("\nenter the rollno:");
scanf("%d",&rollno);
i=search(st,rollno,n);
if(i==-1)
printf("\n No such rollno");
else
{
printf("\nEnter data(name rollno marks):");
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}
}
void sort(student st[],int n)
{
int i,j;
student temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(st[j].rollno>st[j+1].rollno)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}

/*
OUTPUT:-

 1]create 2]insert 3]delete 4] search 5]print 6]sort 7]modify                  
                                                                               
8]quit
Enter your choice:1                                                            
                                                                               
Enter no. of student:2                                                         
                                                                               
Enter data(name rollno marks):jang 34 50                                       
akshay 44 55                                                                   


Enter your choice:2

 enter the position(no. of records=2):2

enter data(name rollno marks):skand 46 60

        jang   34   50
           skand   46   60
          akshay   44   55

Enter your choice:3

enter the roll no:44

        jang   34   50
           skand   46   60

Enter your choice:4

enter roll no:34

 found at location=1
jang    34      50

Enter your choice:6

        jang   34   50
          akshay   44   55

Enter your choice:7

enter the rollno:34

Enter data(name rollno marks):jang 34 53






                 */


                                                                               
                                                                               
                                                                               



"C program for Matrix operation"

#include<stdio.h>
#include<conio.h>
void addition();
void substraction();
void multiplication();
void magic_sqr();
void transpose();
void saddle_pt();
int a[10][10],b[10][10],d[10][10],rsum[20],csum[20];
int r1,r2,c1,c2,m,n,i,j,d1,d2,ar,ac,val,flag=0,rowvalue,colvalue;
void main()
{
  int c;
  char ch;
  do
  {
    clrscr();
    printf("\n\n\t MENU");
    printf("\n\n\t 1.Addition");
    printf("\n\n\t 2.Subtraction");
    printf("\n\n\t 3.Multiplication");
    printf("\n\n\t 4.Transpose");
    printf("\n\n\t 5.Saddle Point");
    printf("\n\n\t 6.Magic Square");
    printf("\n\n\t 7.Exit");
    printf("\n\n\t Enter your choice(1,2,3,4,5,6,7):");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
            addition();
        break;
        case 2:
            substraction();
        break;
        case 3:
            multiplication();
        break;
        case 4:
            transpose();
        break;
        case 5:
            saddle_pt();
        break;
        case 6:
            magic_sqr();
        break;
        case 7:
            exit(0);
        break;
        default:
        printf("\n invalid choice:");
    }
    printf("\n\n\t Do you want to continue in same menu(y/n):");
    c=getch();
  }
  while(c=='y'||c=='Y');
  getch();
}

void addition()
{
   int i,j;
   int d[10][10];
   printf("\n\n\t Enter the size of matrix:");
   scanf("%d%d",&m,&n);
   printf("\n\n\t Enter the elements of first matrix:");
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
       scanf("%d",&a[i][j]);
    }
   }
   printf("\n\n\t Enter the elements of second matrix:");
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
        scanf("%d",&b[i][j]);
    }
   }
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
        d[i][j]=a[i][j]+b[i][j];
    }
   }
   printf("\n\n\t The addition of matrix is:");
   for(i=0;i<m;i++)
   {
    printf("\n\n\t");
    for(j=0;j<n;j++)
    {
        printf("%d\t",d[i][j]);
    }
   }
}

void substraction()
{
  int i,j;
  int d[10][10];
  printf("\n\n\t Enter the size of matrix:");
  scanf("%d%d",&m,&n);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        scanf("%d",&a[i][j]);
    }
  }
  printf("\n\n\t Enter the elements of second matrix:");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        scanf("%d",&b[i][j]);
    }
  }
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
        d[i][j]=a[i][j]-b[i][j];
    }
  }
  printf("\n\n\t The subtraction of matrix is:");
  for(i=0;i<m;i++)
  {
     printf("\n\n\t");
    for(j=0;j<n;j++)
    {
    printf("%d\t",d[i][j]);
    }
  }
}

void multiplication()
{
  int i,j,k,r1,r2,c1,c2;
  int d[10][10];
  printf("\n\n\t Enter the size of first matrix:");
  scanf("%d%d",&r1,&c1);
  printf("\n\n\t Enter the size of second matrix:");
  scanf("%d%d",&r2,&c2);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c1;j++)
    {
        scanf("%d",&a[i][j]);
    }
  }
  printf("\n\n\t Enter the elements of second matrix:");
  for(i=0;i<r2;i++)
  {
    for(j=0;j<c2;j++)
    {
        scanf("%d",&b[i][j]);
    }
  }
  if(c1==r2)
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c2;j++)
    {
        d[i][j]=0;
        for(k=0;k<c2;k++)
        {
            d[i][j]=d[i][j]+(a[i][k]*b[k][j]);
        }
    }
  }
  printf("\n\n\t The multiplication is:");
  for(i=0;i<r1;i++)
  {
    printf("\n\n\t");
    for(j=0;j<c2;j++)
    {
        printf("%d\t",d[i][j]);
    }
  }
}

void transpose()
{
  int i,j,m,n;
  int d[10][10];
  printf("\n\n\t Enter the size of matrix:");
  scanf("%d%d",&m,&n);
  printf("\n\n\t Enter the elements of first matrix:");
  for(i=0;i<m;i++)
 {
    for(j=0;j<n;j++)
    {
        scanf("\t%d",&a[i][j]);
    }
 }
 for(i=1;i<m;i++)
 {
    for(j=0;j<i;j++)
    {
        d[i][j]=a[i][j];
        a[i][j]=a[j][i];
        a[j][i]=d[i][j];
    }
 }
 printf("\n\n\t The transepose is:");
 for(i=0;i<m;i++)
 {
    printf("\n\n\t");
    for(j=0;j<m;j++)
    {
        printf("%d\t",a[i][j]);
    }
 }
}

void saddle_pt()
{
  int i,j,small,large,rsmall,clarge,r1,c1,flag=0;
  printf("\n\n\t Enter size of matrix:");
  scanf("%d%d",&r1,&c1);
  printf("\n\n\t Enter the elements of matrix:");
  for(i=0;i<r1;i++)
  {
    for(j=0;j<c1;j++)
    {
        scanf("\t%d",&a[i][j]);
    }
  }
  for(i=0;i<r1;i++)
  {
    rowvalue=a[i][0];
    rsmall=0;
    for(j=1;j<c1;j++)
    {
        if(rowvalue>a[i][j])
        {
            rowvalue=a[i][j];
            rsmall=j;
        }
    }
    colvalue=a[0][rsmall];
    clarge=0;
    for(j=1;j<r1;j++)
    {
        if(colvalue<a[j][clarge])
        {
            colvalue=a[j][rsmall];
            clarge=j;
        }
    }
    if(i==clarge)
    {
        flag=1;
        printf("\n Saddle point is present at(%d ,%d):",i,rsmall);
        printf("\n The element is=%d",a[i][rsmall]);
    }
  }
  if(flag==0)
  {
    printf("\n Saddle point is not present:");
  }
}

void magic_sqr()
{
   printf("\n\n\t Enter size of matrix:");
   scanf("%d%d",&r1,&c1);
   printf("\n\n\t Enter the elements of matrix:");
   for(i=0;i<r1;i++)
   {
    for(j=0;j<c1;j++)
    {
        scanf("%d",&a[i][j]);
    }
    }
   if(r1==c1)
   {
    for(i=0;i<c1;i++)
    {
        for(j=0;j<c1;j++)
        {
            rsum[i]=rsum[i]+a[i][j];
        }
    }
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            csum[i]=csum[i]+a[i][j] ;
        }
    }
   }
   for(i=0;i<r1;i++)
   {
    for(j=0;j<r1;j++)
    {
        if(i==j)
        d1=d1+a[i][j];
    }
   }
   for(i=0;i<r1;i++)
   {
    for(j=r1-1;j>=0;j--)
    {
        if(i==j)
        {
            d2=d2+a[i][j];
        }
    }
   }
   ar=rsum[0];              //
   for(i=0;i<r1;i++)
   {
    if(ar!=rsum[i])
    flag=1;
   }
   ac=csum[0];
   for(i=0;i<r1;i++)
   {
    if(ac!=csum[i])
    flag=1;
   }
   if(d1==d2&& ar==ac &&flag==0&&d1==ac)
   {
    printf("\n\n\t Matrix is magic square:");
   }
   else
   {
    printf("\n\t Not Magic square:");
   }
}

/*
OUTPUT:-

     MENU

     1.Addition

     2.Subtraction

     3.Multiplication

     4.Transpose

     5.Saddle Point

     6.Magic Square

     7.Exit

     Enter your choice(1,2,3,4,5,6,7):1


     Enter the size of matrix:2 3


     Enter the elements of first matrix:1
2 3 4 5 6


     Enter the elements of second matrix:1 2 3 4 5 6


     The addition of matrix is:

    2       4       6

    8       10      12



    Enter your choice(1,2,3,4,5,6,7):2


     Enter the size of matrix:2 2


     Enter the elements of first matrix:1 2 3 4


     Enter the elements of second matrix:1 2 3 4


     The subtraction of matrix is:

    0       0

    0       0






     Enter your choice(1,2,3,4,5,6,7):3


     Enter the size of first matrix:2 2


     Enter the size of second matrix:1 2 3 4


     Enter the elements of first matrix:1 2 3 4


     Enter the elements of second matrix:

     The multiplication is:

        7   10

        15  22




     Enter your choice(1,2,3,4,5,6,7):4


     Enter the size of matrix:2 2


     Enter the elements of first matrix:1 2 3 4


     The transepose is:

    1       3

    2       4


     Enter your choice(1,2,3,4,5,6,7):5


     Enter size of matrix:2 2


     Enter the elements of matrix:1 2 3 4

 Saddle point is present at(1 ,0):
 The element is=3



     Enter your choice(1,2,3,4,5,6,7):6


     Enter size of matrix:2 2


     Enter the elements of matrix:1 1 1 1


     Matrix is magic square:



*/







                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               





































                                                                               
                                                                               
                                                                               
                                                                               
                                              

                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                          







                                                                               











































































                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               






   

"C program for String operation"

#include<stdio.h>
#include<conio.h>
int length(char s1[20]);
void copy(char s1[20],char s2[20]);
void compare(char s1[20],char s2[20]);
void concat(char s1[20],char s2[20]);
void palindrome(char s1[20]);
void rev(char s1[20]);
int x,i,j,ch;

void main()
 {
   int op,i,j,k,ans,ch;
   char s1[10],s2[10],s3[10],t;
   clrscr();
   do
    {
    printf("\nOur Menu is:-");
    printf("\n 1. Length");
    printf("\n 2.Copy");
    printf("\n 3.Compare");
    printf("\n 4.Concat");
    printf("\n 5.Reverse");
    printf("\n 6.Palindrome");
    printf("\n 7.Exit");
    printf("\n\n enter option:");
    scanf("%d",&ch);
    switch(ch)
     {
       case 1:
           printf("\n Enter the string:");
           scanf("%s",s1);
           i=length(s1);
           printf("\n String length:%d",i);
           break;
       case 2:
           printf("\n Enter the string:");
           scanf("%s",s1);
         //  printf("\nEnter the string:");
         //  scanf("%s",s2);
        //   printf("\n String copied is:");
           copy(s1,s2);
           break;
       case 3:
           printf("\n Enter the first string:");
           scanf("%s",s1);
           printf("\n Enter the second string:");
           scanf("%s",s2);
           compare(s1,s2);
           break;
       case 4:
           printf("\n Enter the first string:");
           scanf("%s",s1);
           printf("\n Enter the second string:");
           scanf("%s",s2);
           concat(s1,s2);
           break;
       case 5:
           printf("\n Enter the string:");
           scanf("%s",s1);
           rev(s1);
           break;
       case 6:
           printf("\n Enter the string:");
           scanf("%s",s1);
           palindrome(s1);
           break;
       case 7 :
           exit(0);
       default :
           printf("Sorry Wrong choice");
           break;
     }
       printf("\n\nDo you want to continue ?(1/0)");
       scanf("%d",&ans);
    }while(ans==1);
  //    while(op!=7);
  getch();
 }

int length(char s1[20])
 {
   int i=0;

   while(s1[i]!='\0')
    {
      i++;
    }
    return(i);
  //  printf("\n String length:%d",i);
 }
void copy(char s1[20],char s2[20])
 {
   int i=0;
   while(s1[i]!=0)
    {
      s2[i]=s1[i];
      i++;
    }
   s2[i]='\0';
   printf("\nCopied String is:%s",s2);
 }

void compare(char s1[20],char s2[20])
 {
   int i,j,l,k,S;
   l=length(s1);
   k=length(s2);
   for(i=0,j=0;i<k,j<l;i++,j++)
    {
      S=s1[i]-s2[j];
    }
   if(S==0)
     printf("\nStrings are equals");
   else
     printf("\nStrings are not equals");
 }

void concat(char s1[20],char s2[20])
 {
   int i,j,l,k;
   l=length(s1);
   k=length(s2);
   for(i=l,j=0;i>=0,j<k;i++,j++)
    {
       s1[i]=s2[j];
    }
    s1[i]='\0';
    printf("\nThe concatinated string is:%s",s1);
 }

void rev(char s1[20])
 {
   int i,j,l;
   char t[20]={0};
   l=length(s1);
   for(i=l-1,j=0;i>=0,j<l;i--,j++)
    {
      t[j]=s1[i];
    }
   printf("\nReverse string is=%s",t);
 }

void palindrome(char s1[50])
 {
   int i,j,l,flag=0;
   char t[50]={0};
   l=length(s1);
   for(i=l-1,j=0;i>=0,j<l;i--,j++)
    {
    t[j]=s1[i];
    }
   printf("\nReverse string is:%s",t);
   for(i=0,j=0;i<l-1,j<l;i++,j++)
    {
      if(t[j]==s1[i])
    {
      flag=1;
      break;
    }
      else
     flag=0;
    }
   if(flag==1)
      printf("\nString is palindrome");
   else
      printf("\nString is not palindrome");
 }

 /*
 OUTPUT:-

Our Menu is:-
 1. Length
 2.Copy
 3.Compare
 4.Concat
 5.Reverse
 6.Palindrome
 7.Exit

 enter option:1

 Enter the string:skand

 String length:5

 enter option:2

 Enter the string:skand

Copied String is:skand

 enter option:3

 Enter the first string:skand
                                                                               
 Enter the second string:kumar                                                 
                                                                               
Strings are not equals                                                         
                                                                               
 enter option:4

 Enter the first string:skand

 Enter the second string: kumar

The concatinated string is:skandkumar


 enter option:
5

 Enter the string:skand

Reverse string is=dnaks


 enter option:6

 Enter the string:madam

Reverse string is:madam
String is palindrome
*/




                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               



                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               








                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

Few Linux App & commands which is going to used to Build AOSP(Android Open Source Project) Source Code

1. sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32n...