Solving Knapsack using recursive algorithm - c++

So, I am trying to implement this algorithm from our textbook.
I wrote this :
// Knapsack_memoryfunc.cpp : Defines the entry point for the console application.
//Solving Knapsack problem using dynamic programmig and Memory function
#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
// ---CONCERNED FUNCTION-----
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = fmax(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
return table[i][j];
}
// --------------------------
void items_picked(int n, int max_wt)
{
cout << "\n Items picked : " << endl;
while (n > 0)
{
if (table[n][max_wt] == table[n - 1][max_wt]) // if value doesnot change in table column-wise, item isn't selected
n--; // n-- goes to next item
else // if it changes, it is selected
{
cout << " Item " << n << endl;
max_wt -= wt[n]; // removing weight from total available (max_wt)
n--; // next item
}
}
}
int main()
{
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
cout << " Optimum value : " << MNSack(n, max_wt);
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
items_picked(n, max_wt);
return 0;
}
Here is the question and output :
It seems like its correct on some places like optimum value, yet isn't fully acceptable.
I've tried to debug it, but its quite hard with recursive functions. Can someone please help?

int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
{
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = max(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
}
return table[i][j];
}
The problem comes in here. When your table item is greater or equal to 0, you will skip the recursion but still set the table item to 0, which won't be right if your table item is greater than 0.
You only need to update the table item when it needs to be change, so put it in the braces will correct this.

The bottom up solution.
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
// Initialization
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
// In practice, this can be skipped in a bottom up solution
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= max_wt; j++)
{
if (j < wt[i])
table[i][j] = table[i - 1][j];
else
table[i][j] = max(table[i - 1][j], val[i] + table[i - 1][j - wt[i]]);
}
}
cout << " Optimum value : " << table[n][max_wt] << endl;
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
return 0;
}
You can see that this changes the recursion to a loop, and therefore avoids the global variables. It also makes the code simpler, so that you can avoid checking if the table item is valid (equal to -1 in your example).
The drawback of this solution is, it always traverses all the possible nodes. But it gains better coefficient per item because the recursion and double checking the table item costs more. Both top-down and bottom-up have the same order of complexity O(n^2), and it's hard to tell which one is faster.

Related

Filtering values from an array and assigning them in different arrays C++

I am trying to filter an array given by the user on basis of whether it is positive even, positive odd, negative even, or negative odd.
And, based on this filtration, I am trying to put them in the respected array but my code is working for the 1st part; i.e. it is taking user array but the problem is: It is not entering my filtration code.
The code is here:
#include<iostream>
#include<limits>
#include <functional>
using namespace std;
int main()
{
int n ;
cout<<"\n Enter the size of array :-";
cin>>n;
int numbers[n];
int peven[n],podd[n],neven[n],nodd[n];
for (int i = 0; i < n ; i++){
cout<<"\n Enter value "<<i+1<<" = ";
cin>>numbers[i];
}
cout<<"\n\n";
for (int i = 0; i < n ; i++){
cout<<" "<<numbers[i];
}
cout<<"\n\n";
for(int j = 0;j<n;j++){
if ( ((numbers[j]%2) == 0) && (numbers[j] > 0) ) {
for(int i = 0; i<1; i--){
cin>>peven[i];
i++;
}
}
else if ( ((numbers[j]%2) == 0) && (numbers[j] < 0) ){
for(int i = 0; i<1; i--){
cin>>neven[i];
i++;
}
}
else if ( ((numbers[j]%2) != 0) && (numbers[j] > 0) ){
for(int i = 0; i<1; i--){
cin>>podd[i];
i++;
}
}
else {
for(int i = 0; i<1; i--){
cin>>nodd[i];
i++;
}
}
}
cout<<"\n The +ve even number array is :- "<<peven[n];
cout<<"\n The +ve odd number array is :- "<<podd[n];
cout<<"\n The -ve even number array is :- "<<neven[n];
cout<<"\n The -ve odd number array is :- "<<nodd[n];
return 0;
}
There are several problems in your code. Here is a list (which is not exhaustive) :
it is forbidden to create a constant size array with an integer which is not constant :
cin>>n;
int numbers[n];
As Sam Varshavchik mentioned, it is not possible to finish this loop :
for(int i = 0; i<1; i--)
You have no reason to read value from cin after you fill the array to filter. Line like this one should be modified :
cin>>peven[i];
Here is a correction :
#include <iostream>
#include <vector>
#include<limits>
#include <functional>
using namespace std;
int main()
{
vector<int> peven, podd, neven, nodd;
int n;
cout << "Enter the size of vector :-" << endl;
cin >> n;
vector<int> numbers(n, 0);
for (int i = 0; i < n; i++) {
cout << "Enter value " << i + 1 << endl;
cin >> numbers[i];
}
cout << endl;
cout << "Your vector contains : [";
for (int i = 0; i < n; i++) {
cout << " " << numbers[i];
}
cout << " ]" << endl;
cout << endl;
for (int j = 0; j < n; j++) {
if (((numbers[j] % 2) == 0) && (numbers[j] >= 0)) {
peven.push_back(numbers[j]);
}
else if (((numbers[j] % 2) == 0) && (numbers[j] < 0)) {
neven.push_back(numbers[j]);
}
else if (((numbers[j] % 2) != 0) && (numbers[j] >= 0)) {
podd.push_back(numbers[j]);
}
else {
nodd.push_back(numbers[j]);
}
}
cout << "\n The +ve even number array is : [";
for (int j = 0; j < (int)peven.size(); j++) {
cout << " " << peven[j];
}
cout << "] " << endl;
cout << "\n The +ve odd number array is : [";
for (int j = 0; j < (int)podd.size(); j++) {
cout << " " << podd[j];
}
cout << "] " << endl;
cout << "\n The -ve even number array is : [";
for (int j = 0; j < (int)neven.size(); j++) {
cout << " " << neven[j];
}
cout << "] " << endl;
cout << "\n The -ve odd number array is : [";
for (int j = 0; j < (int)nodd.size(); j++) {
cout << " " << nodd[j];
}
cout << "] " << endl;
return 0;
}

Give n. Display a triangle with the side of n stars empty inside

This is my code, but i don't understand how can i print only 5 stars on last row. I tried with debugger i know the problem, but i don't know how to fix it.
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= 2 * n; j++)
{
if (i + j == 6 || j - i == 4)
cout << "*";
else cout << " ";
if (i== n && j % 2 !=0)
{
cout << "*";
}
else cout << " ";
}
cout << endl;
}
}
This is the output.
*
* *
* *
* *
* * * * *
You can do as,
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
cout << "* ";
if (i == n) {
for (int j = 1; j <= n - 2; j++) {
cout << "* ";
}
} else {
for (int j = 1; j <= 2 * i - 4; j++) {
cout << " ";
}
}
if (i != 1) cout << "*";
cout << endl;
}

An exception just caused my code to stop debug, and showing on a simple statement

Please, can anyone tell me whats wrong in this code.
I have no idea about these kind of exceptions.
Compiler is just showing this -
Exception thrown: read access violation. a was 0x1D54112. occurred
Is this due to an if statement is inside a loop, which is also inside another loop ??
Here is the code -
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the number of walkers - ";
cin >> n;
int* a = new int[n];
cout << endl << "Enter the distances of walkers from Rick : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << endl << "The distance of walkers from Rick are - ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
int c = 0, i = 0, k = 0;
while (c == 0 || i < n)
{
a[i] = 0; //exception is being shown here.
for (int j = i+1; j < n; j++)
{
a[j] -= 1; //and here too!!.
if (j % 6 == 0) a[j]--;
if (a[j] == 0)
{
c = 1;
k = j;
}
}
i++;
}
if (c == 0) cout << endl << "Rick now go and save Carl and Judas" << endl;
else if (c == 1)
{
cout << endl << "Goodbye Rick" << endl;
cout << "He was able to kill " << k << " walkers. " << endl;
}
}

How to find factors of each number in an array

I have an array of numbers input by the user, the program then sorts it in ascending order. I just need to find a way to get the factors of each number in the array and have it be printed out
#include "stdafx.h"
#include <iostream>
#include <limits>
#define MAX 200
using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;
int main()
{
//array declaration
int arr[MAX];
int n, i, j;
int temp;
//read total number of elements to read
cout << "Enter total number of numbers to read: ";
cin >> n;
//check bound
if (n<0 || n>MAX)
{
cout << "Input valid range!!!" << endl;
return -1;
}
//read n elements
for (i = 0; i < n; i++)
{
cout << "Enter element [" << i + 1 << "] ";
cin >> arr[i];
cout << endl;
}
//print input elements
cout << "Unsorted Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
//sorting - ASCENDING ORDER
for (i = 0; i<n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//print sorted array elements
cout << endl;
cout << "Sorted (Ascending Order) Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl <<endl;
//trying to find factors
cout << "Factors of " << arr[i] << " are: " << endl;
for (k = 1; k <= arr[i]; ++i)
{
if (arr[i] % k == 0)
cout << k << endl;
}
system ("pause")
return 0;
}
I want it to print each number from the array with
"The factors of (number) are ...'
"The factors of (next number) are ..."
and so on
The final for-loop should be loop with k and you forgot to increment k.
You should also write i-loop:
//trying to find factors
for (i = 0; i < n; i++)
{
cout << "Factors of " << arr[i] << " are: " << endl;
for (k = 1; k <= arr[i]; ++k)
{
if (arr[i] % k == 0)
cout << k << endl;
}
}
In addition, as pointed out by #LocTran, the upper bound of outer loop should be n-1.
Alternatively, you can easily sort arr using std::sort as follows:
std::sort(arr, arr+n);
Then your code would well work for you:
Live Demo
There are some issues with your source code.
1> Sorting problem with for outer loop
//sorting - ASCENDING ORDER
for (i = 0; i < (n-1); i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
The upper bound of outer loop should be (n-1), not n as following but maybe you're lucky you won't see the problem when n < MAX. In case of n == MAX you will see the problem
//sorting - ASCENDING ORDER
//for (i = 0; i < n; i++)
for (i = 0; i < (n-1); i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
2> The print functionality for entire array, you should add the outer loop for index of your array, and change the i++ by k++ in your loop as well
//trying to find factors
cout << "Factors of " << arr[i] << " are: " << endl;
for (k = 1; k <= arr[i]; ++i)
{
if (arr[i] % k == 0)
cout << k << endl;
}
should be replaced by
//trying to find factors
for (i = 0; i < n; i++)
{
cout << "Factors of " << arr[i] << " are: " << endl;
//for (k = 1; k <= arr[i]; ++i)
for (k = 1; k <= arr[i]; ++k)
{
if (arr[i] % k == 0)
cout << k << endl;
}
}
Here is my solution based on modified source code
#include <iostream>
#include <limits>
#define MAX 200
using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;
int main()
{
//array declaration
int arr[MAX];
int n, i, j;
int temp;
//read total number of elements to read
cout << "Enter total number of numbers to read: ";
cin >> n;
//check bound
//if (n<0 || n>MAX)
if (n<0 || n>MAX)
{
cout << "Input valid range!!!" << endl;
return -1;
}
//read n elements
for (i = 0; i < n; i++)
{
cout << "Enter element [" << i + 1 << "] ";
cin >> arr[i];
cout << endl;
}
//print input elements
cout << "Unsorted Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
//sorting - ASCENDING ORDER
//for (i = 0; i < n; i++)
for (i = 0; i < (n-1); i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//print sorted array elements
cout << endl;
cout << "Sorted (Ascending Order) Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl << endl;
//trying to find factors
for (i = 0; i < n; i++)
{
cout << "Factors of " << arr[i] << " are: " << endl;
//for (k = 1; k <= arr[i]; ++i)
for (k = 1; k <= arr[i]; ++k)
{
if (arr[i] % k == 0)
cout << k << endl;
}
}
return 0;
}

Difference of 2 sets retained in arrays - C++

Consider two sets retained in two arrays. Find the union, intersection and difference (relative complement) of the two sets.
I managed to solve the union and the intersection, but the difference is giving me a hard time. Any hints? And if possible, keep it as simple as possible, without functions or more complex aspects, because I'm a beginner and I still have a lot to learn.
Thank you in advance!
#include <iostream>
using namespace std;
int main()
{
int v1[100], v2[100], u[200], intersection[100], d[100];
unsigned int v1_length, v2_length, i, j, OK = 0, union_length;
cout << "Enter the number of elements of the first array:" << " ";
cin >> v1_length;
cout << "Enter the elements of the first array:" << '\n';
for (i = 0; i < v1_length; i++)
cin >> v1[i];
cout << "Enter the number of elements of the second array:" << " ";
cin >> v2_length;
cout << "Enter the elements of the second array:" << '\n';
for (i = 0; i < v2_length; i++)
cin >> v2[i];
//Union
union_length = v1_length;
for (i = 0; i < v1_length; i++)
u[i] = v1[i];
for (i = 0; i < v2_length; i++)
{
int ok = 0;
for (j = 0; !ok && j < v1_length; j++)
if (v1[j] == v2[i])
ok = 1;
if (!ok)
{
u[union_length] = v2[i];
union_length++;
}
}
cout << "The union of the two sets contained in the arrays is: ";
for (i = 0; i < union_length; i++)
cout << u[i] << " ";
cout << '\n';
//Intersection
unsigned int k = 0;
cout << "The intersection of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++)
if (v1[i] == v2[j])
{
intersection[k] = v1[i];
k++;
}
for (i = 0; i < k; i++)
cout << intersection[i] << " ";
cout << '\n';
//Difference
unsigned int l = 0, OK2 = 0;
cout << "The difference of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
{
for (j = 0; j < v2_length; j++)
{
if (v1[i] == v2[j])
OK2 = 1;
if (!OK2)
{
d[l] = v1[i];
l++;
}
}
}
for (i = 0; i < l; i++)
cout << d[i] << " ";
cout << '\n';
return 0;
}
It seems that the intersection is the best place to start. You want the items that only in appear in one of the two arrays, right?
So, for the inner loop, you need to compare all the elements. Then, if no match was found, you have the a unique element.
You need to add the curly braces {} to the for loop. I know that curly braces are distracting at times, but over time, you will probably find it safer to almost always include them to avoid confusion.
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++) {
if (v1[i] == v2[j]){
break; // this item is not unique
} else if(j == v2_length - 1){
d[l] = v1[i]; // This is the unique one, add it to the answer array
l++;
}
}
for (i = 0; i < l; i++)
cout << intersection[l] << " ";
cout << '\n';
You're on the right track!
You're doing a few things wrong. Here are some fixes you can try:
Only set OK2 to 0 once per inner-loop
Reset OK2 to 0 at the end of the inner-loop
Only do the insertion into d after the inner-loop has completed
As an optimization, consider breaking after you set OK2 to 1, as you know at that point it can never be set to 0 for the current value pointed to by the outer-loop.