Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Monday, February 10, 2014

C PROGRAM FOR SELECTION SORT

/*C PROGRAM FOR SELECTION SORT*/

#include<stdio.h>
#include<conio.h>
int main() {
   int array[50], n, c, d, position, swap;
   printf("Enter number of elements\n");
   scanf("%d", &n);
   printf("Enter %d integers\n", n);
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
   for ( c = 0 ; c < ( n - 1 ) ; c++ ) {
      position = c;
      for ( d = c + 1 ; d < n ; d++ )  {
         if (array[position]>array[d])
            position = d; }
      if(position!=c) {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap; }
   }
   printf("Sorted list in ascending order:\n");
   for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
   getch();
   return 0;
}

/*OUTPUT:-
Enter number of elements
5
Enter 5 integers
2
8
1
3
45
Sorted list in ascending order:
1
2
3
8
45 */

C PROGRAM FOR QUICK SORT

/*C PROGRAM FOR QUICK SORT*/

#include<stdio.h>
#include<conio.h>
void quicksort(int [10], int, int);
int main( ) {
    int x[20],size,i;
    clrscr();
    printf("Enter size of the array: " );
    scanf("%d",&size);
    printf("Enter %d elements : " , size);
    for(i=0;i<size;i++)
    scanf("%d",&x[i]);
    quicksort(x,0,size-1);
    printf(" Sorted elements : " );
    for(i=0;i<size;i++)
    printf(" %d " , x[i]);
    getch();
    return 0;
    }
void quicksort(int x[10],int first, int last) {
     int pivot,j,temp,i;
     if(first<last) {
          pivot=first;
          i=first;
          j=last;
          while(i<j) {
               while(x[i]<=x[pivot]&&i<last)
               i++;
               while(x[j]>x[pivot])
               j--;
               if(i<j) {
                   temp = x[i];
                   x[i]=x[j];
                   x[j]=temp;
                   }
                   }
                   temp = x[pivot];
                   x[pivot] = x[j];
                   x[j]=temp;
                   quicksort(x,first,j-1);
                   quicksort(x,j+1,last);
                   }
                   }
/*OUTPUT:
Enter size of the array: 4
Enter 4 elements : 12
45
3
5
 Sorted elements :  3  5  12  45 */

C PROGRAM FOR MERGE SORT

/*C PROGRAM FOR MERGE SORT*/

#include<stdio.h>
#include<conio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main()
{
    int merge[MAX],i,n;
     clrscr();
    printf("Enter the total number of elements: ");
    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
     scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
     printf("%d ",merge[i]);
    }
     getch();
   return 0;
}

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
     mid=(low+high)/2;
     partition(arr,low,mid);
     partition(arr,mid+1,high);
     mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

     if(arr[l]<=arr[m]){
         temp[i]=arr[l];
         l++;
     }
     else{
         temp[i]=arr[m];
         m++;
     }
     i++;
    }

    if(l>mid){
     for(k=m;k<=high;k++){
         temp[i]=arr[k];
         i++;
     }
    }
    else{
     for(k=l;k<=mid;k++){
         temp[i]=arr[k];
         i++;
     }
    }

    for(k=low;k<=high;k++){
     arr[k]=temp[k];
    }
}

/*OUTPUT:
Enter the total number of elements: 4
Enter the elements which to be sort: 102
450
32
1
After merge sorting elements are: 1 32 102 450 */

C PROGRAM FOR INSERTION SORT

/*C PROGRAM FOR INSERTION SORT*/

#include<stdio.h>
#include<conio.h>
int main() {
int n ,array[50], c, d, t;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
for(c=1;c<=n-1;c++)
{ d=c;
while(d>0 && array[d]<array[d-1]) {
    t=array[d];
    array[d]=array[d-1];
    array[d-1]=t;
    d--;} }
    printf("Sorted list in ascending order: \n");
    for(c=0;c<=n-1;c++)
         printf("%d\n",array[c]);
    getch();
    return 0 ;
 }

/*OUTPUT:-
Enter number of elements
5
Enter 5 integers
4
8
2
1
65
Sorted list in ascending order:
1
2
4
8
65 */

C PROGRAM FOR HEAP SORTING

/*C PROGRAM FOR HEAP SORTING*/

#include<stdio.h>
#include<conio.h>
void manage(int *, int);
void heapsort(int *, int, int);
int main()
{
 int arr[20];
 int i,j,size,tmp,k;
// clrscr();
 printf("\n\t------- Heap sorting method -------\n\n");
 printf("Enter the number of elements to sort : ");
 scanf("%d",&size);
 for(i=1; i<=size; i++)
 {
   printf("Enter %d element : ",i);
   scanf("%d",&arr[i]);
   manage(arr,i);
 }
 j=size;
 for(i=1; i<=j; i++)
 {
   tmp=arr[1];
   arr[1]=arr[size];
   arr[size]=tmp;
   size--;
   heapsort(arr,1,size);
 }
 printf("\n\t------- Heap sorted elements -------\n\n");
 size=j;
 for(i=1; i<=size; i++)
     printf("%d ",arr[i]);
 getch();
 return 0;
}


void manage(int *arr, int i)
{
 int tmp;
 tmp=arr[i];
 while((i>1)&&(arr[i/2]<tmp))
 {
   arr[i]=arr[i/2];
   i=i/2;
 }
 arr[i]=tmp;
}


void heapsort(int *arr, int i, int size)
{
 int tmp,j;
 tmp=arr[i];
 j=i*2;
 while(j<=size)
 {
   if((j<size)&&(arr[j]<arr[j+1]))
      j++;
   if(arr[j]<arr[j/2])
      break;
   arr[j/2]=arr[j];
   j=j*2;
 }
 arr[j/2]=tmp;
}

/*OUTPUT:

        ------- Heap sorting method -------

Enter the number of elements to sort : 4
Enter 1 element : 12
Enter 2 element : 45
Enter 3 element : 3
Enter 4 element : 8

        ------- Heap sorted elements -------

3 8 12 45 */

C PROGRAM FOR BINARY SEARCH

/*C PROGRAM FOR BINARY SEARCH*/

#include<stdio.h>
#include<conio.h>
int main() {
int a[50],i,n,key,lb,ub,mid;
clrscr();
printf("Enter no. of terms=");
scanf("%d",&n);
printf("Enter the terms in ascending order:\n");
for(i=0;i<n;i++)
    scanf("%d", &a[i]);
lb = 0;
ub = n-1;
printf("Enter key to be searched=");
scanf("%d",&key);
mid = (lb+ub)/2;
while(lb<=ub){
    if(a[mid]<key)
    lb = mid +1;
    else if(a[mid]==key)
    {
    printf("%d found at location %d",key,mid+1);
    break;
    }
    else
    ub = mid - 1;
    mid = (lb+ub)/2;
}
if(lb>ub)
printf("%d is not found in the list",key);
getch();
return 0;
}

/*OUTPUT:
Enter no. of terms=4
Enter the terms in ascending order:
10
23
56
78
Enter key to be searched=56
56 found at location 3*/

Tuesday, July 2, 2013

C PROGRAM FOR WEDDLE'S RULE

/*PROGRAM FOR WEDDLE'S RULE*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
 float f(float x)
  {     float y;
        y=1/(1+x*x);
        return(y); }                        
void main() {
      float a,b,h,s1=0,s2=0,s=0;
      int i,n,m;
      clrscr();
      printf("Enter the value of upper limit= ");
      scanf("%f",&b);
       printf("Enter the value of lower limit= ");
      scanf("%f",&a);
       printf("Enter the value of n=");
      scanf("%d",&n);
      h=(b-a)/n;
      printf("h= %f",h);
      m=n/6;
      s=0;
      if(n%6==0) {
      for(i=1;i<=m;i++){
          s=s+((3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h)));
          a=a+6*h;  }
          printf("\nResult is : %f",s);}
          else {
              printf(" Weddle’s rule is not applicable");}      
              getch();
 }

/*OUTPUT:-
Enter the value of upper limit= 6
Enter the value of lower limit= 0
Enter the value of n=6
h= 1.000000
Result is : 1.373448  */

C PROGRAM FOR TRAPEZOIDAL RULE OF NUMERICAL INTEGRATION

/*PROGRAM FOR TRAPEZOIDAL RULE OF NUMERICAL INTEGRATION*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(X)  (4*X-(3*(X*X)))
void main() {
int i,n;
float a,b,h,x[20],y[20],j;
clrscr();
printf("Enter upper limit,lower limit & sub interval:");
scanf("%f %f %d",&a,&b,&n);
x[0]=b;
h=(a-b)/n;
for(i=0;i<=n;i++) {
      y[i]=f(x[i]);
      x[i+1]=x[i] + h;
}
j=y[0];
for(i=0;i<=n-1;i++)
j=j + ((h/2) * (y[i]+y[i+1]));
printf("The required value is=%f ", j);
getch();
}
/*OUTPUT:-
Enter upper limit,lower limit & sub interval:1
0
10
The required value is=0.995000  */

C PROGRAM FOR SIMPSON'S 1/3rd RULE OF NUMERICAL INTEGRATION

/*PROGRAM FOR SIMPSON'S 1/3rd RULE OF NUMERICAL INTEGRATION*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(X)  (4*X-(3*(X*X)))
void main() {
int i,n;
float a,b,h,x[20],y[20],j;
clrscr();
printf("Enter upper limit,lower limit & sub interval:");
scanf("%f %f %d",&a,&b,&n);
x[0]=b;
h=(a-b)/n;
for(i=0;i<=n;i++) {
      y[i]=f(x[i]);
      x[i+1]=x[i] + h;
}
j=y[0];
for(i=0;i<=n-2;i+=2)
j=j + ((h/3) * (y[i]+4*y[i+1]+y[i+2]));
printf("The required value is=%f ", j);
getch();
}

/*OUTPUT:-
Enter upper limit,lower limit & sub interval:1
0
10
The required value is=1.000000  */

C PROGRAM FOR NEWTON RAPHSON METHOD

/* PROGRAM FOR NEWTON RAPHSON METHOD*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
{ double f(double), f1(double)
   double x[100];            
                 }


main() {
       float a[20][20],ratio,x[20];
       int i,j,k,n;
       clrscr();
       printf("\n Enter order of matrix:");
       scanf("%d", &n);
       printf("Enter the coefficients & RHS \n");
       for(i=1;i<=n;i++){
          for(j=1;j<=n+1;j++)
          scanf("%f", &a[i][j]);
          printf("\n"); }
       for(k=1;k<=n-1;k++){
          for(i=k+1;i<=n;i++){
                  ratio=a[i][k]/a[k][k];
                  for(j=1;j<=n+1;j++)
                  a[i][j] = a[i][j] - ratio * a[k][j];}}
                  x[n]=a[n][n+1]/a[n][n];                            
       for(k=n-1;k>=1;k--){
         x[k] = a[k][n+1];
         for(j = k+1;j<=n;j++)
         x[k] = x[k]-a[k][j] * x[j];
         x[k]= x[k]/a[k][k];}
       for(i=1;i<=n;i++)
         printf("\n x(%d)=%g",i,x[i]);                
       getch();
       }

C PROGRAM FOR NEWTON FORWARD DIFFERENCE FORMULA FOR INTERPOLATION

/*PROGRAM FOR NEWTON FORWARD DIFFERENCE FORMULA FOR INTERPOLATION */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
int i,n,j,fact=1;
float x[20],y[20][20],h,d=1,p,a,f;
clrscr();
printf("Enter The Value of n:");
scanf("%d",&n);
printf("\nEnter the elements of x:");
for(i=1;i<=n;i++)
scanf("%f", &x[i]);
printf("\nEnter The Elements of y:");
for(i=1;i<=n;i++)
scanf("%f",&y[i][1]);
h=x[2]-x[1];
printf("Enter x for which y is to be calculated:");
scanf("%f",&f);
p = (f-x[1])/h;
a=y[1][1];
for(j=2;j<=n;j++) {
for(i=1;i<=(n-j)+1;i++)
y[i][j]= y[i+1][j-1] - y[i][j-1];
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
    {
printf("\t%.2f",x[i]);
for(j=1;j<=(n-i)+1;j++)
   printf("\t%.2f",y[i][j]);
printf("\n");
    }
for(j=2;j<=n;j++) {
for(i=1;i<j;i++)
fact=fact*i;

d = d*(p-(j-1));
a = a + (y[1][j]*d)/fact;
fact=1;
}
printf("\n\nFor x=%f The Value of y is %f",f,a);
getch();
}

/* OUTPUT:-
Enter The Value of n:4

Enter the elements of x:0
1
2
3

Enter The Elements of y:1
0
1
10
Enter x for which y is to be calculated:4
The Table is :

        0.00    1.00    -1.00   2.00    6.00
        1.00    0.00    1.00    8.00
        2.00    1.00    9.00
        3.00    10.00


For x=4.000000 The Value of y is 10.000000  */

C PROGRAM FOR NEWTON BACKWARD DIFFERENCE FORMULA FOR INTERPOLATION

/*PROGRAM FOR NEWTON BACKWARD DIFFERENCE FORMULA FOR INTERPOLATION */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
int i,n,j,fact=1;
float x[20],y[20][20],h,d=1,p,a,f;
clrscr();
printf("Enter The Value of n:");
scanf("%d",&n);
printf("\nEnter the elements of x:");
for(i=1;i<=n;i++)
scanf("%f", &x[i]);
printf("\nEnter The Elements of y:");
for(i=1;i<=n;i++)
scanf("%f",&y[i][1]);
h=x[2]-x[1];
printf("Enter x for which y is to be calculated:");
scanf("%f",&f);
p = (f-x[n])/h;
a=y[n][1];
for(j=2;j<=n;j++) {
for(i=n;i>=1;i--)
y[i][j]= y[i][j-1] - y[i-1][j-1];
}
printf("The Table is :\n\n");
for(i=1;i<=n;i++)
    {
printf("\t%.2f",x[i]);
for(j=1;j<=i;j++)
   printf("\t%.2f",y[i][j]);
printf("\n");
    }
for(j=2;j<=n;j++) {
for(i=1;i<j;i++)
fact=fact*i;

d = d*(p+(j-2));
a = a + (y[n][j]*d)/fact;
fact=1;
}
printf("\n\nFor x=%f The Value of y is %f",f,a);
getch();
}

/* OUTPUT:-
Enter The Value of n:5

Enter the elements of x:.1
.2
.3
.4
.5

Enter The Elements of y:.09
.19
.29
.38
.47
Enter x for which y is to be calculated:.15
The Table is :

        0.10    0.09
        0.20    0.19    0.10
        0.30    0.29    0.10    0.00
        0.40    0.38    0.09    -0.01   -0.01
        0.50    0.47    0.09    0.00    0.01    0.02


For x=0.150000 The Value of y is 0.138594       */

C PROGRAM FOR LAGRANGE'S INTERPOLATION FORMULA

/*PROGRAM FOR LAGRANGE'S INTERPOLATION FORMULA*/

#include<stdio.h>
#include<conio.h>
void main () {
  int i,j, n;
  float x[20],y[20], p[20],f,k=0;
  clrscr();
  printf("Enter the number of terms:");
  scanf("%d",&n);
  printf("Enter the values of:\nx y\n");
  for(i=0;i<n;i++) {
  scanf("%f %f",&x[i], &y[i]);
  }
  printf("Enter value of x for which y is to be found:");
  scanf("%f",&f);
  for(j=1;j<=n;j++) {
  p[j]=1.0;
  for(i=1;i<=n;i++) {
  if(i==j)
  continue;
  p[j]=p[j] * (f-x[i])/(x[j]-x[i]);
  }
  }
  for(i=1;i<=n;i++)
  k = k + p[i]* y[i];
  printf("for x = %f, y = %f",f,k);

getch();
}

/*OUTPUT:-
Enter the number of terms:4
Enter the values of:
x       y
5       12
6       13
9       14
11      16
Enter value of x for which y is to be found:10
for x = 10.000000, y = 14.744108   */

C PROGRAM FOR GAUSS ELIMINATION METHOD

/* PROGRAM FOR GAUSS ELIMINATION METHOD*/
#include<stdio.h>
#include<conio.h>
void main() {
       float a[20][20],ratio,x[20];
       int i,j,k,n;
       clrscr();
       printf("\n Enter order of matrix:");
       scanf("%d", &n);
       printf("Enter the coefficients & RHS \n");
       for(i=1;i<=n;i++){
          for(j=1;j<=n+1;j++)
          scanf("%f", &a[i][j]);
          printf("\n"); }
       for(k=1;k<=n-1;k++){
          for(i=k+1;i<=n;i++){
                  ratio=a[i][k]/a[k][k];
                  for(j=1;j<=n+1;j++)
                  a[i][j] = a[i][j] - ratio * a[k][j];}}
                  x[n]=a[n][n+1]/a[n][n];                            
       for(k=n-1;k>=1;k--){
         x[k] = a[k][n+1];
         for(j = k+1;j<=n;j++)
         x[k] = x[k]-a[k][j] * x[j];
         x[k]= x[k]/a[k][k];}
       for(i=1;i<=n;i++)
         printf("\n x(%d)=%g",i,x[i]);                
       getch();
       }

/*OUTPUT:-
Enter order of matrix:4
Enter the coefficients & RHS
2       2       1       2       7

-1      2       0       1       -2

-3      1       2       1       -3

-1      0       0       2       0


 x(1)=2.16216
 x(2)=-0.45946
 x(3)=1.43243
 x(4)=1.08108 */

C PROGRAM TO FIND THE MAXIMUM VALUE USING ARRAY

\* PROGRAM TO FIND THE MAXIMUM VALUE USING ARRAY*\

#include<stdio.h>
#include<conio.h>
void main() {
int a[155],n,i,max;
               clrscr();
printf("Enter No. of items:");
scanf("%d", &n);
for(i=0;i<n;i++)
{ printf("Enter value a[%d]", i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\t%d", a[i]);
}
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
   printf("\nMaximum value is %d", max);
getch();
}



\*OUTPUT:
         
Enter No. of items:4
Enter value a[0]5
Enter value a[1]7
Enter value a[2]99
Enter value a[3]656
        5       7       99      656
Maximum value is 656  *\

C PROGRAM FOR STACK

/*PROGRAM TO MAKE A STACK TEST PUSH & POP OPERATION IN A STACK*/
#include<stdio.h>
#include<conio.h>
#define max 5
void PUSH();
void POP();
void DISPLAY();
int a[max], item, i, v, top=-1, n;
void main()
{
clrscr();
do {
printf("\nChoice are:");
printf("\n1: PUSH");
printf("\n2: POP");
printf("\n3: DISPLAY ITEMS");
printf("\n4: EXIT");
printf("\nEnter your choice:");
scanf("%d", &n);
switch(n) {
case 1: { PUSH();
break; }
case 2: { POP();
    break; }
case 3: { DISPLAY();
break; }
}
}
while (n!=4);
}

void PUSH() {
if(top==max-1)
printf("\nSTACK OVERFLOW\n");
else {
top=top+1;
printf("\nEnter pushing value:");
scanf("%d", &v);
a[top] = v; }
}
void POP() {
if(top==-1)
printf("\nSTACK UNDERFLOW\n");
else {
item = a[top];
printf("The poped item is %d", item);
top= top-1; }
}
void DISPLAY() {
printf("\nThe items in the STACK are:");
for(i=0;i<=top;i++)
printf(" %d " , a[i]); }





/*OUTPUT:-

Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter pushing value:30

Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the STACK are: 30
Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:2
The poped item is 30
Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:4 */

C PROGRAM FOR LINEAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN LINEAR QUEUE*/
#include<stdio.h>
#include<conio.h>
void main() {
int max=5, n, r=0, f=0, a[5], v, item, i;
clrscr();
do {
printf("\nChoice are:");
printf("\n1: INSERTION");
printf("\n2: DELETION");
printf("\n3: DISPLAY ITEMS");
printf("\n4: EXIT");
printf("\nEnter your choice:");
scanf("%d", &n);
switch(n) {
case 1: {
 if(r==max)
 printf("\nQUEUE REACHED LIMIT\n");
 else {
 printf("\nEnter insertion value:");
 scanf("%d", &v);
 a[r]=v;
 r++; }
 break;
}
case 2: { if(f==r)
 printf("\nQUEUE IS EMPTY");
 else {
 item=a[f];
 f++;
 printf("\nDeleted item is: %d", item); }
 break;
}
case 3: { printf("\nThe items in the QUEUE are:");
 for(i=f;i<r;i++)
 printf(" %d ", a[i]);
 break; }
}
}
while(n!=4);
}
















/*OUTPUT:-

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter insertion value:1

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter insertion value:2

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the QUEUE are: 1  2
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:2

Deleted item is: 1
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the QUEUE are: 2
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:4   */

C PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION

/*PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION*/

#include<stdio.h>
#include<conio.h>
int b,c,d,n,i,j,v,p,m,x;
int a[100];
void insertion();
void deletion();
void main() {
clrscr();
printf("Enter array size:");
scanf("%d", &n);
printf("\nEnter values of array:\n");
for(b=0;b<n;b++) {
scanf("%d", &a[b]);
x=b+1; }
do {
printf("\nChoices are:\n");
printf("1: Insertion\n");
printf("2: Deletion\n");
printf("3: Display\n");
printf("4: Exit\n");
printf("\nEnter Your Choice:\n");
scanf("%d", &c);
switch(c) {
case 1: {
insertion();
break; }
case 2: {
deletion();
break; }
case 3: {
printf("\nThe values in the array are:");
for(d=0;d<x;d++)
printf(" %d ", a[d]);
break; }
}
}
while(c!=4);
getch();
}

void insertion() {
if(x!=n) {
printf("\nEnter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):");
scanf("%d", &p);
printf("\nEnter insertion value:");
scanf("%d", &v);
for(i=x-1;i>=p;i--)
a[i+1]= a[i];
a[p]= v;
x=x+1; }
else
printf("Array is full"); }

void deletion() {
if(x!=0) {
printf("\nEnter deletion Position 0 to n:");
scanf("%d", &m);
for(j=m;j<n-1;j++)
a[j]= a[j+1];
x=x-1; }
else
printf("Array is empty"); }


/*OUTPUT:
       
Enter array size:3

Enter values of array:
1
2
3

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  2  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
2

Enter deletion Position 0 to n:1

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
1

Enter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):1

Enter insertion value:9

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  9  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
4*/

C PROGRAM FOR CIRCULAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN CIRCULAR QUEUE*/
#include<stdio.h>
#include<conio.h>
#define max 5
int front,rear,q[max];
void inqueue();
void delqueue();
void qdisplay();
int main() {
int c;
clrscr();
front=rear=-1;
do {
printf("\n1:INSERTION \n2:DELETION\n3:DISPLAY ");
printf("\n4:EXIT\nEnter Your Choice:");
scanf("%d",&c);
switch(c) {
case 1: {inqueue ();
break; }
case 2: {delqueue ();
break; }
case 3: {qdisplay ();
break; }
}
}
while(c!=4);
}

void inqueue() {
int x;
if ((front==0&&rear==max-1)|| (front==rear+1))
{ printf("\nQUEUE OVERFLOW\n");
return; }
if(front==-1)
{ front=rear=0; }
else
{ if(rear==max-1)
{ rear=0; }
else { rear++; } }
printf("\nEnter The Number:");
scanf("%d",&x);
q[rear]=x;
return; }

void delqueue() {
int y;
if(front==-1) {
printf("\nQUEUE IS UNDERFLOW \n");
return; }
y=q[front];
if(front==rear)
{ front=rear=-1; }
else
{ if(front==max-1)
{ front=0; }
else
{ front++; } }
printf("\n%d SUCESSFULLY DELETED \n",y);
return; }

void qdisplay() {
int i,j;
if(front==rear==-1)
{ printf("\nQUEUE IS EMPTY \n");
return; }
printf("\nITEMS ARE:");
for(i=front;i!=rear;i=(i +1)%max)
{ printf(" %d ",q[i]); }
printf(" %d ",q[rear]);
return; }





/************************************************************/
/*OUTPUT:-\

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:1

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:2

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:3

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:4

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:5

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

QUEUE OVERFLOW

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 1  2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:2

1 SUCESSFULLY DELETED

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:7

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5  7
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:4 */

C PROGRAM FOR 2D MATRIX MULTIPLICATION

/*PROGRAM TO MULTIPLY TWO MATRICES*/

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,k,m,n,o,p,sum=0;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter order of 1st 2D matrix:");
scanf("%d %d",&m,&n);
printf("Enter order of 2nd 2D matrix");
scanf("%d %d",&o,&p);
if(n==o){
printf("Enter elements of 1st 2d matrix:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  printf("\na[%d][%d]=", i,j);
                  scanf("%d", &a[i][j]);
                  }
printf("Enter elements of 2nd 2D matrix:");
for(i=1;i<=o;i++)
for(j=1;j<=p;j++){
                  printf("\nb[%d][%d]=", i,j);
                  scanf("%d", &b[i][j]);
                  }
printf("\nThe product of two matrices has these elements:");
for(i=1;i<=m;i++)
for(j=1;j<=p;j++){
                  for(k=1;k<=n;k++)
                  sum=sum+a[i][k]*b[k][j];
                  c[i][j]=sum;
                  sum=0;
                  printf("\nc[%d][%d]=%d", i,j,c[i][j]);
                  }                
}
else
printf("The matrices cannot be multiplied");
getch();
}

/* OUTPUT:-
Enter order of 1st 2D matrix:1
2
Enter order of 2nd 2D matrix2
3
Enter elements of 1st 2d matrix:
a[1][1]=1

a[1][2]=2
Enter elements of 2nd 2D matrix:
b[1][1]=3

b[1][2]=4

b[1][3]=5

b[2][1]=6

b[2][2]=7

b[2][3]=8

The product of two matrices has these elements:
c[1][1]=15
c[1][2]=18
c[1][3]=21  */