Program For Greedy Algorithm
Program For Greedy Algorithm
#include<iostream.h>
#include<conio.h>
int a[10];
class greedy
{
public:
static int n,N,l;
void make_money();
int get_money();
void avail_coins();
void sort();
};
int greedy::n;
int greedy::N;
int greedy::l;
void greedy::make_money()
{
int A,i=0,b;
n=get_money();
avail_coins();
sort();
N=n;
while(N!=0)
{
A=N/(int(a[i]));
cout<<"\n"<<a[i]<<" amount of coin is"<<A;
b=N-(A*a[i]);
i++;
N=b;
}
}
void greedy::sort()
{
int j;
for(int i=1;i<l;i++)
{
int key=a[i];
j=i-1;
while(j+1>0 && a[j]<key)
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
int greedy::get_money()
{
int n;
cout<<"Enter the amount of bill :";
cin>>n;
return n;
}
void greedy::avail_coins()
{
int i=0;
cout<<"How many coins u want to take? ";
cin>>l;
cout<<"\n enter the values for coins :\n";
while(i<l)
{
cin>>a[i];
i++;
}
}
void main()
{
clrscr();
greedy p;
p.make_money();
getch();
}
Output: