Home / Essays / Coin Change brute force array

Coin Change brute force array

Coin Change brute force array

You run the program on the command line and indicate the file where the avalable coins and desired change is stored. I am attaching file- test.txt

[1, 2, 5] 10
[1, 3, 7, 26] 22

Â

the program reads array of coins one by one and then it needs to output minimum number of coins needed to make that change and also print an array of coins used to make the change.

[0, 0, 2] 2
[1, 0, 3, 0] 4

Â

Right now, I am outputing everything to the screen and as you can see coins used are not displayed. IT HAS TO BE BRUTE FORCE

FOR SOME REASON, I WAS NOT ABLE TO ATTACHED .CCP FILE, SO CODE IS BELOW: #include #include #include #include #include #include #include #include //#include #include #include #include #include #include #include #include // std::cout #include // std::min using std::cout; using std::cin; using std::endl; using std::string; using std::vector; using std::stringstream; using std::ifstream; using std::ofstream; //http://romanenco.com/coin-change-problem/ int bruteforceCoin(vector &coins, int sum, vector &array_Coin_Count) { int coin; vector array_Minimum_Coins; vector optimal_Set_of_Coins; //ofstream output; //vector array_Coin_Count; //copy vector of avaibale coins to a new vector that will go to the output file //and assign each element to 0 — meaning 0 # of coins used at the begining if (sum == 0) { return 0; // base case } int minimum = INT_MAX; for (int m = 0; m < coins.size(); m++) { if (coins[m]<= sum){ int minimum_old = minimum; int minimum_new = bruteforceCoin(coins, sum – coins[m],array_Coin_Count) +1; if (minimum_new < minimum_old){ //array_Coin_Count.push_back([m]); minimum = minimum_new; //coin = m; } //minimum = std::min(minimum_old, minimum_new); //cout << minimum; } // array_Minimum_Coins.push_back(minimum); //optimal_Set_of_Coins.push_back(coin); } //fill coin array array_Coin_Count with coin values // while (sum > 0){ //increments coin position for current change value // array_Coin_Count[optimal_Set_of_Coins[sum]]++; //decrease change by the coin value //sum -= coins[optimal_Set_of_Coins[sum]]; //} // /* cout << “[“; for (int i = 0; i < (int)array_Minimum_Coins(); i++){ cout << array_Minimum_Coins[i]; if (i != (int)array_Minimum_Coins.size() – 1) cout<< “,”; } cout << “]” << endl; cout < array_Coin_Count; string input_file; string output_file; string line; vector array_Available_Coins; vector array_Coin_Count; //User runs the programs on the command-line, specifying a file ([input filename].txt) //in which the first line contains the array V, formatted as [1, 5, 10, 25], denominations in increasing order. if (argc == 2){ // the user should specify 2 parameters to run th program input_file = string(argv[1]);//name of the input file //output file—–[input filename]change.txt where [input filename].txt is the input filename output_file = string(argv[1]); output_file.insert(input_file.find(“.txt”),”change”); } else{ cout << “No file specified.” << endl; exit (EXIT_FAILURE); } //tokanize the input ifstream input(input_file.c_str()); ofstream output(output_file.c_str()); int counter = 0; while (getline(input, line)){ ++counter; //get array of coins // getline(input, line); stringstream ss(line); int entry; int change; if (ss.peek() == ‘[‘) ss.ignore(); if (counter % 2==1){ while (ss >> entry){ array_Available_Coins.push_back(entry); if (ss.peek() == ‘,’) ss.ignore(); } for (int i = 0; i < (int)array_Available_Coins.size(); i++) array_Coin_Count.push_back(0); } else{ ss >> change; //greedy(array_Available_Coins, change, output); int countmin = bruteforceCoin(array_Available_Coins, change,array_Coin_Count); cout << countmin; output << countmin; //print result to the output file //possibly we can place this code into a function because it will be reused //output << “[“; /* for (int i = 0; i < (int)array_Coin_Count.size(); i++){ output << array_Coin_Count[i]; if (i != (int)array_Coin_Count.size() – 1) output << “,”; } */ //output << “]” << endl; //output <
FOR YOUR ASSIGNMENTS TO BE DONE AT A CHEAPER PRICE PLACE THIS ORDER OR A SIMILAR ORDER WITH US NOW :)

Leave a Reply

WPMessenger