NPTEL » An Introduction to Programming Through C++
Programming Assignment 4.1
Due on 2022-02-24, 23:59 ISTIn 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
Public Test Cases Input Expected Output Actual Output Status 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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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 ISTWrite 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.
Public Test Cases Input Expected Output Actual Output Status 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
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
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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 Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Test Case 5 | Passed |
Very Helpful, Thanks.
ReplyDeleteGlad ! It helped You.
Delete