Saturday, 4 November 2017

C Program to sort given set of numbers using Heap sort.

#include<stdio.h>
#include<conio.h>
void main()
{
int heap[10],n,i,j,c,root,temp;
clrscr();
printf("\n Enter no of elements :");
scanf("%d",&n);
printf("\n Enter the nos : ");
for(i=0;i<n;i++)
scanf("%d",&heap[i]);
for(i=1;i<n;i++)
{
c=i;
do
{
root=(c-1)/2;
if(heap[root]<heap[c])
{
temp=heap[root];
heap[root]=heap[c];
heap[c]=temp;
}
c=root;
}while(c!=0);
}
printf("Heap array : ");
for(i=0;i<n; i++)
printf("%d\t", heap[i]);
for(j=n-1;j>=0;j--)
{
temp=heap[0];
heap[0]=heap[j];
heap[j]=temp;
root=0;
do
{
c=2*root+1;
if((heap[c]<heap[c+1])&&c<j-1)
c++;
if(heap[root]<heap[c]&&c<j)
{
temp=heap[root];
heap[root]=heap[c];
heap[c]=temp;
}
root=c;
}while(c<j);
}
printf("\n The sorted array is : ");
for(i=0;i<n;i++)
printf("\t %d",heap[i]);
getch();
}

Input/Output:


C Program to sort given set of numbers in ascending/descending order using Insertion sort and also search a number using linear search.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void insertion_sort(int a[],int);
void linear_search(int a[],int, int);
void main()
{
int data[100],i,n,element,ch;
while(1)
{
clrscr();
printf("****MENU****\nEnter 1 to apply insertion sort\nEnter 2 to search an element using Linear Search\nEnter 3 for exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
insertion_sort(data,n);
break;
case 2:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
printf("Enter the element you want to search for\n");
scanf("%d",&element);
linear_search(data,n,element);
break;
case 3:
exit(0);
default:
printf("You have entered wrong choice\n");
}
getch();
}
}
void insertion_sort(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
j=i;
temp=a[j];
while(j>0 && temp<a[j-1])
{
a[j]=a[j-1];
j--;
}
a[j]=temp;
}
printf("After sorting array elements are:- \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);

}
void linear_search(int a[],int n,int element)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==element)
{
printf("Element found at location %d\n",i+1);
return;

}


Input/Output:




C Program to sort given set of numbers in ascending/descending order using Bubble sort and also search a number using binary search.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void bubble_sort(int a[],int);
void binary_search(int a[],int, int);
void main()
{
int data[100],i,n,element,ch;
while(1)
{
clrscr();
printf("****MENU****\nEnter 1 to apply bubble sort\nEnter 2 to search an element using Binary Search\nEnter 3 for exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
break;
case 2:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
printf("Enter the element you want to search for\n");
scanf("%d",&element);
binary_search(data,n,element);
break;
case 3:
exit(0);
default:
printf("You have entered wrong choice\n");
}
getch();
}
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting array elements are:- \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
                              }
void binary_search(int a[],int n,int element)
{
int beg,end,mid;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(element==a[mid])
{
printf("Element found at location %d\n",mid+1);
return;
}
else if(element>a[mid])
beg=mid+1;
else
end=mid-1;
}
printf("Element not found\n");
return;
                              }#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void bubble_sort(int a[],int);
void binary_search(int a[],int, int);
void main()
{
int data[100],i,n,element,ch;
while(1)
{
clrscr();
printf("****MENU****\nEnter 1 to apply bubble sort\nEnter 2 to search an element using Binary Search\nEnter 3 for exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
break;
case 2:
printf("Enter the number of elements in array \n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
printf("enter %d element\n",i+1);
scanf("%d",&data[i]);
}
bubble_sort(data,n);
printf("Enter the element you want to search for\n");
scanf("%d",&element);
binary_search(data,n,element);
break;
case 3:
exit(0);
default:
printf("You have entered wrong choice\n");
}
getch();
}
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting array elements are:- \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
                              }
void binary_search(int a[],int n,int element)
{
int beg,end,mid;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(element==a[mid])
{
printf("Element found at location %d\n",mid+1);
return;
}
else if(element>a[mid])
beg=mid+1;
else
end=mid-1;
}
printf("Element not found\n");
return;
                              }


Input/Output:



C Programme for Euler Modified Methd

#include<stdio.h>
#include<math.h>
#include<string.h>
float fun(float,float);
main()
    {
        int i,j,c;
        float x[100],y[100],h,m[100],m1,m2,a,s[100],w;
        printf("\n     C program for Modified Euler Method \n\n");
        printf("  Enter the initial value of x:");
        scanf("%f",&x[0]);
        printf("\n  Enter the value of increment h:");
        scanf("%f",&h);
        printf("\n  Enter the final value of x:");
        scanf("%f",&a);
        printf("\n  Enter the initial value of the variable y :");
        scanf("%f",&y[0]);
        s[0]=y[0];
        for(i=1;x[i-1]<a;i++)
            {
                w=100.0;
               x[i]= x[i-1]+h;
               m[i]=fun(x[i-1],y[i-1]);
               c=0;
               while(w>0.0001)
                {
                    m1=fun(x[i],s[c]);
                    m2=(m[i]+m1)/2;
                    s[c+1]=y[i-1]+m2*h;
                    w=s[c]-s[c+1];
                    w=fabs(w);
                    c=c+1;
                }
              y[i]=s[c];
            }
        printf("\n\n The respective values of x and y are\n     x  \t     y\n\n");
        for(j=0;j<i;j++)
            {
                printf("  %f\t%f",x[j],y[j]);
                printf("\n");
            }
    }
float fun(float a,float b)
    {
        float c;
        c=a*a+b;
        return(c);
    }

Input/Output:


C Programme for Euler Method

#include<stdio.h>
float fun(float x,float y)
{
    float f;
    f=x+y;
    return f;
}
main()
{
    float a,b,x,y,h,t,k;
    printf("\nEnter x0,y0,h,xn: ");
    scanf("%f%f%f%f",&a,&b,&h,&t);
    x=a;
    y=b;
    printf("\n  x\t  y\n");
    while(x<=t)
    {
        k=h*fun(x,y);
        y=y+k;
        x=x+h;
        printf("%0.3f\t%0.3f\n",x,y);
    }
}


Input/Output:


Thursday, 2 November 2017

C Programme for Simpson 1/3 Rule

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{ return (1/(1+pow(x,2)));
}
void main()
{int i,n;
 float a,b,h,I,sum=0.0;
 clrscr();
 printf("enter the values of lower and upper limits 'a' and 'b'");
 scanf("%f%f",&a,&b);
 printf("enter the no. of subinetrvals n");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=f(a)+f(b);
 for(i=1;i<n;i++)
 {if(i%2==0)
 sum=sum+2*f(a+i*h);
 else
 sum=sum+4*f(a+i*h);
 }
 I=(h/3)*sum;
 printf("the value is %f:",I);
 getch();
}

Input/Output:


C Programme for Trapezoidal Rule

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{ return (1/(1+pow(x,2)));
}
void main()
{int i,n;
 float a,b,h,I,sum=0.0;
 clrscr();
 printf("enter the values of lower and upper limits 'a' and 'b'");
 scanf("%f%f",&a,&b);
 printf("enter the no. of subinetrvals n");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=f(a)+f(b);
 for(i=1;i<n;i++)
 {sum=sum+2*f(a+i*h);
 }
 I=(h/2)*sum;
 printf("the value is %f:",I);
 getch();
}

Input/Output: