Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( Please use one of the emulators listed to send the FINAL CODE ) This lab assignment is a variation on the Knapsack Problem lab
Please use one of the emulators listed to send the FINAL CODEThis lab assignment is a variation on the Knapsack Problemlab exercise from a couple of weeks ago. The input is the same as in the lab exercise, but the processing and output are different.
Your program must start with the following heading:
; CSLab Assignment Section xxxxx Last, First
; Emulator used: yyyyyy
You must replace xxxxx with your section number, Last and First with your name, and yyyyy with the emulator or other platform used VisUALARMsim Mac, Raspberry Piet cetera You will have points taken off if the heading is not correct. Also, failure to list the emulator correctly will cause your lab to be graded last.
Your program must have two functions. One is named knapsack and operates similarly to the earlier lab exercise. The other is named sort and will be called from the knapsack function.
Input: The address of an array holding a set of triplets of values:
A weight
A value
A chosen indicator, for not chosen, for chosen
This address will be passed to your function in register as a threedimensional integer array containing the weight and value of each item you may wish to place in the knapsack. The function must be named knapsack.
Also, you will receive a parameter m in register which represents the maximum weight you can carry.
The end of the array will be marked with a special value of
Typical input for an afternoon hike with supper:
Weight Value Chosen Possible Item
Can of beans
Can opener
Silverware
Can of beef stew
Bottle of wine
Matches
Can of Vienna sausage
end of list
Maximum weight:
Knapsack Output:
Updated entries in the Chosencolumn in the original array
The total value of your choices in register
You do not have to perform any special linear programming as discussed in the earlier lab exercise. But your knapsack function must call a sort function, described below.
Sort Function:
This function receives the address of the array of triplets originally passed to the knapsack function. Its purpose is to sort the array into the desired sequence. You may sort the array entries by weight first columnor value second column You may sort the entries into ascending sequence or descending sequence.
Use your CSClanguage course textbook to determine how to sort an array if your introductory programming course did not cover that. Also, you may use Wikipedia, cplusplus.com, cppreference.com, or any other internet reference you desire to develop the sorting code. Most online examples are for sorting a singlecolumn array, and you will almost certainly have to modify any example you find.
Function Headings
Functions in any language should have a heading which gives some details of the function. The following is an example for VisUAL:
; knapsack function
; input: Table address in R
; Maximum weight in R
; Output: Actual total weight for selected items
; Purpose: Scans an array of integer values weightvalue
; selectedand chooses which items to include in the knapsack.
; Items are selected by ; putting a value of in the
; selectedcolumn
; Register usage:
; RRparameters as listed above
; other registers as used
It is usually a good idea to list any special starting conditions, input limitsacceptable ranges of input values, or similar information as part of the heading. You will have points deducted if you do not include an appropriate heading similar to the above for both the knapsack and the sort functions.
Processing
Your knapsack function will be called by the sample test driver program shown later. It will call the sort function to sort the table called theArray in the test driver program You may sort by weight or by value.
When the sort function returns to the knapsack function, the knapsack function should move a into the third variable Choseninto the entries you want to select. Rather than doing any complex linear programming, you may sort the table by weight, then add in items until the weight limit is met, or sort the table by value, adding in the highest value items until the weight limit is met.
Sample Test Driver Program
; Knapsack lab exercise Sample main driver program
ADR RtheArray
ADR RworkArea
LDR RR# ; maxWeight
BL knapsack
END
workArea DCD
maxWeight DCD
; Weight,Value,Chosen
theArray DCD
a DCD
a DCD
a DCD
a DCD
a DCD
a DCD
last DCD
; YOUR CODE GOES HERE
knapsack
MOV PCLR ;return
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