# An Introduction To Programming Through C++ Assignment 4 Answers

NPTEL An Introduction To Programming Through C++ Assignment 4 Answers 2022:-In This article, we have provided the answers of An Introduction To Programming Through C++ Assignment 4. You must submit your assignment to your own knowledge.

## What is An Introduction to Programming Through C++?

This course provides an introduction to problem-solving and programming using the C++ programming language. The topics include:

• Basic programming notions. Control flow, variables and assignments statements, conditional execution, looping, function calls including recursion. Arrays and structures. Elementary aspects of classes. Heap memory.
• Program design. How human beings solve problems manually. Strategies for translating manual strategies to computer programs. Organizing large programs into units such as functions and classes. Introduction to assertions and invariants.
• Programming applications. Arithmetic on polynomials, matrices. Root finding. Sorting and searching. Design of editors and simulators, including graphical editors. Elementary animation. A rudimentary graphics system will be discussed.
• Standard Library of C++. The string, vector and map classes.

## CRITERIA TO GET A CERTIFICATE

Average assignment score = 25% of the average of best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF THE AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met, you will not get the certificate even if the Final score >= 40/100.

## NPTEL An Introduction to Programming Through C++ Assignment 4 Answers 2022:-

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):

1. x < 17
2. x*x <= N
3. x*x < N
4. xxx <= N

Answer:- 2.x*x <= 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):

1. N = N – 1
2. N = N*x
3. N = N/x
4. N = N%x

Answer:- 3.N = N/x

πFOR NEXT WEEK ASSIGNMENT ANSWERSπ

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

1. cout << βprimeβ
2. cout << N-1
3. cout << N
4. cout << x

Answer:- 3. 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:

1. If f(x) = 5-x, the answer found by numerical integration is less than the actual integration
2. If f(x) = 1/x, the answer found by numerical integration is less than the actual integration
3. If f(x) = 3xΒ³, the answer found by numerical integration is less than the actual integration
4. None of these

Answer:- 2.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:

• For f(x), xn = (Β½)n
• For g(x), xn = (β)n
• x2 is closer to the root for f(x) than for g(x).
• x2 is closer to the root for g(x) than for f(x).

Which of these statements are true:

1. 1 and 3
2. 2 and 3
3. 1 and 4
4. 2 and 4

Answer:- 1. 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=

1. a % b
2. a β b
3. a / b
4. none of the others

Answer:- 1. 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:

1. Compile error
2. 10 10 10 10 10
3. 11 13 15 17 19
4. 11 12 13 14 15

Answer:- 4. 11 12 13 14 15

πFOR NEXT WEEK 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-- << β β;
}
}``````

OPTIONS:

1. 9 8 7 6 5 4 3 2 1
2. 9 7 5 3 1
3. Infinite loop
4. None of the above

Answer:- 3. 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 basedon 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

1. >
2. <
3. >=
4. <=

Answer:- 3. >=

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?

1. It cannot be done
2. It can be done if the digits are given least significant to most significant and need to be printed in the same order.
3. It can be done if the digits are given most significant to least significant and need to be printed in the same order
4. It can be done if the digits are given in any order and need to be printed in the same order.

Answer:- 3. It can be done if the digits are given most significant to least significant and need to be printed in the same order

## 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

Code:-

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

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

πFOR NEXT WEEK ASSIGNMENT ANSWERSπ

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

An Introduction To Programming Through C++ Assignment 4 Answers 2022:-In This article, we have provided the answers of An Introduction To Programming Through C++ Assignment 4 You must submit your assignment to your own knowledge.