Click here to Skip to main content
15,895,667 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Help me - C++ Pin
kalberts16-Jun-20 5:36
kalberts16-Jun-20 5:36 
GeneralRe: Help me - C++ Pin
Richard MacCutchan16-Jun-20 6:06
mveRichard MacCutchan16-Jun-20 6:06 
QuestionVisual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
charlieg11-Jun-20 12:44
charlieg11-Jun-20 12:44 
AnswerRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Richard MacCutchan11-Jun-20 21:32
mveRichard MacCutchan11-Jun-20 21:32 
AnswerRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Mircea Neacsu12-Jun-20 2:24
Mircea Neacsu12-Jun-20 2:24 
GeneralRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
charlieg12-Jun-20 8:27
charlieg12-Jun-20 8:27 
GeneralRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Mircea Neacsu12-Jun-20 8:59
Mircea Neacsu12-Jun-20 8:59 
QuestionRecursion to normal way Pin
iNoor728-Jun-20 9:28
iNoor728-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 <iostream>
#include <climits>
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<n; i++) {
        cout<<"Enter the value of item number "<< i <<"\n";
        cin>>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 Big Grin | :-D
AnswerRe: Recursion to normal way Pin
jeron18-Jun-20 9:47
jeron18-Jun-20 9:47 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 11:00
iNoor728-Jun-20 11:00 
GeneralRe: Recursion to normal way Pin
David Crow9-Jun-20 3:15
David Crow9-Jun-20 3:15 
GeneralRe: Recursion to normal way Pin
harold aptroot8-Jun-20 10:30
harold aptroot8-Jun-20 10:30 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 11:01
iNoor728-Jun-20 11:01 
GeneralRe: Recursion to normal way Pin
harold aptroot8-Jun-20 11:11
harold aptroot8-Jun-20 11:11 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 11:39
iNoor728-Jun-20 11:39 
GeneralRe: Recursion to normal way Pin
Dave Kreskowiak8-Jun-20 16:44
mveDave Kreskowiak8-Jun-20 16:44 
AnswerRe: Recursion to normal way Pin
kalberts8-Jun-20 10:38
kalberts8-Jun-20 10:38 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 11:03
iNoor728-Jun-20 11:03 
GeneralRe: Recursion to normal way Pin
Richard MacCutchan8-Jun-20 21:33
mveRichard MacCutchan8-Jun-20 21:33 
GeneralExercise Pin
kalberts8-Jun-20 22:31
kalberts8-Jun-20 22:31 
GeneralRe: Exercise Pin
Richard MacCutchan8-Jun-20 22:34
mveRichard MacCutchan8-Jun-20 22:34 
JokeRe: Exercise Pin
Peter_in_27808-Jun-20 22:43
professionalPeter_in_27808-Jun-20 22:43 
GeneralRe: Exercise Pin
kalberts9-Jun-20 2:50
kalberts9-Jun-20 2:50 
GeneralRe: Exercise Pin
Richard MacCutchan9-Jun-20 3:39
mveRichard MacCutchan9-Jun-20 3:39 
GeneralRe: Exercise Pin
kalberts9-Jun-20 3:59
kalberts9-Jun-20 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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