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 4 Programming Assignment | Jan-2022 | NPTEL

 NPTEL » An Introduction to Programming Through C++



Programming Assignment 4.1

Due on 2022-02-24, 23:59 IST

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


Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
main_program{
3
  double k;
4
  cin>>k;
5
  double an=k;
6
  double an1=(an + (k/an))/2 ;
7
  while(abs(an1-an)>1e-5){
8
    an=an1;
9
    an1=(an1 + k/an1)/2;
10
  }
11
12
  cout.precision(2);
13
  cout << fixed << an1 << endl;
14
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2.00
1.41\n
1.41\n
Passed
Test Case 2
3.00
1.73\n
1.73\n
Passed
Test Case 3
74.50
8.63\n
8.63\n
Passed
Test Case 4
9.55
3.09\n
3.09\n
Passed



Private Test cases used for Evaluation
Status
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed



Programming Assignment 4.2

Due on 2022-02-24, 23:59 IST

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.


INPUT


### The next n lines contain a char value each

c0

c1

...

cn

n20 . ciis the outcome of the ithgame. It can take a value of either A/B. A indicates that this game was won by Player A and B indicates that it was won by Player B. The input will end when a player has won according to the given rules or 20 characters have been given.


OUTPUT

At the end of the input, if player A wins, output “ A”, If player B wins output “B”. If no one has won, output “Tie”

Note: DO NOT output the quotes

Please print a newline( using “cout<<endl;” is one way to do it) after printing your answer



Just scroll your mouse wheel over this code or swipe on this code snippet to scroll down to see the remaining code.

Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
main_program {
3
  int acount=0, bcount=0; char c;
4
  bool flag=true;
5
  for(int i=1;i<=20;++i)
6
  {
7
    cin>>c;
8
    if(c=='A')
9
        {
10
            ++acount;
11
        }
12
    else if(c=='B')
13
        {
14
            ++bcount;
15
        }
16
    if( acount>=5 && (acount-bcount>=2))
17
        {   cout<<'A'<<endl;
18
            flag=false;
19
            break;  }
20
    else if( bcount>=5 && (bcount-acount>=2))
21
        {   cout<<'B'<<endl;
22
            flag=false;
23
            break;  }
24
  }
25
    if(flag)
26
    cout<<"Tie"<<endl;
27
}
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
A
A
A
A
A
A\n
A\n
Passed
Test Case 2
A
B
B
B
B
A
A
A
B
B
B\n
B\n
Passed
Test Case 3
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
Tie\n
Tie\n
Passed

 



Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed

Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !

2 comments:

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.