Answer :
The C/C++ program to solve the fractional Knapsack Problem is given below:
The Program
// C/C++ program to solve fractional Knapsack Problem
#include
#include
using namespace std;
#define SIZE 4
struct KnapItems
{
int val;
int w;
};
// This function sort the KnapItems acc to val/weight ratio
bool compare(struct KnapItems it1, struct KnapItems it2)
{
double ratio1 = (double)it1.val / it1.w;
double ratio2 = (double)it2.val / it2.w;
return ratio1 > ratio2;
}
// Greedy algorithm for computing optimal value
double fractionalKS(int W, struct KnapItems arr[], int n)
{
sort(arr, arr + n, compare);
int currentWeight = 0;
double result = 0.0; // Result (value in Knapsack)
// For Loop for all KnapItemss
for (int i = 0; i < n; i++)
{
// If adding KnapItems not overflowing then adding
if (currentWeight + arr[i].w <= W)
{
currentWeight += arr[i].w;
result += arr[i].val;
}
// Adding fractional part If we cannot add current KnapItems
else
{
int remain = W - currentWeight;
result += arr[i].val * ((double) remain / arr[i].w);
break;
}
}
return result;
}
Read more about programs here:
https://brainly.com/question/1538272
#SPJ4