forked from Shreyashi14/DAA-AIML-Hons.-B1-B2-B3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack.c
51 lines (51 loc) · 1.34 KB
/
Knapsack.c
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
#include<stdio.h>
int main()
{
int i, n, constraint, count = 0;
printf("Enter the number of objects: ");
scanf("%d", &n);
int a[n];
float profit[n], weight[n], Profit_by_Weight[n];
for(i=0; i<n; i++)
{
printf("\nEnter weight of object %d: ", i+1);
scanf("%f", &weight[i]);
printf("Enter profit margin of object %d: ", i+1);
scanf("%f", &profit[i]);
printf("\n");
Profit_by_Weight[i] = (float)profit[i]/weight[i];
printf("\n%.2f", Profit_by_Weight);
}
printf("\n");
for (int i = 0; i < n - 1; i++)
{
int maxIndex = i;
for (int j = i + 1; j < n; j++)
{
if (Profit_by_Weight[j] > Profit_by_Weight[maxIndex])
{
maxIndex = j;
}
}
if (maxIndex != i)
{
count++;
int temp = Profit_by_Weight[i];
int temp2 = weight[i];
Profit_by_Weight[i] = Profit_by_Weight[maxIndex];
weight[i] = weight[maxIndex];
Profit_by_Weight[maxIndex] = temp;
weight[maxIndex] = temp2;
}
}
for(i=0; i<n; i++)
{
printf("%.2f ", Profit_by_Weight[i]);
}
printf("\n");
for(i=0; i<n; i++)
{
printf("%.2f ", weight);
}
return 0;
}