gs gs122 Dynamic Programming - Rod Cutting - Quiz No.1
gs gs122 Data Communication and Computer Network Quiz
This quiz belongs to book/course code gs gs122 Data Communication and Computer Network of gs organization. We have 268 quizzes available related to the book/course Data Communication and Computer Network. This quiz has a total of 14 multiple choice questions (MCQs) to prepare and belongs to topic Dynamic Programming. NVAEducation wants its users to help them learn in an easy way. For that purpose, you are free to prepare online MCQs and quizzes.
NVAEducation also facilitates users to contribute in online competitions with other students to make a challenging situation to learn in a creative way. You can create one to one, and group competition on an topic of a book/course code. Also on NVAEducation you can get certifications by passing the online quiz test.
#include<stdio.h> #include<limits.h> int max_of_two(int a, int b) { if(a > b) return a; return b; } int rod_cut(int *prices, int len) { int max_price = INT_MIN; // INT_MIN is the min value an integer can take int i; if(len <= 0 ) return 0; for(i = 0; i < len; i++) max_price = max_of_two(_____________); // subtract 1 because index starts from 0 return max_price; } int main() { int prices[]={2, 5, 6, 9, 9, 17, 17, 18, 20, 22},len_of_rod = 10; int ans = rod_cut(prices, len_of_rod); printf("%d",ans); return 0; }
Complete the above code.
#include<stdio.h> #include<limits.h> int rod_cut(int *prices, int len) { int max_val[len + 1]; int i,j,tmp_price,tmp_idx; max_val[0] = 0; for(i = 1; i <= len; i++) { int tmp_max = INT_MIN; // minimum value an integer can hold for(j = 1; j <= i; j++) { tmp_idx = i - j; //subtract 1 because index of prices starts from 0 tmp_price = _____________; if(tmp_price > tmp_max) tmp_max = tmp_price; } max_val[i] = tmp_max; } return max_val[len]; } int main() { int prices[]={2, 5, 6, 9, 9, 17, 17, 18, 20, 22},len_of_rod = 5; int ans = rod_cut(prices, len_of_rod); printf("%d",ans); return 0; }
Which line will complete the ABOVE code?