0% found this document useful (0 votes)
43 views2 pages

Program For Greedy Algorithm

This program implements a greedy algorithm to determine the minimum number of coins needed to make change for a given amount of money. It takes user input for the amount of money owed and the denominations of available coins, sorts the coins in descending order, and iteratively chooses the coin with the highest value that still leaves the amount owed greater than zero until no coins remain.

Uploaded by

Jiten Amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Program For Greedy Algorithm

This program implements a greedy algorithm to determine the minimum number of coins needed to make change for a given amount of money. It takes user input for the amount of money owed and the denominations of available coins, sorts the coins in descending order, and iteratively chooses the coin with the highest value that still leaves the amount owed greater than zero until no coins remain.

Uploaded by

Jiten Amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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:

You might also like