I am having an issue with my coin change program. The idea behind the program is to take input from a text file which will be coin values and an amount and fine the lowest number of coins of the given domination to make that amount. For example:
1 2 5
10
1 3 7 12
29
This would return the following in a text file:
1 2 5
10
0 0 2
2
1 3 7 12
29
0 1 2 1
4
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <array>
usingnamespace std;
//function to convert string to int vector
vector<int> list_from_string(string len) {
string x = "";
vector<int> vec;
for (int i = 0; i <= len.length(); i++) {
if (i == len.length() || len[i] == ' ') {
//stoi function used to convert string to int
vec.push_back(stoi(x));
x = "";
}
else {
//appending chars to string
x = x + len[i];
}
}
return vec;
}
//function to calculate change from denom and coins
vector<int> calculate(vector<int> denom, int coins) {
//creating a 2d bool array
bool arr[coins + 1][denom.size() + 1]; //issue here
//setting all values to false
for (int i = 0; i <= coins; i++) {
for (int j = 0; j <= denom.size(); j++) {
arr[i][j] = false;
}
}
//setting values with 0 coins to true
for (int i = 0; i <= denom.size(); i++) {
arr[0][i] = true;
}
//filling array using dynamic programming
for (int i = 1; i <= coins; i++) {
for (int j = 1; j <= denom.size(); j++) {
if (i >= denom[j - 1]) {
arr[i][j] = arr[i][j] || arr[i - denom[j - 1]][j] || arr[i - denom[j - 1]][j - 1];
}
}
}
int x = coins; int y = denom.size();
//creating resulting vector
vector<int> ans;
for (int i = 0; i<denom.size(); i++) { ans.push_back(0); }
//filling vector using backtracking
while (x>0 && y>0) {
if (arr[x - denom[y - 1]][y]) {
ans[y - 1]++;
x = x - denom[y - 1];
}
elseif (arr[x - denom[y - 1]][y - 1]) {
ans[y - 1]++;
x = x - denom[y - 1];
y--;
}
else {
y--;
}
}
//returning the vector
return ans;
}
int main() {
//ifstream object to read amount.txt to take as input
ifstream infile("amount.txt");
string str;
//denom vector to store denominators in a vector
vector<int> denom;
//a flag variable to read denominators first then coins second.
int flag = 0;
//ofstream object to write output to change.txt
ofstream myfile;
myfile.open("change.txt");
//while loop to read lines of amount.txt
while (getline(infile, str)) {
//if flag is 0 then denominators are read
//else if flag is 1 then no of coins are read
if (flag == 0) {
//function to convert string to int vector
denom = list_from_string(str);
//writing vector to change.txt file
for (int i = 0; i<denom.size(); i++) {
myfile << denom[i] << " ";
}
myfile << "\n";
//setting flag to 1 to read no of coins
flag = 1;
}
else {
//stoi is used to convert string to integer
int coins = stoi(str);
//writing coins to change.txt file
myfile << coins << "\n";
//function to convert vector and coins to change vector
vector<int> change = calculate(denom, coins);
//writing change vector to change.txt file and calculating no of coins
int no = 0;
for (int i = 0; i<change.size(); i++) {
myfile << change[i] << " ";
no = no + change[i];
}
//writing no of coins to change.txt
myfile << "\n";
myfile << no << "\n";
//setting flag to 0 again
flag = 0;
}
}
return 0;
}
When I go to try and make a 2D array of bools using this: bool arr[coins + 1][denom.size() + 1], my IDE is causing an error to be thrown. Any advice or guidance would be appreciated.