0% found this document useful (0 votes)
64 views3 pages

Lab4 ReadMe PDF

This document outlines 5 programming assignments for an introduction to programming course. The assignments cover: 1) calculating perimeter of rectangles and implementing as a function, 2) summing integers and implementing as a function, 3) left-rotating strings, 4) checking bits in integers and generating all subsets of a string, 5) calculating effective resistance in a step-ladder resistor circuit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Lab4 ReadMe PDF

This document outlines 5 programming assignments for an introduction to programming course. The assignments cover: 1) calculating perimeter of rectangles and implementing as a function, 2) summing integers and implementing as a function, 3) left-rotating strings, 4) checking bits in integers and generating all subsets of a string, 5) calculating effective resistance in a step-ladder resistor circuit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Four

ID1303: Introduction to Programming

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

You might also like