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 C++This 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:
; CS130 Lab Assignment 5 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, 0 for not chosen, 1 for chosen
This address will be passed to your function in register 0 as a three-dimensional 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 1, which represents the maximum weight you can carry.
The end of the array will be marked with a special value of -999.
Typical input for an afternoon hike with supper:
Weight
Value
Chosen
Possible Item
5
2
0
Can of beans
2
9
0
Can opener
3
3
0
Silverware
4
7
0
Can of beef stew
8
5
0
Bottle of wine
1
10
0
Matches
2
8
0
Can of Vienna sausage
-999
end of list
Maximum weight: 20
Knapsack Output:
Updated entries in the Chosen column in the original array
The total value of your choices in register 0
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 CS116 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 single-column 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 R0
; Maximum weight in R1
; 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 1 in the
; selected column.
; Register usage:
; R0, R1= 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 1 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
ADRR0,theArray
ADRR12,workArea
LDRR1,[R12,#4] ; maxWeight
BLknapsack
END
workAreaDCD0
maxWeightDCD25
;Weight,Value,Chosen
theArrayDCD5,2,0
a2DCD2,9,0
a3DCD3,3,0
a4DCD4,7,0
a5DCD8,5,0
a6DCD1,10,0
a7DCD2,8,0
lastDCD-999
;YOUR CODE GOES HERE
knapsack
MOVPC,LR;return
Grading:
Correct program heading and function headings 10 points
Correct sort 10 points
Selected items weigh less than maximum, more than one item selected, et cetera 10 points
Proper function operation return to calling program, total weight returned in register 0, no END statements or SWI instructions to end the program in the function, et cetera 10 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!