-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6assignment.cpp
More file actions
81 lines (80 loc) · 1.4 KB
/
Copy path6assignment.cpp
File metadata and controls
81 lines (80 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
using namespace std;
float wt[20],pt[20],profit=0.0,y[20];
int n,i,w,temp,j,u;
class fractional_knapsack
{
public:
void getdata()
{
cout<<"enter the max weight of the bag:"<<endl;
cin>>w;
cout<<"how many objects you want to store inside the bag:"<<endl;
cin>>n;
cout<<"enter the wt of the objects:\n";
for(i=1;i<=n;i++)
{
cin>>wt[i];
}
cout<<"enter the profit of the objects:"<<endl;
for(i=1;i<=n;i++)
{
cin>>pt[i];
}
}
void sortdata()
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((pt[j]/wt[j])<(pt[j+1]/wt[j+1]))
{
temp=pt[j];
pt[j]=pt[j+1];
pt[j+1]=temp;
temp=wt[j];
wt[j]=wt[j+1];
wt[j+1]=temp;
}
}
}
}
void calculation()
{
for(i=0;i<n;i++)
y[i]=0.0;
u=w;
for(i=0;i<n;i++)
{
if(wt[i]>u)
break;
y[i]=1.0;
u=u-wt[i];
}
if(i<n)
y[i]=u/wt[i];
for(i=0;i<n;i++)
profit=profit+(pt[i]*y[i]);
}
void displaydata()
{
cout<<"-------------------------------------------\n";
cout<<"objectno weigth profit\t\n";
cout<<"-----------------------------------------------\n";
for(i=1;i<=n;i++)
{
cout<<i<<"\t"<<wt[i]<<"\t"<<pt[i]<<"\t"<<endl;
}
cout<<"MAXIMUM PROFIT:"<<profit;
}
};
int main()
{
fractional_knapsack obj;
obj.getdata();
obj.sortdata();
obj.calculation();
obj.displaydata();
return 0;
}