Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

An Introduction to Programming Through C++ Week 6 programs

  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.
Your last recorded submission was on 2022-03-09, 00:29 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
#include<iostream>
using namespace std;
 
long int f(int n){
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 1;
if (n == 3) return 1;
else return f(n-1) + f(n-4);
}
 
int main()
{
  int n;
  cin>>n;
  cout<<f(n)<<endl;
  return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   




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.
Your last recorded submission was on 2022-03-09, 00:31 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
int inches(int y,int f=0,int i=0)
{
  return y*36+f*12+i;
}
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   





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.