Showing posts with label Conceptial programs. Show all posts
Showing posts with label Conceptial programs. Show all posts

Insertion sort program


#include
main()
{
int a[20],i,j,n,t;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the numbers to be sorted : \n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{
t=a[i];
j=i+1;
while(t{
a[j+1]=a[j];
j=j-1;
if (j<=0)
break;
}
a[j+1]=t;
}
printf("\nSorted elements : \n");
for(i=0;iprintf("%d\n",a[i]);
}

Heap sort Program

#include
int n,a[10],k;
void heapsort(int a[],int n);
void swap(int b,int c);
void pushdown(int x,int y);
main()
{
system("clear");
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements one by one...\n");
for(k=0;kscanf("%d",&a[k]);
for(k=0;kheapsort(a,n);
printf("\nElements after the Heap sort...\n");
for(k=0;kprintf("%d\n",a[k]);
}
void heapsort(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
pushdown(i,n);
for(i=n;i>=2;i--)
{
swap(1,i);
pushdown(1,i-1);
}
}
void pushdown(int first,int last)
{
int r;
r=first;
while(r<=last/2)
{
if(last==2*r)
{
if (a[r]>a[2*r])
swap(r,2*r);
r=last;
}
else
{
if (a[r]>a[2*r]&&a[2*r]<=a[2*r+1])
{
swap(r,2*r);
r=2*r;
}
else if(a[r]>a[2*r+1]&&a[2*r+1]{
swap(r,2*r+1);
r=2*r+1;
}
else
r=last;
}
}
}
void swap(int m,int x)
{
int t;
t=a[m];
a[m]=a[x];
a[x]=t;
}

E-series calculation



#include
#include "math.h"
int fact(int i);
main()
{
float x;
int n,i=1;
float term=0.0,sum=0.0;
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nEnter the value of x : ");
scanf("%f",&x);
x=x*(3.14/180.0);
for(i=0;i{
term=(pow(x,i))/fact(i);
sum=sum+term;
}
printf("\nValue of sum is (i.e e(x)) : %f\n",sum);
}
int fact(int n)
{
int i,s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;
}

Cos series calculation


#include
#include "math.h"
int fact(int i);
main()
{
float x;
int n,i=1;
float term=0.0,sum=0.0,sign=-1;
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nEnter the value of x : ");
scanf("%f",&x);
x=x*(3.14/180.0);
for(i=0;i{
sign=-1*sign;
term=(sign*pow(x,i))/fact(i);
sum=sum+term;
}
printf("\nValue of sum is (i.e cos(x)) : %f\n",sum);
}
int fact(int n)
{
int i,s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;
}

Bubble sort Program



#include
main()
{
int a[10],n,i,pass;
system("clear");
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements one by one...\n");
for(i=0;iscanf("%d",&a[i]);
printf("\nMaking the sorting of elements in Bubble Sort...\n");
for(pass=0;passfor(i=0;iif (a[i]>a[i+1])
{
a[i]=a[i]+a[i+1];
a[i+1]=a[i]-a[i+1];
a[i]=a[i]-a[i+1];
}
printf("\nElements in the order after Bubble sort..\n");
for(i=0;iprintf("%d\n",a[i]);
}


Euclid Algorithm Implementation

#include
#include

class euclid
{
int n,m,r;
public:
void getdata();
int eu_alg(int m,int n);
};

void main()
{
clrscr();
euclid e;
e.getdata();
getch();
}

void euclid::getdata()
{
cout<<"Enter the two numbers : ";
cin>>m>>n;
r=eu_alg(m,n);
cout<<"The GCD of the given numbers : "<}

int euclid::eu_alg(int m,int n)
{
while(n>1)
{
r=m%n;
m=n;
n=r;
}
return m;
}

Consecutive Integer Checking Program

#include
#include

class cons
{
int n,m,t,r;
public:
void getdata();
int cic(int m,int n,int t);
int min(int m,int n);
};

void cons::getdata()
{
cout<<"Enter the two numbers : ";
cin>>m>>n;
t=min(m,n);
t=cic(m,n,t);
if (t!=1)
cout<<"The GCD of two numbers : "<else
cout<<"The given two numbers doesn't have GCD value!"<}

int cons::min(int m,int n)
{
if (m t=m;
else
t=n;
return t;
}

int cons::cic(int m,int n,int t)
{
l10:
r=m%t;
if (r==0)
{
r=n%t;
if (r==0)
return t;
}
t--;
goto l10;
}

void main()
{
clrscr();
cons c;
c.getdata();
getch();
}

Optimal Binary Search Tree


#include
#include
#define infinity 1000
class obst
{
int kmin,k,n,i,d,s,j,root[10][10];
float sum,table[10][10],prob[10],min;
public:
obst()
{
for(i=1;i<=n;i++)
for(j=0;j<=n;j++)
{
table[i][j]=float(0);
root[i][i]=float(0);
}
}
void getdata();
void obst1();
void display();
};
void obst::getdata()
{
cout<<"\nEnter the number of objects : ";
cin>>n;
cout<<"\nEnter the probability : ";
for(i=1;i<=n;i++)
cin>>prob[i];
}

void obst::obst1()
{
for(i=1;i<=n;i++)
{
table[i][i]=prob[i];
root[i][i]=float(i);
}
table[n+1][n]=float(0);
for(d=1;d{
for(i=1;i<=n-d;i++)
{
sum=float(0);
j=i+d;
min=infinity;
for(k=i;k<=j;k++)
{
if ((table[i][k-1]+table[k+1][j]){
min=table[i][k-1]+table[k+1][j];
kmin=k;
}
}
root[i][j]=kmin;
for(s=i;s<=j;s++)
sum=sum+prob[s];
table[i][j]=sum+min;
}
}
}
void obst::display()
{
cout<<"\n\tMAIN TABLE\n";
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(j!=0)
cout<<"\t"<else
cout<<"0.0";
}
cout<}
cout<<"\n\t\tROOT TABLE\n";
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(j!=0)
cout<<"\t"<else
cout<<"0";
}
cout<}
}
void main()
{
clrscr();
obst ob;
ob.getdata();
ob.obst1();
ob.display();
getch();
}