Section 8: Part A: Tutorial Problems For Practice
Section 8: Part A: Tutorial Problems For Practice
Part‐A: Tutorial Problems for Practice
Instructions:
Draw flowchart and or algorithm depicting how to solve each problem in this part.
Submit your work latest by today. Please clearly write your name, roll number and mobile
number on the front page of the sheets.
Create a sub directory named as LabT6 under your home directory.
Give the name of the programs as <R> <T>1.c, <R> <T>2.c, .. etc. for the tutorial problems
problem 1, 2….., respectively. Here <R> implies your Registration No.
Store all the programs under this tutorial part in the directory LabT6
1. ------------------------------------------------------------
while (n > 0) {
if (n % 2 == 1) ++t;
n = n / 2;
}
return t;
}
2. ---------------------------------------------------------------
while (n > 0) {
if (n & 1) ++t;
n >>= 1;
}
return t;
}
3. -----------------------------------------------------------------
s = 0;
t = 1;
while (n > 0) {
s += t;
t *= a;
--n;
}
return s;
}
4. ---------------------------------------------------------
s = t = 0;
for (i=0; i<n; ++i) {
s += A[i];
t += A[i] * A[i];
}
return (t/n)-(s/n)*(s/n);
}
[Hint: First try to have the answer of your own and then verify your answer by writing
appropriate programs for each problems. If you need help, ask the TAs.]
Part‐B: Assignment Problems
Instructions:
Create a sub directory named as LabA6.
Give the name of the programs as <R> <A2>1.c, <R> <A2>2.c, …, etc. for the tutorial
problems 1, 2….., respectively. Here <R> implies your Registration No.
Store all the programs under this tutorial part in the directory LabA6.
You should upload your programs to the Moodle course web page. Preferably in ZIP form
latest by 12:45 hrs. today only.
For each of the above functions write a main program to read the input to the function
and print the output of the function for that input.
2. Two rectangles are said to overlap if there exists a common point lying inside or on the
boundary of both rectangles. Assume that all rectangles have edges parallel to the x and y
axes.
Fig 1(a) shows a case where all pairs of rectangles overlap. In Fig 1(b). the shaded
rectangles do not overlap. Both figures contain 3 rectangles.
a) Write a C function overlap(x1, y1, x2, y2, a1, b1, a2, b2) that receives the diagonally
opposite corner points of two rectangles as arguments such that (x1, y1) and (x2, y2) are
the two diagonally opposite corner points of one rectangle, while (a1, b1) and (a2, b2) are
the two diagonally opposite corner points of the other rectangle. The function returns 1 if
the rectangles overlap, otherwise 0.
b) Write a main( ) program that reads the value of an integer N and then reads in the
diagonally opposite corner points of N rectangles. Assume N < 10. The coordinates are
stored in four arrays, X1[ ], Y1[ ], X2[ ], Y2[ ]. The coordinates (X1[k], Y1[k]) and
(X2[k], Y2[k]) represent the corner points of the kth rectangle. Your program must then
use the function overlap(…) to determine whether all pairs of rectangles overlap. If not,
then it must print the corner points of every distinct pair of rectangles which do not
overlap.
[Submit your solutions to Moodle course management system, latest by 12:45 hrs. toady.]