Submit to Blackboard before the deadline. You will be able to revise your answers until the deadline with no penalty.
IMPORTANT: By submitting your answers, you are certifying that these answers have been exclusively your own work.
All students enrolled in this course are expected to adhere to the UT Arlington Honor Code:
I pledge, on my honor, to uphold UT Arlington's tradition of academic
integrity, a tradition that values hard work and honest effort in the
pursuit of academic excellence.
I promise that I will submit only work that I personally create or
contribute to group collaborations, and I will appropriately reference
any work from other sources. I will follow the highest standards of
integrity and uphold the spirit of the Honor Code.
Task 1 (20 pts.)
File knapsack_bottom_up.c contains the implementation of a bottom-up solution for the knapsack problem. Modify the file, to include:
- A definition of the following structure:
struct Solution
{
int value;
list objects;
};
- A function knapsack2 with the following signature:
struct Solution knapsack2(int max_weight, struct Items items);
Your function knapsack2 should return a solution that contains both the optimal value that can be attained for the specific weight and items, AND a list of the actual objects that produce this value. For example, suppose that the optimal value is 25, and it can be obtained by choosing:
- Two objects of type "type4", each of which has value 10.
- One object of type "type2", that has value 5.
Then, the knapsack2 function should return a result such that:
- result.value = 25
- result.objects is a list of length 3, that contains three strings: "type4", "type4", "type2".
Feel free to use files lists.h and lists.c, but you are not allowed to modify those files. The lists used in your code should be defined and accessed using only the interface in lists.h.
Feel free to modify the main function so that it tests your code in any way you see fit.
IMPORTANT: your solution for this task should use bottom-up dynamic programming.
Task 2 (20 pts.)
Do the same thing as in Task 1 (i.e., implement a function called knapsack2, with the exact same signature as in Task 1), but this time using top-down dynamic programming. You can use file knapsack_top_down.c as the basis for your implementation.
Task 3 (20 pts.)
Create a C file called make_change.c, that contains a function make_change with the following signature and specifications:
Signature:
list make_change(int amount, list coins);
Specifications:
- Argument coins is a list of integers that describes the types of coins that are available. For each coin, we need to specify its value in cents. For example, to specify the coins typically used in the United States, the list should contain integers 1, 5, 10, 25.
- Argument amount is the total amount, in cents, that we want to pay.
- The function should figure out and return the combination of coins that produces EXACTLY the specified amount, with as few coins as possible. For example, if we are using coins with values 1, 5, 10, 25, and the amount is 37, then the result should be a list containing integers 25, 10, 1, 1, since the smallest combination of U.S. coins that is equal to 37 cents is a quarter, a dime, and two pennies.
- While in the above examples we used U.S. coins, your function should work with arbitrary types of coins. For example, the argument coins could contain integers 1, 2, 5, 10, 20, 50, or it could contain integers 1, 3, 7, 17, or any other conceivable combination.
- Your function should have running time complexity that is O(amount * number of coin types).
Feel free to use files lists.h and lists.c, but you are not allowed to modify those files. The lists used in your code should be defined and accessed using only the interface in lists.h.
Task 4 (10 pts.)
Consider the following definition of a recurrence:
F(0) = 1.
If N is an integer, N > 0, then: F(N) = max{5+F(N-2), 2*F(N-4)}.
- Is the above definition complete? Does it provide enough base cases to completely specify F(N) for any integer N >= 0? If not, give an example of how the above definition can be completed, by specifying the missing base cases.
- Specify the value of F(N) for each integer N from 0 up to and including 10. If you provided your own base cases for the previous part, your answers here should be consistent with those base cases.
Task 5 (15 pts.)
Consider the following definition of a recurrence:
Base case: F(1) = 1.
If N is an odd integer, N > 1, then: F(N) = F(N+1) + 1.
If N is an even integer, N > 1, then: F(N) = F(N/2) + 3.
- In your answers.xxx document, specify the value of F(N) for each integer N from 1 up to and including 10.
- Create a C file called task5.c, that contains a function
int computeF(int N)
, that computes and returns F(N) for any int N >= 1. Your function should have running time complexity O(N).
Task 6 (15 pts.)
Consider the following definition of a recurrence:
F(0) = 1.
F(1) = 0.
If N is an integer, N > 1, then: F(N) = max{F(N-2)+F(N-1)+10, 2*F(N-2)}.
- In your answers.xxx document, specify the value of F(N) for each integer N from 0 up to and including 10.
- Create a C file called task6.c, that contains a function
int computeF(int N)
, that computes and returns F(N) for any int N >= 0. Your function should have running time complexity O(N).
How to submit
The assignment should be submitted via Blackboard.
Submit a ZIPPED directory called Firstname_Lastname.zip. No other forms of compression are accepted, contact the instructor or TA if you do not know how to produce .zip files. The zipped directory should contain the following documents:
- knapsack_bottom_up.c (your modified version)
- knapsack_top_down.c (your modified version)
- make_change.c
- task5.c
- task6.c
- answers.xxx containing answers to all non-programming questions.
As stated on the course syllabus, programs must be in C or Java, and must run on omega.uta.edu. If you want to do this in any language other than C, you have to re-implement in that language all the code that is currently provided and implemented in C. If you want to use another language or platform, you must obtain prior approval via e-mail by the instructor.
IMPORTANT: Pay close attention to all specifications on this page, including
file names and submission format. Even in cases where your answers are correct,
points will be taken off liberally for non-compliance with the instructions given on this
page (such as wrong file names, wrong compression format for the submitted code,
and so on). The reason is that non-compliance with the instructions makes the
grading process significantly (and unnecessarily) more time consuming. Contact the
instructor or TA if you have any questions.
Back to the list of assignments.