Question

Suppose we want to make change for N cents, using the least number of coins of...

Suppose we want to make change for N cents, using the least number of coins of denominations {1, 5, 10} cents (Different from the US currency system!). Consider the following greedy strategy: suppose the amount left to change is M, take the largest coin that is no more than M; subtract this coin's value from M, and repeat.

  1. Implement the Greedy Coin Changing Algorithm in Java.

public class Coinchange {
   public static int greedycoinchange(int givenvalue, int[] givencoins)
   {
       // Complete the code here to make change of givenvalue using coins in the array givencoins
       // Minimize the number of coins used
       // Input: Coin denominations in array givencoins are already sorted in descending order
       // Output: The number of coins used to make change of givenvalue          
   }
  
   public static void main(String[] args) {
       int n = 0; // n cents
       Scanner reader = new Scanner(System.in);
       System.out.println("Please enter the value you want to make change: ");
       n = reader.nextInt();
       int[] coins = {25, 10, 5, 1};
      
       System.out.println("used "+ greedycoinchange(n, coins)+" coins for "+ n + " cents");  
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

import java.util.*;
public class Coinchange {
public static int greedycoinchange(int givenvalue, int[] givencoins)
{
Vector<Integer> t_coins = new Vector<>(); /*here we create a vector of store a coins value*/
int n=givencoins.length; /*here we find a size of givencoins array and store into n*/
  
for (int i = 0; i < n; i++) /*take one for loop for all the coins value*/   
{
/* Find coins */
while (givenvalue >= givencoins[i]) /*here we check if value grater that coins value than we take that coins*/
{
givenvalue -= givencoins[i]; /*substract that coins value from given value*/
t_coins.add(givencoins[i]); /*add coin into vector*/
}
}
return t_coins.size(); /*return the size of vector*/
}
  
public static void main(String[] args) {
int n = 0; // n cents
Scanner reader = new Scanner(System.in);   
System.out.println("Please enter the value you want to make change: ");
n = reader.nextInt(); /*take value from user*/
int[] coins = {25, 10, 5, 1};
  
System.out.println("used "+ greedycoinchange(n, coins)+" coins for "+ n + " cents"); /*print total used coins*/
}
}

Add a comment
Know the answer?
Add Answer to:
Suppose we want to make change for N cents, using the least number of coins of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we want to make change for n cents, using the least number of coins of...

    Suppose we want to make change for n cents, using the least number of coins of denominations 1, 10, and 25 cents. Consider the following greedy strategy: suppose the amount left to change is m; take the largest coin that is no more than m; subtract this coin's value from m, and repeat. Either give a counter example, to prove that this algorithm can output a non-optimal solution or prove that this algorithm always outputs an optimal solution.

  • Making Change: Given coins of denominations (value) 1 = v1 < v2< … < vn, we...

    Making Change: Given coins of denominations (value) 1 = v1 < v2< … < vn, we wish to make change for an amount A using as few coins as possible. Assume that vi’s and A are integers. Since v1= 1 there will always be a solution. Formally, an algorithm for this problem should take as input:  An array V where V[i] is the value of the coin of the ith denomination.  A value A which is the amount...

  • Can you modify this code so that it display the number of coins and subtotal for...

    Can you modify this code so that it display the number of coins and subtotal for each type of coin(dimes, penny, quarter, nickel) and the total value in the wallet. But still reads the data from file coins.txt. Which is below MyCode. The original question was this: Two classes are given: Coin and Wallet. Write an application named MyWallet.java that reads all coins from a data file named coins.txt. The first line in the data file contains the number of...

  • Given a value V in cents, you need to make change using a minimum number of...

    Given a value V in cents, you need to make change using a minimum number of coins. Assume you have an infinite supply of quarters, nickels, dimes, and pennies. Find the minimum number of coins. Display the quantity of each coin and the total number of coins, similar to the example output below. Use global constant identifiers, of type unsigned int, for the values of quarters, nickels, dimes, and pennies. Example: const unsigned int QUARTER = 25: Do not use...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • Recall the coin changing problem: given coins of denomination di zonks, where 1 = d1 <...

    Recall the coin changing problem: given coins of denomination di zonks, where 1 = d1 < d2 < ··· < dk, and infinitely many coins of each denomination, find the minimum number of coins needed to make exact change for n zonks. Here we ask a different problem. Each coin has a weight, the weight of each coin with denomination di being wi > 0. We want to make exact change while minimizing the total weight of the coins used....

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • Write a program that tells what coins to give out for as change from 1 cent...

    Write a program that tells what coins to give out for as change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies) only. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. Solution Demo Hello I am the coin machine! I will give you the least number of coins for your change....

  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT