Click here to Skip to main content
14,690,616 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Help me - C++ Pin
Member 798912216-Jun-20 6:36
MemberMember 798912216-Jun-20 6:36 
GeneralRe: Help me - C++ Pin
Richard MacCutchan16-Jun-20 7:06
mveRichard MacCutchan16-Jun-20 7:06 
QuestionVisual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
charlieg11-Jun-20 13:44
Membercharlieg11-Jun-20 13:44 
AnswerRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Richard MacCutchan11-Jun-20 22:32
mveRichard MacCutchan11-Jun-20 22:32 
AnswerRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Mircea Neacsu12-Jun-20 3:24
MemberMircea Neacsu12-Jun-20 3:24 
GeneralRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
charlieg12-Jun-20 9:27
Membercharlieg12-Jun-20 9:27 
GeneralRe: Visual Studio and Windows SDKs - need some experienced comments about SDK usage and RTLs Pin
Mircea Neacsu12-Jun-20 9:59
MemberMircea Neacsu12-Jun-20 9:59 
QuestionRecursion to normal way Pin
iNoor728-Jun-20 10:28
MemberiNoor728-Jun-20 10: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 10:47
Memberjeron18-Jun-20 10:47 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 12:00
MemberiNoor728-Jun-20 12:00 
GeneralRe: Recursion to normal way Pin
David Crow9-Jun-20 4:15
MemberDavid Crow9-Jun-20 4:15 
GeneralRe: Recursion to normal way Pin
harold aptroot8-Jun-20 11:30
Memberharold aptroot8-Jun-20 11:30 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 12:01
MemberiNoor728-Jun-20 12:01 
GeneralRe: Recursion to normal way Pin
harold aptroot8-Jun-20 12:11
Memberharold aptroot8-Jun-20 12:11 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 12:39
MemberiNoor728-Jun-20 12:39 
GeneralRe: Recursion to normal way Pin
Dave Kreskowiak8-Jun-20 17:44
mveDave Kreskowiak8-Jun-20 17:44 
AnswerRe: Recursion to normal way Pin
Member 79891228-Jun-20 11:38
MemberMember 79891228-Jun-20 11:38 
GeneralRe: Recursion to normal way Pin
iNoor728-Jun-20 12:03
MemberiNoor728-Jun-20 12:03 
GeneralRe: Recursion to normal way Pin
Richard MacCutchan8-Jun-20 22:33
mveRichard MacCutchan8-Jun-20 22:33 
GeneralExercise Pin
Member 79891228-Jun-20 23:31
MemberMember 79891228-Jun-20 23:31 
GeneralRe: Exercise Pin
Richard MacCutchan8-Jun-20 23:34
mveRichard MacCutchan8-Jun-20 23:34 
JokeRe: Exercise Pin
Peter_in_27808-Jun-20 23:43
professionalPeter_in_27808-Jun-20 23:43 
GeneralRe: Exercise Pin
Member 79891229-Jun-20 3:50
MemberMember 79891229-Jun-20 3:50 
GeneralRe: Exercise Pin
Richard MacCutchan9-Jun-20 4:39
mveRichard MacCutchan9-Jun-20 4:39 
GeneralRe: Exercise Pin
Member 79891229-Jun-20 4:59
MemberMember 79891229-Jun-20 4: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.