Question: DO NOT ANSWER IN C + + This lab assignment is a variation on the Knapsack Problem lab exercise from a couple of weeks ago.
DO NOT ANSWER IN CThis lab assignment is a variation on the Knapsack Problem lab 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:
; CS Lab 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 VisUAL ARMsim, Mac, Raspberry Pi et 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 Chosen column 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 column or value second column You may sort the entries into ascending sequence or descending sequence.
Use your CS C language 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 weight value,
; selected and chooses which items to include in the knapsack.
; Items are selected by ; putting a value of in the
; selected column.
; Register usage:
; R R parameters as listed above
; other registers as used
It is usually a good idea to list any special starting conditions, input limits acceptable 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 Chosen into 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
ADRRtheArray
ADRRworkArea
LDRRR# ; maxWeight
BLknapsack
END
workAreaDCD
maxWeightDCD
;WeightValue,Chosen
theArrayDCD
aDCD
aDCD
aDCD
aDCD
aDCD
aDCD
lastDCD
;YOUR CODE GOES HERE
knapsack
MOVPCLR;return
Grading:
Correct program heading and function headings points
Correct sort points
Selected items weigh less than maximum, more than one item selected, et cetera points
Proper function operation return to calling program, total weight returned in register no END statements or SWI instructions to end the program in the function, et cetera points
functions will be tested with the sa
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
