NPTEL » An Introduction to Programming Through C++
Programming Assignment 6.1
Due on 2022-03-10, 23:59 IST
Suppose there are two types of blocks: Red blocks (R) of height 1 and Blue blocks (B) of height 4. These blocks can be stacked one above the other to build a tower. You have to write a program that counts the number of distinct towers of a given height n that can be built using these 2 types of blocks.
Note: 2 towers of the same height are called distinct if the order in which R and B blocks occur in them is different.
Hint:
Clearly, to build towers of heights 1, 2, 3, I can use only red blocks, so there is only one way of building a tower of height 1 (R), height 2 (RR), or height 3 (RRR) (the blocks are listed from bottom to top). However, for height 4, there are 2 possibilities: RRRR, and B. Similarly, for height 5, there are 3 possibilities: RRRRR, RB and BR. In general, for
heights h > 3, I can use a recursive strategy. The number of height h towers = number of height h towers in which the bottom block is B + number of height h towers in which the bottom block is R. But now, the number of height h towers in which the bottom block is B = number of height h-3 towers because variation is possible only in the
top h-3 height part. Something similar can be thought of for the number of height h towers in which the bottom block is R.
Write a recursive program that takes n and returns the number of distinct towers of height n.
Programming Assignment 6.2
Due on 2022-03-10, 23:59 IST
Given below is a main program with calls to a function inches. Given a distance measured in yards, feet and inches, the function is supposed to
return the equivalent number of inches. In particular, given y, f, i as the values of the parameters yards, feet, inches, the function should return
y*36+f*12+i. However, it is acceptable to call the function with just two arguments y, f, in which case they should be interpreted as yards and feet,
and the value returned should be y*36+f*12. The function might also be called with a single argument y, in which case it should return y*36. You
have to write the code for the function inches. Your code may implement inches using default values for the parameters or by overloading it. You
do not need to give the main program. It will be added automatically. The main program is given below for your reference.
Note: You don’t need to write/copy the main program. You just need to write the inches function, without the main_program or any of the
header files. Ensure that the function is correctly named as inches.
main_program{
int y,f,i; cin >> y >> f >> i;
cout << inches(y,f,i) <<","<<inches(y,f) <<","<<
inches(y) << endl;
}
The above code (main program) will be there but will not appear in your editor.
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.