14,605,229 members

# C / C++ / MFC

 Re: Help me - C++ Member 798912216-Jun-20 5:36 Member 7989122 16-Jun-20 5:36
 Re: Help me - C++ Richard MacCutchan16-Jun-20 6:06 Richard MacCutchan 16-Jun-20 6:06
 Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs charlieg11-Jun-20 12:44 charlieg 11-Jun-20 12:44
 Re: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Richard MacCutchan11-Jun-20 21:32 Richard MacCutchan 11-Jun-20 21:32
 Re: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Mircea Neacsu12-Jun-20 2:24 Mircea Neacsu 12-Jun-20 2:24
 Re: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs charlieg12-Jun-20 8:27 charlieg 12-Jun-20 8:27
 Re: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Mircea Neacsu12-Jun-20 8:59 Mircea Neacsu 12-Jun-20 8:59
 Recursion to normal way iNoor728-Jun-20 9:28 iNoor72 8-Jun-20 9:28
 Hello guys ! I just need a little help with this code, it's a recursion method to solve the 0-1 knapsack problem, but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code: ```// 0-1 knapsack problem using Divide & Conquer #include #include using namespace std; // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) // Knapsack capacity W int knapSack(int v[], int w[], int n, int W) { // Stopping conditions // Condition 1: Negative capacity if (W < 0) return INT_MIN; // Condition 2: No items left or capacity becomes 0 if (n < 0 || W == 0) return 0; // Cases of solving // Case 1: Take an item, use recursion for capacity (W - item's weight) and (No. of items - 1) int include = v[n] + knapSack(v, w, n - 1, W - w[n]); // Case 2. Take out the current item n from the knapsack. Use recursion for the remaining items (n - 1) int exclude = knapSack(v, w, n - 1, W); // return maximum value we get by including or excluding current item return max (include, exclude); } int main() { // Input: set of items each with a weight and a value // Change it to any numbers you wish int n; cout<<"Enter number of items:"; cin>n; for (int i=0; i>v[i]; cout<<"Enter the weight of item number "<< i <<"\n"; cin>>w[i]; } // Knapsack capacity, put it as you wish int W; cout<<"Enter the capacity of knapsack"<<"\n"; cin>>W; cout << "Knapsack value is " << knapSack(v, w, n - 1, W); return 0; }``` And I really appreciate all of your help
 Re: Recursion to normal way jeron18-Jun-20 9:47 jeron1 8-Jun-20 9:47
 Re: Recursion to normal way iNoor728-Jun-20 11:00 iNoor72 8-Jun-20 11:00
 Re: Recursion to normal way David Crow9-Jun-20 3:15 David Crow 9-Jun-20 3:15
 Message Closed 9-Jun-20 21:21 Anujjndal01 9-Jun-20 21:21
 Re: Recursion to normal way harold aptroot8-Jun-20 10:30 harold aptroot 8-Jun-20 10:30
 Re: Recursion to normal way iNoor728-Jun-20 11:01 iNoor72 8-Jun-20 11:01
 Re: Recursion to normal way harold aptroot8-Jun-20 11:11 harold aptroot 8-Jun-20 11:11
 Re: Recursion to normal way iNoor728-Jun-20 11:39 iNoor72 8-Jun-20 11:39
 Re: Recursion to normal way Dave Kreskowiak8-Jun-20 16:44 Dave Kreskowiak 8-Jun-20 16:44
 Re: Recursion to normal way Member 79891228-Jun-20 10:38 Member 7989122 8-Jun-20 10:38
 Re: Recursion to normal way iNoor728-Jun-20 11:03 iNoor72 8-Jun-20 11:03
 Re: Recursion to normal way Richard MacCutchan8-Jun-20 21:33 Richard MacCutchan 8-Jun-20 21:33
 Exercise Member 79891228-Jun-20 22:31 Member 7989122 8-Jun-20 22:31
 Re: Exercise Richard MacCutchan8-Jun-20 22:34 Richard MacCutchan 8-Jun-20 22:34
 Re: Exercise Peter_in_27808-Jun-20 22:43 Peter_in_2780 8-Jun-20 22:43
 Re: Exercise Member 79891229-Jun-20 2:50 Member 7989122 9-Jun-20 2:50
 Re: Exercise Richard MacCutchan9-Jun-20 3:39 Richard MacCutchan 9-Jun-20 3:39
 Last Visit: 11-Aug-20 3:19     Last Update: 11-Aug-20 3:19 Refresh ᐊ Prev1234567891011 Next ᐅ

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.