52. Use the greedy algorithm to make change using quarters, dimes, nickels, and
ID: 3816409 • Letter: 5
Question
52.
Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for
a)
87 cents.
b)
49 cents.
c)
99 cents.
d)
33 cents.
53.
Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for
a)
51 cents.
b)
69 cents.
c)
76 cents.
d)
60 cents.
52.
Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for
a)
87 cents.
b)
49 cents.
c)
99 cents.
d)
33 cents.
53.
Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for
a)
51 cents.
b)
69 cents.
c)
76 cents.
d)
60 cents.
MATH178 Discrete Mathematics Lab3 Due on Thursday April 13th by 4:00 pm Write a program that implements the greedy change-making algorithm. [Algorithm 6 on page 199 of the text.] The algorithm should be implemented using a void function that accepts two arrays one for input of the coin amounts and one for output of the count of each coin. The prototype is void greedy er (const int int[], const int, int) Here is a skeleton: include kiostream include kiomanip using namespace std; void greedychangeMaker (const int 11, int CJ, const int int int main const int NUM COINS 4; int coins NUM COINS 25 10 5, 1 int count NUM COINS (0, 0, 0, 0 const char CENT SYM 55 int n cout This program implements the greedy change-making algorithm endl cout It will calculate the fewest number of quarters, dimes, nickels end cout and pennies necessary to make change for an inputted amount." endl; cout Enter the amount cin n greedy ChangeMaker (coin count NUM COINS n) output the results system ("pause return 0 void greedychangeMaker (const int c int d 1, const int r, int, n) implementation of the algorithm.Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void greedyChangeMaker(const int[],int [],const int,int);
void spaces(int n)
{
for(int i=0;i<n;i++)
{
cout<<" ";
}
}
int digits(int n)
{
if(n==0)
{
return 1;
}
int count=0;
while(n>0)
{
n = n/10;
count++;
}
return count;
}
int main()
{
const int NUM_COINS=4;
int coins[NUM_COINS] = {25,10,5,1};
int count[NUM_COINS] = {0,0,0,0};
const char CENT_SYM = 155;
int n;
cout<<"This program implements the greedy change-making algorithm."<<endl;
cout<<"It will calculate the fewest number of quarters, dimes, nickels, "<<endl;
cout<<"and pennies neccesary to make change for an inputted amount."<<endl;
cout<<"Enter an amount: ";
cin>>n;
greedyChangeMaker(coins,count,NUM_COINS,n);
cout<<"The fewest number of coins to make change of "<<n<<" is:"<<endl;
spaces(10);
cout<<"NUMBER AMOUNT"<<endl;
spaces(14-digits(count[0]));
cout<<count[0];
spaces(10);
cout<<coins[0];
cout<<CENT_SYM<<endl;
spaces(14-digits(count[1]));
cout<<count[1];
spaces(10);
cout<<coins[1];
cout<<CENT_SYM<<endl;
spaces(14-digits(count[2]));
cout<<count[2];
spaces(11);
cout<<coins[2];
cout<<CENT_SYM<<endl;
spaces(14-digits(count[3]));
cout<<count[3];
spaces(11);
cout<<coins[3];
cout<<CENT_SYM<<endl;
system("pause");
return 0;
}
void greedyChangeMaker(const int c[],int d[],const int r,int n)
{
for(int i=0;i<r;i++)
{
d[i] = n/c[i];
n = n%c[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.