Skip to content

Commit 72cfd27

Browse files
New Commit
1 parent b4ec278 commit 72cfd27

File tree

2 files changed

+0
-103
lines changed

2 files changed

+0
-103
lines changed

TowerofHanoi.exe

0 Bytes
Binary file not shown.

fractional_knapsack.c

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
#include <stdio.h>
32
float wt[100], pt[100];
43
float pw[100];
@@ -99,106 +98,4 @@ int main()
9998

10099

101100
knapsack(capacity, n);
102-
=======
103-
#include <stdio.h>
104-
float wt[100], pt[100];
105-
float pw[100];
106-
int capacity;
107-
108-
void sort(int n)
109-
{
110-
int i, j;
111-
float t, t1, t2;
112-
for(int i=0;i<n-1;i++)
113-
{
114-
for(int j=0;j<n-1-i;j++)
115-
{
116-
if(pw[j]<pw[j+1]){
117-
t= pw[j];
118-
t1 = wt[j];
119-
t2 = pt[j];
120-
121-
pw[j] = pw[j+1];
122-
wt[j] = wt[j+1];
123-
pt[j] = pt[j+1];
124-
125-
pw[j+1] = t;
126-
wt[j+1] = t1;
127-
pt[j+1] = t2;
128-
}
129-
}
130-
}
131-
}
132-
133-
void knapsack(int capacity, int n)
134-
{
135-
int i,k=0;
136-
float mul, profit = 0;
137-
// Calculating the unit profit values
138-
for(i = 0; i<n; i++)
139-
{
140-
pw[i] = pt[i]/wt[i];
141-
}
142-
143-
sort(n); // Sorting the arrays wrt unit profit pw
144-
145-
/*for (i=0;i<n;i++)
146-
{
147-
printf("%f ", pw[i]);
148-
}
149-
printf ("\n");
150-
for (i=0;i<n;i++)
151-
{
152-
printf("%f ", pt[i]);
153-
}
154-
printf ("\n");
155-
for (i=0;i<n;i++)
156-
{
157-
printf("%f ", wt[i]);
158-
}
159-
printf ("\n");
160-
*/
161-
162-
while(capacity != 0)
163-
{
164-
if(capacity >= wt[k])
165-
{
166-
capacity = (float)capacity - wt[k];
167-
profit = profit+pt[k];
168-
k++;
169-
}
170-
else
171-
{
172-
mul = capacity/wt[k];
173-
wt[k] = wt[k]*mul;
174-
pt[k] = pt[k]*mul;
175-
capacity = (float)capacity - wt[k];
176-
profit = profit + pt[k];
177-
k++;
178-
}
179-
}
180-
printf("The Maximum profit that can be made is: %f", profit);
181-
}
182-
183-
184-
int main()
185-
{
186-
int n,i;
187-
printf("Enter the number of weights and profit: ");
188-
scanf("%d", &n);
189-
190-
printf("Enter the capacity of the knapsack: ");
191-
scanf("%d", &capacity);
192-
193-
for(i = 0; i< n; i++)
194-
{
195-
printf("Weight: ");
196-
scanf("%f", &wt[i]);
197-
printf("Profit: ");
198-
scanf("%f", &pt[i]);
199-
}
200-
201-
202-
knapsack(capacity, n);
203-
>>>>>>> a9b6520f1aa074c45bc777fd42beb569ca960413
204101
}

0 commit comments

Comments
 (0)