Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with following programming assignment in C code: Details Your program should operate as follows: It expects a single argument, which is the weight

Need help with following programming assignment in C code:

Details

Your program should operate as follows:

  • It expects a single argument, which is the weight capacity of the knapsack e.g.

programname 15, would run your program for a bag with a weight capacity of 15;

  • your program should read the file knapsack.data from the current directory. This file should contain a set of weight/value/name triples, such as the following:
    1. 10 mouse
    2. 25 cellphone

5 40 tablet

7 100 laptop

In the above file, there are four items: a mouse with a weight of 1 and value of 10; a cellphone with a weight of 2 and value of 25; and so on.

  • After reading knapsack.data and storing the data in arrays, you should call a function (I called this MaxVal(x) in class) for computing the maximum value that can be loaded into a

knapsack with a weight capacity of x. MaxVal(x) works by returning 0 if x <= 0, otherwise returning the maximum of (v0+MaxVal(x-w0), v1+MaxVal(x-w1), ..., Vn+MaxVal(x-wn)) where each (vi,wi) is the value and weight of item i, and the maximum is computed over all items where wi <= x (i.e. where item i can fit in a bag with capacity x). This is the algorithm that was discussed in class.

  • In addition to printing the maximum possible value, you should print how many of each item should be put in the bag.
  • Your program should operate efficiently by storing the value of each MaxVal(x) that is computed, and retrieving that value (max value and list of items) instead of computing it whenever possible.
  • Your program should echo the contents of knapsack.data, and then print the maximum value that can be loaded in a knapsack of the given capacity (which was specified as the argument to "pa5").

Error Checking

Make sure you handle error conditions, including: no argument to the program; too many arguments to the program; invalid argument to the program; missing or invalid knapsack.data file; and so on.

Assumptions

You may assume a maximum of 128 items in the knapsack.data file, and a maximum bag capacity of 1024. You may assume a maximum length of 32 characters for the 3rd column of knapsack.data (the item name), and assume that the name contains no spaces.

To access the arrays inside the function MaxVal() I planned to try using a global variable, although I'm not sure if there's a more efficient way to do this.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions