Algorithm producing wrong output

Can anyone tell me why my calculate function is producing a zero?I feel like it may have something to do with there being zeros the arrays it takes, but i can't figure it out. Any help would be awesome! The arbitrary cout statements are just for my debugging purposes

Input is formatted as follows:

<int>
<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
and so on

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
#include <iostream>
#include <string>
#include <sstream>

int** parse_input(int &num_items, int &pouch_size);

int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value);

int generate_output(int &max_value, int* output);


int main()
{
	int num_items;
	int pouch_size;
	int max_value =;
	//int* new_array = new int(2);
	int** new_array = parse_input(num_items, pouch_size);
	std::cout << "new_array[1][1]  " << new_array[1][1] <<std::endl;
	int* output = calculate(num_items, pouch_size, new_array[0], new_array[1], max_value);
	generate_output(max_value, output);

	return 0;
}

int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value)
{
	std::cout << "values 1  " << values[1] << std::endl;
	int **M = new int *[num_items + 1];
	for (int p = 0; p < (num_items + 1); ++p)
	{
		M[p] = new int[pouch_size + 1];
	}

	int **B = new int *[num_items + 1];
	for (int p = 0; p < (num_items + 1); ++p)
	{
		B[p] = new int[pouch_size + 1];
	}

	for (int i = 1; i < num_items; i++)
	{
		for (int j = 0; j < pouch_size; j++)
		{
			if (weights[i] <= j)
			{
		
				if ((M[i-1][j-weights[i]] + values[i]) > M[i-1][j])
				{
					M[i][j] = M[i-1][j-weights[i]] + values[i];
					//std::cout << M[i][j] << " = M[i][j]" << std::endl;
					B[i][j] = i;
				}

				else
				{

					M[i][j] = M[i-1][j];
					B[i][j] = 0;
				}
			}

			else
			{

				M[i][j] = M[i-1][j];
				std::cout << "i, j =  " << i << " " << j << std::endl;
				std::cout << "M[i][j] = " << M[i][j] << std::endl;
				B[i][j] = 0;	
			}	
		}
	}	
	std:: cout << "M[1][2]  " << M[0][5] << std::endl; 
	int r = num_items;
	int s = pouch_size;
	int *output = new int(num_items + 1);
	int out_index = 1;
	while (r > 0 && s > 0)
	{
		if (B[r][s] == 0)
		{
			r--;
		}

		else
		{
			output[out_index] = B[r][s];
			out_index++;
			s = s - weights[r];
			r--;
		}

	}
	std::cout << "num items  " << num_items << std::endl; //pouch_size] << std::endl;
	std::cout << pouch_size <<std::endl;
	max_value = M[num_items][pouch_size];
	//std::cout << max_value << std::endl;
	delete[] M;
	delete[] B;
	return output;
}

int** parse_input(int &num_items, int &pouch_size)
{
	std::string x;
	std::string y;
	std::getline(std::cin, x);
	std::getline(std::cin, y);
	pouch_size = std::stoi(x);
	num_items = std::stoi(y);
	// std::cout << num_items << ' ' << pouch_size << std::endl;
	int *values = new int[(num_items + 1)];
	int *weights = new int[(num_items + 1)];
	// std::cout << "I created weights and values!" << std::endl;
	int j = 1;
	int i = 0;
	for (std::string line; std::getline(std::cin, line); )
	{
		i = 0;
		int place = 1;
		//std::cout << line << " is the 3rd line" << std::endl;
		std::stringstream info(line);
		int *A = new int[2];// = { NULL };
		std::string B;
		//std::cout << "I created an array for each line" << std::endl;
		for (std::string key; std::getline(info, key, ','); )
		{
			// std::cout << key << std::endl;
			if (place == 1)
			{
				B = key;
				place += 1;
			}
			else
			{
				//std::cout << key << std::endl;
				A[i] = std::stoi(key);
				i += 1;
				place += 1;
			}
			
		}
		values[j] = A[0];
		weights[j] = A[1];
		j++;
		delete [] A;
	}	
	int **new_array = new int*[2];
	for (int w = 0; w < 2; ++w)
	{
		new_array[w] = new int[(num_items + 1)];
	}
	for (int q = 0; q < (num_items + 1); q++)
	{
		new_array[0][q] = weights[q];
		
		new_array[1][q] = values[q];
	} 
	// std::cout << "I created new_array " << std::endl;
	//new_array[0] = weights;
	//new_array[1] = values;
	delete [] values;
	delete [] weights;
	std::cout << "new array 1  " << new_array[0][1] << std::endl;
	return new_array;
}	
		
int generate_output(int &max_value, int* output) //, std::string output[])
{
	std::cout << max_value << std::endl;
	
	 return 0;
}	
		
		
	
Last edited on
You are using uninitialized data.
M[p] = new int[pouch_size + 1]; just allocates memory, it does not fill it with any particular value.


Line 76: int *output = new int(num_items + 1);
note, parenthesis()


> why my calculate function is producing a zero?
¿what is it supposed to do?
I don't want to go through a lot of obfuscated code trying to figure out what was your intention


> Input is formatted as follows:
¿do they hold an special meaning? ¿may I put negative values? ¿how many lines?
42
54
foo,-1,13
Topic archived. No new replies allowed.