An Introduction to Programming Through C++ | NPTEL 2022 | Week 4 Answers

This set of MCQ(multiple choice questions) focuses on the An Introduction to Programming Through C++ NPTEL 2022 Week 4 Answers.

Course layout

Answers COMING SOON! Kindly Wait!

NOTE: You can check your answer immediately by clicking show answer button. An Introduction to Programming Through C++ NPTEL 2022 Week 4 Assignment Answers” contains 12 questions.

Now, start attempting the quiz.

An Introduction to Programming Through C++ NPTEL 2022 Week 4 Assignment Answers

Given an integer N >=2, we need to output all its prime factors in increasing order. To do this, we exploit the fact that if N is composite, it has a prime factor that is <= √N. If it is prime, then N itself is the only prime factor of itself. 
Eg:
Input: N=12
Output: 2 2 3
Input: N=17
Output: 17

int N;

    cin >> N;

    for(int x=2; blank1 ; x++){

        while (N%x == 0) 

        {

        // We print out x as long as N remains divisible by x

            cout << x << “ “;

            blank2 ;

        }        

    }

    if(N>1){

    // The case when N is prime

        blank3 ;

    }

Q1. What would be blank1 (we need to check division by numbers upto what value of x):

a) x < 17
b) x*x <= N
c) x*x < N
d) x*x*x <= N

Answer: b)

Q2. What would be blank2 (if N is divisible by x, then we print x as a factor and then must remove it from N):

a) N = N – 1
b) N = N*x
c) N = N/x
d) N = N%x

Answer: c)

Q3. What would be blank3 (this case occurs when N is prime):

a) cout << “prime”
b) cout << N-1
c) cout << N
d) cout << x

Answer: c)

Q4. The numerical integration program discussed in the lecture estimates the area under the curve f(x) between p+iw and p+(i+1)w by f(p+iw)*w. Which of these is correct:

a) If f(x) = 5-x, the answer found by numerical integration is less than the actual integration
b) If f(x) = 1/x, the answer found by numerical integration is less than the actual integration
c) If f(x) = 3x³, the answer found by numerical integration is less than the actual integration
d) None of these

Answer: c)

Q5. We know that the roots of the functions f(x) = x² and g(x) = x⁴ are x = 0. However, we decide to employ the Newton-Raphson method to estimate their roots. We begin with x0 = 1 for both these functions. Consider the following statements:

  1. For f(x), xn = (½)n
  2. For g(x), xn = (⅔)n
  3. x2 is closer to the root for f(x) than for g(x).
  4. x2 is closer to the root for g(x) than for f(x).

Which of these statements are true:

a) 1 and 3
b) 2 and 3
c) 1 and 4
d) 2 and 4

Answer: a)

An Introduction to Programming Through C++ NPTEL Week 4 Assignment Answers

Q6. What does the following program output for any given positive integers ‘a’ and ‘b’?

main_program {
			int a; int b;
			cin >> a >> b;
			while(a – b >= 0) {
				a = a – b;
			}
			
			cout << a;
	}

a) a % b
b) a – b
c) a / b
d) none of the others

Answer: a)

Q7. What is the output of the following code snippet?

main_program()
	{
		int k, j;
		
		for(k=1, j=10; k <= 5; k++)
		{
			cout << k+j << ’ ‘;
		}
	}

a) Compile error
b) 10 10 10 10 10
c) 11 13 15 17 19
d) 11 12 13 14 15

Answer: d)

An Introduction to Programming Through C++ NPTEL Week 4 Assignment Answers

Q8. What is the output of the following code snippet?

main_program()
	{
		int k, j;
	
		for(k=9; k!=0; k–-)
		{
			cout << k-- << ‘ ‘;
		}
	}

a) 9 8 7 6 5 4 3 2 1
b) 9 7 5 3 1
c) Infinite loop
d) None of the above

Answer: c)

Given below is the code to find the number of digits in the binary representation (base 2)of a given number x, without leading zeros (x>0). Answer the following questions based on this.

main_program{
int x;
cin >> x;
int d = 0, n=BLANK_P;
while(x BLANK_Q n){
d++;
n *= BLANK_R;
}
cout<<d<<endl;

Q9. What is BLANK_P (Integer answer)

Answer: 1

Q10. What is BLANK_Q

a) >
b) <
c) >=
d) <=

Answer: c)

An Introduction to Programming Through C++ NPTEL week 4 Assignment Solutions

Q11. What is BLANK_R (Integer answer)

Answer: 2

Q12. Suppose you are given a 1000 digit number(N) and without storing all the digits of either the number or the quotient we want to find the quotient when N is divided by another given number (small enough to be stored, say p). Which of the following is true?

a) It cannot be done
b) It can be done if the digits are given least significant to most significant and need to be printed in the same order.
c) It can be done if the digits are given most significant to least significant and need to be printed in the same order
d) It can be done if the digits are given in any order and need to be printed in the same order.

Answer: c

An Introduction to Programming Through C++ NPTEL Week 4 Programming Assignment Answers

Programming Assignment 4.1

In continuation of the topic of computing mathematical functions explored in the lectures, we see another method to find square roots.
Suppose we wish to find the square root of some k > 0. Consider the sequence (a0, a1, a2…)
defined by    
    a0 = k
    an+1 = (an + (k/an))/2 for n >= 0
It can be shown that as n increases, the an converges to the square root of k . Write a program that takes as input a double k, and computes its square root using this method. Compute the value of an till (an – an-1 < 1e-5) and then report an correct to 2 decimal places.
Note: Start writing the program directly from main_program. To print a double x correct to 2 decimal places, use
    cout.precision(2);
cout << fixed << x << endl;

INPUT
k (2 <= k <= 100, of type double) 

OUTPUT
The square root of k correct to two decimal places

CODE:

main_program {
  
  double k, a0, a1;
  cin >> k;
  a0 = k;
  a1 = (a0 + (k/a0)) / 2;
  while((a0-a1) >= 0.00001) {
    a0 = a1;
    a1 = (a0 + (k/a0)) / 2;
  }
  cout.precision(2);
  cout << fixed << a1 << endl;
}

Programming Assignment 4.2

Write a program to keep track of a match consisting of a series of games between two people: player A and player B, and report the outcome. The input consists of a sequence of letters A or B.  If the input is A, it indicates that A has won a game.  If it is B, then it indicates B has won a game.  The first player to win 5 or more games with a difference of 2 or more games between him and his opponent wins the match. If no player wins the match in 20 games then the match is declared a tie after these 20 games have been played.

CODE:

main_program {
  
  char ch;
  int A=0, B=0, count=1;
  
  while(count <= 20) {
    cin >> ch;
    switch(ch) {
      case 'A': A++;
                break;
      case 'B': B++;
                break;
    }
    count++;
  }
  
  if(A==B)
    cout << "Tie" << endl;
  else if((A-B) > 1)
    cout << "A" << endl;
  else
    cout << "B" << endl;
    
}

<< Prev- An Introduction to Programming Through C++ Week 3 Solutions

>> Next- An Introduction to Programming Through C++ Week 5 Solutions


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

Programming in Java NPTEL week 1 quiz answers

NPTEL – Python for Data Science assignment solutions

Nptel – Deep Learning assignment solutions

Social Networks nptel assignment answers

NPTEL answers: Programming in Modern C++

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *