NPTEL Week:2 Assignment 2 Answers(Jan 2020-Apr2020) (An Introduction to Programming through C++ )2020

    

               An Introduction To Programming Through C++


IIT Bombay.


Week 2 - Assignment 2:
------------------------------------------------------------------------------------------------------------
1.For each of the following mention whether it is a valid identifier.
a) _x
b) @x
c) x@y
d) x3
e) 3x
------------------------------------------------------------------------------------------------------------
2.To print a message "What is your name?" the proper command is
 a)cout >> "What is your name?";
 b)cout >> 'What is your name?';
 c)cout << "What is your name?";
 d)cout << 'What is your name?';
------------------------------------------------------------------------------------------------------------
What will be printed because of the following code

int x=5;
double xx=5;
cout << 1/2*x << endl; // OUTPUT1
cout << 1/2*xx << endl; // OUTPUT2
cout << x/2 << endl; // OUTPUT3
cout << xx/2 << endl; // OUTPUT4

3.What is OUTPUT1?
Ans:0

4.What is OUTPUT2?
Ans:0

5.What is OUTPUT3?
Ans:2

6.What is OUTPUT4?
Ans:2.5
------------------------------------------------------------------------------------------------------------
7.What is printed because of the following code?

int x = 2;
repeat(4){ x = x*x; }

cout << x << endl;
Ans:65536

8.Based on the previous exercise, how many multiplication operations will be enough for calculating 3^8, i.e. 3 to the power 8?
Ans:3
------------------------------------------------------------------------------------------------------------
9.What will be printed by the following code?

int x=4;
repeat(4){ x = 2 * x + 3; }

cout << x << endl;
Ans:109
------------------------------------------------------------------------------------------------------------
The code below, with the proper initialization of x, y, is supposed to print the sequence 5, 9, 17, 33, 65.

int x = _, y = _;
repeat(5){
   cout << x << endl;
   x = 2*x + y;

}

10.What should x be initialized to?
Ans:5

11.What should y be initialized to?
Ans:-1
------------------------------------------------------------------------------------------------------------
The next few questions are for the program given below.  The program is expected to compute the value of the mathematical constant 'e'.You are to fill in the blanks as per the plan given in the comments

main_program{
        int n; cin >> n;
        double i= BlankA, term = BlankB, result = BlankC;
        repeat(n){// On t-th entry, t=1..n
            // i=t-1, term=1/t!
            // result =1/0!+..+1/(t-1)!
            BlankD
        }
  cout << result << endl;

}

12.What is BlankA?
Ans:0

13.What is BlankB?
Ans:1

14.What is BlankC?
Ans:1
------------------------------------------------------------------------------------------------------------
15.Which of the following will be correct in BlankD?

a)i = i + 1;
term = term / (i+1);
result = result + term;

b)result = result + term;
i = i + 1;
term = term / (i+1);

c)i = i + 1;
result = result + term;
term = term / (i+1);

d)term = term / (i+1);
result = result + term;
i = i + 1;
------------------------------------------------------------------------------------------------------------
Consider the code below for calculating the value of e.  It solves the same problem as discussed in the lecture but it does it differently. In the ith iteration it calculates the value of 1/i! and adds it to the result

main_program{
  int n; cin >> n;

  double result = 0;
  int i=Blank-X;

  repeat(n){
    // calculate 1/i!
    int t=1;
    double term = 1;
    repeat(i){
      term = term/t;
      t = t + 1;
    }
    result = result + term;
    i = i + 1;
  }
  cout << result << endl;


}

16.What should i be initialized to (Blank-X)?
Ans:0

17.How many division operations does the above code do for n=10?
Ans:45

18.How many division operations did the code discussed in the lecture do for n=10?
Ans:10
------------------------------------------------------------------------------------------------------------
19.Give the statement which would enable you to create a rectangle having corners (10,30), (50,30), (10,80), (50,80).  The rectangle should be given the name "r1".
Ans:30,55,40,50 (NOT SURE).

20.Give the command that would rotate the above rectangle right by 5 degrees about its center.
Ansr1.right(5);
------------------------------------------------------------------------------------------------------------
Fill in the blanks in the following code which is intended for drawing
5 circles of radius 50 pixels centered at points (100,100), (150,100),
(200,100), (250,100), (300,100).

initCanvas();
double x=100;
repeat(5){
  Circle c(x,BLANK-P,BLANK-Q);
  c.imprint();
  x = x + BLANK-R;

}

21.What is BLANK-P?

Use as few spaces as possible
Ans:100

22.What is BLANK-Q?
Ans:50

23.What is BLANK-R?
Ans:50

COMMENT FOR OTHER PROGRAMMING ASSIGNMENTS SOLUTIONS.
WEEK 2: PROGRAMMING ASSIGNMENT 1 SOLUTION
CLICK ON BELOW LINK.
<<CLICK HERE FOR  PROGRAMMING ASSIGNMENT 2 MCQS WEEK 2>> <<CLICK HERE FOR  QUIZ MCQS WEEK 3>>

FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.

Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages