Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

*****NEED THE CODE IN C**** #include #include #define P_LENGTH 20 int p[] = {0,1,3,4,6,10,9,13,16,14,17,23,20,21,28,27,24,34,25,25,24}; /* Helper function to display arrays */ void display_int_array(int a[], int

image text in transcribed*****NEED THE CODE IN C****

#include  #include  #define P_LENGTH 20 int p[] = {0,1,3,4,6,10,9,13,16,14,17,23,20,21,28,27,24,34,25,25,24}; /* Helper function to display arrays */ void display_int_array(int a[], int length) { for (int i=1; i  Problem 2 (1 pt). Attached is a file rod.c that has a program that solves the rod cutting problem discussed in Part 1 of the lectures on dynamic programming. There is a rod.c. It has a function total_price that determines the optimal way to cut a rod into pieces to maximize profit. The profit is assumed to be the sum of the prices of the pieces. Function total-price calls a recursive function total_price_rec which is implemented by top-down dynamic programming with memoization. The memo array is memo[ ]. The program will run on a rod of length 20 feet and with the set of prices p[], where p[k] is the price of a rod of length k. For example, suppose the rod was cut to make lengths of 2,8 , and 10 , and p[2]=3,p[8]=16, and p[0]=17 then the sum of the prices is 3+16+17=36. Function total_price( ) will find the maximum sum of prices as well as the corresponding cuts. The program should have the following output: Given a rod of length 20 , the max price by cutting the rod: 40 Rod length :1234567891011121314151617181920 Cut price :134610111316172023242628303334363940 Rod lengths of optimal solution: 1811 The data in the table is according to the length of a rod. Uncut price is the price of a rod at the length. Cut price is the maximum price for a rod by cutting it up and selling the pieces. First cut corresponds to how you would optimally cut a rod, and it's the length of the first cut. The last line, which is highlighted in yellow, indicates that the optimal cuts have length 1,8 , and 11 . Then the cuts occur at 1 foot, 9 feet, and 20 feet. (Note that a cut at 20 feet doesn't do anything since the original rod is 20 feet.) According to the prices of rod lengths, a rod of length 1 has price 1 , a rod of length 8 has price 16 , and a rod of length 11 has price 23. This adds up to 40, which is the maximum. Modify the program so that it computes and displays these cuts as shown above. Upload it into laulima. There are two functions to modify: total_price_rec and total_price. The first function can use the array cut[]. The second function should display what's shown in yellow

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Detailed note on the contributions of F.W.Taylor

Answered: 1 week ago