Sign Up

Sign In

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

NPTEL An Introduction To Programming Through C++ Assignment 4 Answers 2022

NPTEL An Introduction To Programming Through C++ Assignment 4 Answers (Week 4)

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.

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. xx <= N

c. xx < N
d. xxx <= N

Answer: b. xx <= N


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. N = N/x


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. cout << N


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: b. If f(x) = 1/x, the answer found by numerical integration is less than the actual integration


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. 1 and 3


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;
	}

OPTIONS=

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

Answer: a. a % b


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 << ’ ‘;
		}
	}

OPTIONS=

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

Answer: d. 11 12 13 14 15


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

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

OPTIONS=

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. Infinite loop


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 are the difference(s) betweWhat is BLANK_Q

a. >
b. <

c. >=
d. <=

Answer: c. >=


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. It can be done if the digits are given most significant to least significant and need to be printed in the same order


NPTEL An Introduction To Programming Through C++ Week 4 Programming Assignment

Q1. 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

PROGRAM:

main_program
{
double k,a1,a2,x;
cin>>k;
a1=k;
a2=(a1+(k/a1))/2;
  while ((a1-a2)>=0.00001)
  {
a1=a2; 
    a2=(a1+(k/a1))/2;
  }
cout.precision(2);

cout <<fixed <<a2 << endl;
}

Q2. 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.

PROGRAM:

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;
}

Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.


Also Available:

NPTEL C++ Assignment 5 Answers

NPTEL An Introduction To Programming Through C++ Assignment 3 Answers

NPTEL Problem Solving Through Programming In C Week 4 assignment answers

Related Posts

Leave a comment

You must login to add a new comment.