I have the following java code for making change using the greedy method int makeChangeGreedy(int amount, int[] coins) { if(amount==0) { //check if there is
I have the following java code for making change using the greedy method int makeChangeGreedy(int amount, int[] coins) { if(amount==0) { //check if there is no remaining change System.out.printIn(""); return 0; }
for(int i=coins.length; i>0; i--){ int coin = coins[i-1]; //if next largest coin is found, display its value if(amount>=coin){ System.out.print(coin+""); return 1 + makeChangeGreedy(amount-coin, coins); } } System.out.print("Cannot make change. "); System.out.printIn("Cents remaining: " + amount); return 0; } I need to implement similar code but in C. My program needs to also be able to read a text file "data.txt". Each line in in the text contains three values, c, k and n (in this specific order, ex. 3 4 38 where c=3,k=4, and n=38). That is, the set of denominations is {30; 31; 32; 33; 34} = {1; 3; 9; 27; 81} and we would like to make change for n = 38. The file "data.txt" may include multiple lines like above.
Furthermore, the program needs to write to a file called change.txt
Implement in C
The output will be written to a file called "change.txt", where the output corresponding to each input line contains a few lines. Each line has two numbers, where the first number denotes a de- nomination and the second number represents the cardinality of that denomination in the solution. For example, for the above input line '3 4 38', the optimal solution is the multiset {27, 9, 1, 1), and the output in the file change.txt" is as follows: 271 91 12 which means the solution contains 1 coin of denomination 27, one coin of 9 and two coins of denomination 1. You can use a delimiter line to separate the outputs generated for different input linesStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started