Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help programming in C please.. not sure on how to print out item names You will write a program knapsack which chooses items from

image text in transcribedimage text in transcribedneed help programming in C please.. not sure on how to print out item names

You will write a program knapsack which chooses items from a manifest. knapsack takes two arguments: the weight limit and a manifest file. It chooses a selection of items from the manifest that maximizes the total value without exceeding the weight limit. Once the selection is made, knapsack will print the selected items, one per line, in the same order they appear in the manifest file. After the list, knapsack will print the total value and total weight of the selected items. The weight limit is a positive integer written in decimal. (You may use atoi or some other function to covert the string to an integer.) The manifest file is specified using a path that should be passed to open without interpretation. Your implementation SHOULD check for the correct number of arguments and report an error if first argument is not a positive integer or if the file specified by the second argument cannot be opened. You will not be tested for these cases. Usage $ cat manifest.01.txt 6 Heavy 4 39 Light1 1 10 Light2 1 10 Light3 1 10 Light4 1 10 Light5 1 10 $ ./knapsack 5 manifest.01.txt Light1 Light2 Light3 For limit 9, it is items 1, 2, and 4. For limit 11, it is 0, 1, 2, and 3. Light4 Light5 50 / 5 /R/2/2/p/src $ $ ./knapsack 6 manifest.01.txt Heavy Light1 Light2 59 / 6 2.1 Manifest file format The manifest file begins with an integer indicating the number of items in the manifest. This is followed by the item definitions. Each item is specified by giving a name, a weight, and a value. The name is a string of at most 31 non-whitespace characters. The weight and value are positive integers. All integers will be given in decimal notation. The format specifies only that names and numbers are separated by whitespace. It is not advised to assume that items will be given one per line. Your program will only be given manifests with uniquely named items. It is not necessary to check the uniqueness of item names. Your program SHOULD confirm that all specified weights and values are positive. There is no specified maximum number of items, but you may assume that the number of items will fit in a regular int, signed or unsigned. Note that the %s format specifier does not allocate space, and may only be safely used when an upper limit is given: %31s. 2.2 Output format knapsack prints the names of the selected items, one per line, in the same order they are given in the manifest file. Item names must be given exactly as in the file. Immediately after the list of items, knapsack must print the total value and total weight of the selected items, separated by a slash (/). The numbers must be printed in decimal, without leading zeros, and must be separated from the slash by a single space character. You will write a program knapsack which chooses items from a manifest. knapsack takes two arguments: the weight limit and a manifest file. It chooses a selection of items from the manifest that maximizes the total value without exceeding the weight limit. Once the selection is made, knapsack will print the selected items, one per line, in the same order they appear in the manifest file. After the list, knapsack will print the total value and total weight of the selected items. The weight limit is a positive integer written in decimal. (You may use atoi or some other function to covert the string to an integer.) The manifest file is specified using a path that should be passed to open without interpretation. Your implementation SHOULD check for the correct number of arguments and report an error if first argument is not a positive integer or if the file specified by the second argument cannot be opened. You will not be tested for these cases. Usage $ cat manifest.01.txt 6 Heavy 4 39 Light1 1 10 Light2 1 10 Light3 1 10 Light4 1 10 Light5 1 10 $ ./knapsack 5 manifest.01.txt Light1 Light2 Light3 For limit 9, it is items 1, 2, and 4. For limit 11, it is 0, 1, 2, and 3. Light4 Light5 50 / 5 /R/2/2/p/src $ $ ./knapsack 6 manifest.01.txt Heavy Light1 Light2 59 / 6 2.1 Manifest file format The manifest file begins with an integer indicating the number of items in the manifest. This is followed by the item definitions. Each item is specified by giving a name, a weight, and a value. The name is a string of at most 31 non-whitespace characters. The weight and value are positive integers. All integers will be given in decimal notation. The format specifies only that names and numbers are separated by whitespace. It is not advised to assume that items will be given one per line. Your program will only be given manifests with uniquely named items. It is not necessary to check the uniqueness of item names. Your program SHOULD confirm that all specified weights and values are positive. There is no specified maximum number of items, but you may assume that the number of items will fit in a regular int, signed or unsigned. Note that the %s format specifier does not allocate space, and may only be safely used when an upper limit is given: %31s. 2.2 Output format knapsack prints the names of the selected items, one per line, in the same order they are given in the manifest file. Item names must be given exactly as in the file. Immediately after the list of items, knapsack must print the total value and total weight of the selected items, separated by a slash (/). The numbers must be printed in decimal, without leading zeros, and must be separated from the slash by a single space character

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions