Lab4 ReadMe PDF
Lab4 ReadMe PDF
1. (a) Write a program to accept two integers m and n and display the perimeter of a
m × n rectangle.
Example run:
Enter the value of m: 4
Enter the value of n: 7
* * * * * * *
* *
* *
* * * * * * *
(b) Implement the above as a function.
2. (a) Write a program that accepts a positive integer n and a sequence of n numbers
from the user and prints their sum. (b) Implement the above as a function.
3. Write a program that accepts a string and a number k and prints the string left-rotated
k times.
Example runs:
Enter the string: APPLE
Enter the number of left-rotations: 1
The rotated string is: PPLEA
Enter the string: CHROME
Enter the number of left-rotations: 4
The rotated string is: MECHRO
4. (a) Write a function that accepts a positive integer n and a position k and checks if
the kth bit of n (from right) is equal to 1.
Example runs:
Enter the number: 5
Enter the position: 2
The bit is zero.
1
Enter the number: 5
Enter the position: 3
The bit is one.
[Hint: Method 1: AND with 2k−1 ; Method 2: Right-shift k − 1 times.]
(b) Accept a string as input and print all subsets of the string (in any order). Hint:
Let the length of the string be n. Map the subsets to numbers in {0, 1, . . . , 2n − 1}
as shown in class and for each of these numbers, print the appropriate characters,
possibly using the function in part (a).
Example run:
Enter the string: CAT
T
A
TA
C
TC
AC
TAC
5. A step-ladder arrangement of resistors is shown below.
a1 a2 an
A
b1 b2 ... bn−1 bn
Write a program which accepts the the values of the resistances (ai s and bi s as in the
picture) in two arrays, and prints the effective resistance between A and B. Assume
that all values are in some common unit, eg: ohms.
Hint: Update the value of the effective resistance from right-to-left as shown below.
2
ai
rnew
≡ bi rold