Dynamic Programming Coin Change Problem

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
   #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <array>

using namespace 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];

		}
		else if (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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <fstream>
#include <string>
#include <vector>

//function to convert string to int vector
std::vector<int> list_from_string(const std::string& list);
//function to calculate change from denom and coins
std::vector<int> calculate(const std::vector<int>& denom, int amount);

int main()
{
    std::ifstream infile("amount.txt");
    bool flag { true };             // denominations first, then amount.
    std::ofstream outfile("change.txt");
    std::vector<int> denom; // denominations (must endure two iterations!)
    for (std::string str; getline(infile, str); /**/) {
        if (flag) {
            denom = list_from_string(str);
            for (std::size_t i = 0; i < denom.size(); i++) {
                outfile << denom.at(i) << ' ';
            }
            outfile << '\n';
            flag = false;   //  at next iteration read the amount!
        }
        else {
            int amount = std::stoi(str);
            outfile << amount << '\n';
            std::vector<int> change = calculate(denom, amount);
            int no = 0;
            for (std::size_t i = 0; i<change.size(); i++) {
                outfile << change[i] << ' ';
                no +=  change[i];
            }
            outfile << '\n' << no << '\n';
            denom.clear();
            flag = true;    // at next iteration read the denominations list!
        }
    }
    return 0;
}

std::vector<int> list_from_string(const std::string& list)
{
    std::string x;
    std::vector<int> vec;
    for (std::size_t i = 0; i <= list.length(); i++) {
        if (i == list.length() || list.at(i) == ' ') {
            vec.push_back(std::stoi(x));
            x.clear();
        }
        else {
            x += list.at(i);
        }
    }
    return vec;
}

std::vector<int> calculate(const std::vector<int>& denom, int coins)
{
    bool** arr = new bool*[coins + 1];
    for(int i {}; i < coins + 1; ++i) { arr[i] = new bool[denom.size() + 1] { false }; }

    //setting values with 0 coins to true
    for (std::size_t i = 0; i <= denom.size(); i++) {
        arr[0][i] = true;
    }

    //filling array using dynamic programming
    for (int i = 1; i <= coins; i++) {
        for (std::size_t 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, y = denom.size();

    //creating resulting vector
    std::vector<int> ans (denom.size());

    //filling vector using backtracking
    while (x > 0 && y > 0) {
        if (arr[x - denom[y - 1]][y]) {
            ans[y - 1]++;
            x = x - denom[y - 1];
        }
        else if (arr[x - denom[y - 1]][y - 1]) {
            ans[y - 1]++;
            x = x - denom[y - 1];
            y--;
        }
        else {
            y--;
        }
    }
    for(int i {}; i < coins + 1; ++i) { delete[] arr[i]; }
    delete[] arr;
    return ans;
}

Topic archived. No new replies allowed.