0% found this document useful (0 votes)
7 views

Ada PGM 7

Uploaded by

salim07238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Ada PGM 7

Uploaded by

salim07238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ada lab pgm 7

#include <stdio.h>

// Function to sort items based on value/weight ratio

void sortItems(int n, double weight[], double profit[], double ratio[]) {

double temp;

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

if (ratio[i] < ratio[j]) {

temp = ratio[j];

ratio[j] = ratio[i];

ratio[i] = temp;

temp = weight[j];

weight[j] = weight[i];

weight[i] = temp;

temp = profit[j];

profit[j] = profit[i];

profit[i] = temp;

// Function to solve the discrete knapsack problem

double discreteKnapsack(int n, double weight[], double profit[], double ratio[], double capacity) {

double Totalvalue = 0.0;


for (int i = 0; i < n; i++) {

if (weight[i] > capacity) {

break;

} else {

Totalvalue += profit[i];

capacity -= weight[i];

return Totalvalue;

// Function to solve the continuous knapsack problem

double continuousKnapsack(int n, double weight[], double profit[], double ratio[], double capacity) {

double Totalvalue = 0.0;

for (int i = 0; i < n; i++) {

if (weight[i] > capacity) {

Totalvalue += (ratio[i] * capacity);

break;

} else {

Totalvalue += profit[i];

capacity -= weight[i];

return Totalvalue;

int main() {

double weight[50], profit[50], ratio[50], capacity;

int n, choice;
printf("Enter the number of items: ");

scanf("%d", &n);

for (int i = 0; i < n; i++) {

printf("Enter Weight and Profit for item[%d]: ", i);

scanf("%lf %lf", &weight[i], &profit[i]);

printf("Enter the capacity of knapsack: ");

scanf("%lf", &capacity);

for (int i = 0; i < n; i++) {

ratio[i] = profit[i] / weight[i];

sortItems(n, weight, profit, ratio);

printf("Choose the knapsack problem to solve:\n");

printf("1. Discrete Knapsack Problem\n");

printf("2. Continuous Knapsack Problem\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

double Totalvalue;

if (choice == 1) {

Totalvalue = discreteKnapsack(n, weight, profit, ratio, capacity);

printf("\nThe maximum value for Discrete Knapsack is: %lf\n", Totalvalue);

} else if (choice == 2) {
Totalvalue = continuousKnapsack(n, weight, profit, ratio, capacity);

printf("\nThe maximum value for Continuous Knapsack is: %lf\n", Totalvalue);

} else {

printf("Invalid choice!\n");

return 0;

Explanation:
1. Sorting Function: sortItems sorts the items based on their value-to-weight ratio in
descending order.
2. Discrete Knapsack Function: discreteKnapsack solves the discrete knapsack
problem.
3. Continuous Knapsack Function: continuousKnapsack solves the continuous knapsack
problem.
4. Main Function: The main function collects user input, sorts the items, prompts the user
for the type of knapsack problem, and displays the result
Output:
 Number of items: 3
 Weight and profit for each item:
o Item 0: Weight = 10, Profit = 60
o Item 1: Weight = 20, Profit = 100
o Item 2: Weight = 30, Profit = 120
 Knapsack capacity: 50
 Choice: 1 (Discrete Knapsack)
Discrete Knapsack Problem
In the discrete knapsack problem, the program will select items based on their value-to-weight
ratio until the capacity is full or no more items can be added.
1. Compute the value-to-weight ratios:
o Item 0: 6010=6.0\frac{60}{10} = 6.01060=6.0
o Item 1: 10020=5.0\frac{100}{20} = 5.020100=5.0
o Item 2: 12030=4.0\frac{120}{30} = 4.030120=4.0
2. Sort the items based on these ratios (descending order):
o Item 0: Weight = 10, Profit = 60, Ratio = 6.0
o Item 1: Weight = 20, Profit = 100, Ratio = 5.0
o Item 2: Weight = 30, Profit = 120, Ratio = 4.0
3. Select items until the capacity is full:
o Add Item 0: Remaining capacity = 50 - 10 = 40, Total value = 60
o Add Item 1: Remaining capacity = 40 - 20 = 20, Total value = 160
o Item 2 can't be added completely since its weight (30) exceeds the remaining
capacity (20).
Output for Discrete Knapsack:
Enter the number of items: 3

Enter Weight and Profit for item[0]: 10 60

Enter Weight and Profit for item[1]: 20 100

Enter Weight and Profit for item[2]: 30 120

Enter the capacity of knapsack: 50

Choose the knapsack problem to solve:

1. Discrete Knapsack Problem

2. Continuous Knapsack Problem

Enter your choice (1 or 2): 1

The maximum value for Discrete Knapsack is: 160.000000

Continuous Knapsack Problem


In the continuous knapsack problem, the program can take fractions of items if necessary to
maximize the value.
1. Compute the value-to-weight ratios (same as above).
2. Sort the items based on these ratios (same as above).
3. Select items until the capacity is full:
o Add Item 0: Remaining capacity = 50 - 10 = 40, Total value = 60
o Add Item 1: Remaining capacity = 40 - 20 = 20, Total value = 160
o Take a fraction of Item 2: Remaining capacity = 20, Fraction = 2030≈0.667\frac{20}{30} \
approx 0.6673020≈0.667
o Add the fraction of Item 2: Total value = 160 + 0.667×1200.667 \times 1200.667×120 =
160 + 80 = 240
Output for Continuous Knapsack:
Enter the number of items: 3

Enter Weight and Profit for item[0]: 10 60


Enter Weight and Profit for item[1]: 20 100

Enter Weight and Profit for item[2]: 30 120

Enter the capacity of knapsack: 50

Choose the knapsack problem to solve:

1. Discrete Knapsack Problem

2. Continuous Knapsack Problem

Enter your choice (1 or 2): 2

The maximum value for Continuous Knapsack is: 240.000000

You might also like