Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1 You are given a set of n items, where each item i has 1 ) A weight wi A value vi The total
Question You are given a set of items, where each item i has A weight wi A value vi The total capacity of your knapsack is w Determine the optimal way to fill the knapsack with a subset of these items to maximize the total value. This means you can take any portion of item where xi is the fraction of item i taken. The total weight of the selected items including fractions must not exceed the knapsack's capacity Hint: Use merge sort algorithm. You have been provided with the driver code, with appropriate TODO comments to guide you through the implementation. You have to only edit merge mergesort and knapsack subroutines in the file studentfile.c You have been provided with the driver code in the main function in the same file. DO NOT MODIFY ANY LINES OF THE FILE APART FROM WITHIN THE FUNCTION merge mergesort and knapsack else your assignment will not be evaluated. You are free to add other functions if you wish to Note: The example input given here differs from the test cases checked when the code is submitted. code #include #include Define a structure for items typedef struct int weight; int value; double ratio; Item; Function prototypes void mergesortItem items int left, int right; void mergeItem items int left, int mid, int right; double knapsackItem items int n int capacity; Main function int main int n capacity; User inputs scanfd &n; Item itemsn; for int i ; i n; i scanfd d &itemsivalue, &itemsiweight; itemsiratio doubleitemsivalue itemsiweight; scanfd &capacity; Calculate the maximum value double maxvalue knapsackitems n capacity; printff maxvalue;
Question
You are given a set of items, where each item i has
A weight wi
A value vi
The total capacity of your knapsack is w
Determine the optimal way to fill the knapsack with a subset of these items to maximize the total value. This means you can take
any portion of item where xi is the fraction of item i taken. The total weight of the selected items including
fractions must not exceed the knapsack's capacity
Hint: Use merge sort algorithm.
You have been provided with the driver code, with appropriate TODO comments to guide you through the implementation. You have to
only edit merge mergesort and knapsack subroutines in the file studentfile.c You have been provided with the driver
code in the main function in the same file. DO NOT MODIFY ANY LINES OF THE FILE APART FROM WITHIN THE FUNCTION merge
mergesort and knapsack else your assignment will not be evaluated. You are free to add other functions if you wish to
Note: The example input given here differs from the test cases checked when the code is submitted.
code
#include
#include
Define a structure for items
typedef struct
int weight;
int value;
double ratio;
Item;
Function prototypes
void mergesortItem items int left, int right;
void mergeItem items int left, int mid, int right;
double knapsackItem items int n int capacity;
Main function
int main
int n capacity;
User inputs
scanfd &n;
Item itemsn;
for int i ; i n; i
scanfd d &itemsivalue, &itemsiweight;
itemsiratio doubleitemsivalue itemsiweight;
scanfd &capacity;
Calculate the maximum value
double maxvalue knapsackitems n capacity;
printff
maxvalue;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started