Skip to main content

Posts

Showing posts from February, 2022

An Introduction To Programming Through C++ || NPTEL Week-5 Programming Assignment 5.

  Write a function that takes a number  b , and a number   N , and returns true/false indicating whether N is a power of b, ie b k =N for some non negative integer k. The function returns true if N can be expressed as a power of   b , and false otherwise. Use the following function signature : bool isPower(int b,int N). The following code will be there, but won’t appear in your editor : main_program{ int b,N; cin>>b>>N; if(isPower(b,N)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } This invisible code will test your function. This code will read the values  b  and  N  from the test case and print out “Yes” if your function returns true and “No” otherwise. You  don’t  need to write/copy the main program. You just need to write the  isPower  function, without the main_program or any of the header files. Note :  Ensure that the name of the function is  isPower  and it takes the two parameters and returns true or false without printing

An Introduction To Programming Through C++ || NPTEL Week-4 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. INPUT ### The next n lines contain a char value eac h c 0 c 1 ... c n n 20 . c i is the outcome of the i th game. 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 “T

An Introduction To Programming Through C++ || NPTEL Week-4 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 (a 0 , a 1 , a 2 ...) defined by         a 0 = k     a n+1 = (a n + (k/a n ))/2 for n >= 0 It can be shown that as n increases, the a n 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 a n till (a n - a n-1 < 1e-5) and then report a n 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 SOLUTION:  int main() {     double k, y, x;     cin >> k;     if (k < 2 || k > 100)     {         ex

An Introduction To Programming Through C++ || NPTEL Week-3 Programming Assignment 3.2

  Write a program to find the sum of the digits of a given integer N. Solving this problem is easy manually if the number is given to you on paper: you simply see the digits written down and you can add them up.  A computer program does not “see” the digits.  However, you can get the least significant digit by taking the remainder modulo 10, using the % operator.  You can determine the number resulting from erasing the least significant digit of an integer x by dividing x by 10.  As you know x/10 will equal the quotient, which is exactly what remains if you erase the last digit of x.  If you now take the remainder of this modulo 10 you will get the second least significant digit.  Thus by alternately taking the remainder modulo 10 and dividing by 10 you will be able to obtain the different digits.  So then you can add them up.  All that remains is to put this into a nice loop. INPUT: N (1<=N<=1000000000).  The upper bound for N is set at 10 9 so that the numbers will fit in stan

An Introduction To Programming Through C++ || NPTEL Week-3 Programming Assignment 3.1

  You are currently at the origin (0, 0) and will be given commands to either go Right (R), Left (L), Up (U) or Down (D) by a certain number of steps. At the end of all these commands, you will be signaled to stop by reading the character ‘E’, after which you need to output your position in the x-y plane. The four kinds of movements are the following (direction followed by number of steps in that direction): R number_of_steps : You need to increase your x-coordinate by “number_of_steps”. L number_of_steps : You need to decrease your x-coordinate by “number_of_steps”. U number_of_steps : You need to increase your y-coordinate by “number_of_steps”. D number_of_steps : You need to decrease your y-coordinate by “number_of_steps”. INPUT: Direction number_of_steps (a character and integer separated by a space) . . .  E (command to stop) OUTPUT: x y SOLUTION: int main() { char U, D, R, L, E; int x = 0, y = 0; int steps; char direction; while (1) { cin >&g