Using the incomplete programming code given, complete the code using dynamic programming, to reproduce the results in the following Table 1. (C++) #include using namespace std; const int W = //***WRITE YOUR CODE HERE***//; // max knapsack capacity //==========================================================================
Question 1
Using the incomplete
(C++)
#include<iostream>
using namespace std;
const int W = //***WRITE YOUR CODE HERE***//; // max knapsack capacity
//==========================================================================
// Dynamic Programming function for solving Knapsack Problem
void Knapsack(int i, int j, int w[], int n, int v[], int F[][W]){
//==========================================================================
// i: iteration index for item numbers (i: 0~n)
// j: iteration index for weights/capacity (j: 0~max knapsack capacity W)
// w: current item's weight
// v: current item's value
// Output: Values of F(i,j)
//---------------------------------------------------------------------------
//Instruction: Write your code to calculate F(i,j). Include comments
// ***WRITE YOUR CODE HERE***
// ***WRITE YOUR CODE HERE***
// ***WRITE YOUR CODE HERE***
}
//===============
// Main Function
int main(void){
//===============
// number of items
const int n /* =? ***WRITE YOUR CODE HERE*** */
int i; // item: 1 <= i <= n
int j; // capacity: 1 <= j <= W
int weight[] = {/* ***WRITE YOUR CODE HERE*** */}; // weight of each item
int value[] = {/* ***WRITE YOUR CODE HERE*** */}; // value of each item
// Instruction: Initialize F
//F: dynamic programming table/matrix, containing values of F(i,j)
int F.. // *** WRITE YOUR CODE HERE *** (for F)
// call the dynamic programming function
Knapsack(i, j, weight, n, value, F);
// Instruction: Display your output, containing values of F(i,j)
//NOTES:
/* Output can be in the form of a TABLE, or:
F(0,0) = 0
F(0,1) = 0
F(0,2) = 0
.. (continues until the end)
*/
// *** WRITE YOUR CODE HERE ***
// *** WRITE YOUR CODE HERE ***
// *** WRITE YOUR CODE HERE ***
return 0;
}
Step by step
Solved in 2 steps
I'm still unsure how to incorporate the answer into the incomplete code. I've attached some images to help understand the situation. It would be really helpful if you could fill in the code according to as shown in the images.