w. You can come back to it in a year, after you are familiar with all the more common ways of using C++.
So for now, I've just accepted the code in the book and come back with it later !
Today, I've just written a program to solve "Knapstack" problem, I've just wanted tried to use all the things that you taught me to practice with class and object. It run with no errors but when I enter data it doesn't show the result instead of something which quickly appeared and then disappeared and it give me the file.exe.core, could you help me with this one? I means just some suggestion and I will debug myself ^^:
I used to method here : one is dynamic programming, other is greedy. Here's all I tried :
[syntax="cpp"]
#include <iostream>
#include <string>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <algorithm> //sort function
using namespace std;
class Knapstack
{
public :
Knapstack(int number_of_items, int weight_of_bag);
~Knapstack();
//Copy constructor
Knapstack::Knapstack(Knapstack ©_object);
//overloading operator "="
const Knapstack &operator = (Knapstack &);
/*method for greedy*/
void Solving_by_greedy();
void SortItems();
/*method for dynamic programming*/
void Solving_by_dynamic_programming();
void Dynamic_programming_trace_solution();
friend istream &operator >> (istream &, Knapstack &);
private :
int weight_of_bag;
int number_of_items;
//data information
int *items_No;
int *items_value;
int *items_weight;
//solution array
float *greedy_solution;
int **dynamic_solution;
};
Knapstack::Knapstack(int number_of_items, int weight_of_bag)
:number_of_items(number_of_items), weight_of_bag(weight_of_bag)
{
greedy_solution = new float[number_of_items+1];
items_No = new int[number_of_items+1];
items_value = new int[number_of_items+1];
items_weight = new int[number_of_items+1];
int **dynamic_solution = new int *[number_of_items+1];
for(int x = 0; x <= number_of_items; x++)
{
dynamic_solution[x] = new int[weight_of_bag+1];
}
for(int i = 0; i <= number_of_items; i++)
{
*(items_No + i) = 0;
*(items_value + i) = 0;
*(items_weight + i) = 0;
*(greedy_solution + i) = 0.0;
}
for(int i = 0; i <= number_of_items; i++){
for(int j = 0; j <= weight_of_bag; j++){
dynamic_solution[i][j] = 0;
}
}
}
//Destructor
Knapstack::~Knapstack()
{
delete[] greedy_solution;
delete[] items_No;
delete[] items_value;
delete[] items_weight;
for(int x = 0; x <= number_of_items; x++){
delete[] dynamic_solution[x];
}
delete[] dynamic_solution;
}
//Copy constructor
Knapstack::Knapstack(Knapstack ©_object)
:number_of_items(copy_object.number_of_items), weight_of_bag(copy_object.weight_of_bag)
{
greedy_solution = new float[number_of_items+1];
items_No = new int[number_of_items+1];
items_value = new int[number_of_items+1];
items_weight = new int[number_of_items+1];
for(int i = 0; i <= number_of_items; i++)
{
*(items_No + i) = copy_object.items_No[i];
*(items_value + i) = copy_object.items_value[i];
*(items_weight + i) = copy_object.items_weight[i];
*(greedy_solution + i) = copy_object.greedy_solution[i];
}
int **dynamic_solution = new int *[number_of_items+1];
for(int x = 0; x <= number_of_items; x++)
{
dynamic_solution[x] = new int[weight_of_bag+1];
}
for (int x = 0; x <= number_of_items; ++x)
{
for ( int y = 0; y <= weight_of_bag; ++y )
{
dynamic_solution[x][y] = copy_object.dynamic_solution[x][y];
}
}
}
const Knapstack &Knapstack::operator = (Knapstack &obj)
{
if (&obj != this)
{
if((number_of_items != obj.number_of_items) || (weight_of_bag != obj.weight_of_bag))
{
delete[] items_No;
delete[] items_value;
delete[] items_weight;
delete[] greedy_solution;
for( int x = 0; x <= number_of_items; x++){
delete[] dynamic_solution[x];
}
delete[] dynamic_solution;
}
items_No = new int[number_of_items+1];
items_value = new int[number_of_items+1];
items_weight = new int[number_of_items+1];
greedy_solution = new float[number_of_items+1];
for(int i = 0; i <= number_of_items; i++)
{
*(items_No + i) = obj.items_No[i];
*(items_value + i) = obj.items_value[i];
*(items_weight + i) = obj.items_weight[i];
*(greedy_solution + i) = obj.greedy_solution[i];
}
dynamic_solution = new int*[number_of_items+1];
for(int i = 0; i <= number_of_items; i++)
{
dynamic_solution[i] = new int[weight_of_bag];
}
for (int i = 0; i <= number_of_items; i++)
{
for (int j = 0; j <= weight_of_bag; j++)
{
dynamic_solution[i][j] = obj.dynamic_solution[i][j];
}
}
}
return *this;
}
istream &operator >> (istream &input, Knapstack &obj)
{
cout << "The number of items : \n";
input >> obj.number_of_items;
cout << "The maximum capacity of bags : \n";
input >> obj.weight_of_bag;
for(int i = 1; i <= obj.number_of_items; i++)
{
cout << " The [" << i << "] item weigh : ";
input >> obj.items_weight[i];
cout << " Its value is : ";
input >> obj.items_value[i];
cout << endl;
}
return input;
}
void Knapstack::SortItems()
{
for(int i = 1; i <= number_of_items ; i++)
{
for(int j = 1;j <= number_of_items; j++)
{
if(((float)items_value[i]/items_weight[i]) < ((float)items_value[i+1]/items_weight[i+1]))
{
swap(items_weight[i], items_weight[i+1]);
swap(items_value[i], items_value[i+1]);
}
}
}
}
void Knapstack::Solving_by_greedy()
{
int i;
float max_profit = 0.0;
SortItems();
for(i = 1; i < number_of_items; i++)
{
if(items_weight[i] > weight_of_bag)
break;
greedy_solution[i] = 1.0;
weight_of_bag = weight_of_bag - items_weight[i];
max_profit += items_value[i];
cout << "\n Items chosen : "<< items_No[i]
<< "with the % quantiy %:" << greedy_solution[i] << endl;
}
if(i <= number_of_items)
{
greedy_solution[i] = (float)weight_of_bag/items_weight[i];
cout << "\n" << items_No[i] << "with the quantiy % chosen : " << greedy_solution[i] << endl;
max_profit = max_profit + (greedy_solution[i]*items_value[i]);
}
cout << "\n Maximum value the thief can get : " << max_profit;
}
void Knapstack::Solving_by_dynamic_programming()
{
for (int i = 1; i <= number_of_items; i++)
{
for (int j = 0; j <= weight_of_bag; j++)
{
dynamic_solution[i][j] = dynamic_solution[i-1][j];
if (j >= items_weight[i] && dynamic_solution[i][j] < dynamic_solution[i-1][j-items_weight[i]] + items_value[i] )
{
dynamic_solution[i][j] = dynamic_solution[i-1][j-items_weight[i]] + items_value[i];
}
}
}
}
void Knapstack::Dynamic_programming_trace_solution()
{
cout << "Maximum value the thief can get : "
<< dynamic_solution[number_of_items][weight_of_bag] << endl;
while (number_of_items != 0)
{
if(dynamic_solution[number_of_items][weight_of_bag] != dynamic_solution[number_of_items-1][weight_of_bag])
{
cout << "Chosen items are : " << number_of_items;
weight_of_bag = weight_of_bag - items_weight[number_of_items];
}
number_of_items--;
}
}
void Menu()
{
Knapstack problem(1,1);
cout << "\nInformation data\n";
cin >> problem;
int command;
cout << "Enter a method for solving knapstack problems [1].Greedy\n"
<< "[2].Dynamic programming\n" << endl;
cin >> command;
if (command == 1)
{
problem.Solving_by_greedy();
}
else
{
problem.Solving_by_dynamic_programming();
problem.Dynamic_programming_trace_solution();
}
}
int main()
{
Menu();
system("pause");
return 0;
}
[/syntax]
I opened the file.core.exe with notepad, it had this message :
[main] KnapStackUseClass 1000 (0) exception: trapped!
[main] KnapStackUseClass 1000 (0) exception: code 0xC0000005 at 0x6105AFE2
[main] KnapStackUseClass 1000 (0) exception: ax 0x0 bx 0xA031630 cx 0xA dx 0x0
[main] KnapStackUseClass 1000 (0) exception: si 0x30 di 0xA031610 bp 0x246FE2C sp 0x246FE00
[main] KnapStackUseClass 1000 (0) exception: exception is: STATUS_ACCESS_VIOLATION
[main] KnapStackUseClass 1000 (1) stack: Stack trace:
[main] KnapStackUseClass 1000 (0) stack: frame 0: sp = 0x246F9F0, pc = 0x6100A2C3
[main] KnapStackUseClass 1000 (0) stack: frame 1: sp = 0x246FA2C, pc = 0x7C9037BF
[main] KnapStackUseClass 1000 (0) stack: frame 2: sp = 0x246FA50, pc = 0x7C90378B
[main] KnapStackUseClass 1000 (0) stack: frame 3: sp = 0x246FB00, pc = 0x7C90EAFA
[main] KnapStackUseClass 1000 (0) stack: frame 4: sp = 0x246FE2C, pc = 0x6101A4A7
[main] KnapStackUseClass 1000 (0) stack: frame 5: sp = 0x246FE3C, pc = 0x40E927
[main] KnapStackUseClass 1000 (0) stack: frame 6: sp = 0x246FEC4, pc = 0x401265
[main] KnapStackUseClass 1000 (0) stack: frame 7: sp = 0x246FEE4, pc = 0x4023D1
[main] KnapStackUseClass 1000 (0) stack: frame 8: sp = 0x246FF2C, pc = 0x4023F1
[main] KnapStackUseClass 1000 (0) stack: frame 9: sp = 0x246FF34, pc = 0x61004402
[main] KnapStackUseClass 1000 (0) stack: frame 10: sp = 0x246FF88, pc = 0x61004420
[main] KnapStackUseClass 1000 (0) stack: frame 11: sp = 0x246FF94, pc = 0x4144AE
[main] KnapStackUseClass 1000 (0) stack: frame 12: sp = 0x246FFA4, pc = 0x40103A
[main] KnapStackUseClass 1000 (0) stack: frame 13: sp = 0x246FFC0, pc = 0x7C816FD7
[main] KnapStackUseClass 1000 (0) stack: frame 14: sp = 0x246FFF0, pc = 0x0
[main] KnapStackUseClass 1000 (0) stack: End of stack trace
I already checked each algorithm by separate small program. It worked really fine ! But when I used class stuff....^^! Thanks !
