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 LabT7
1. What does the following program return?
if (n == 0) return 0;
t = foo6(A,n-1);
if ( ((A[n-1]>='a') && (A[n-1]<='z')) ||
((A[n-1]>='A') && (A[n-1]<='Z')) ||
((A[n-1]>='0') && (A[n-1]<='9')) )
++t;
return t;
}
2. A rational number is defined by a pair of integers (a,b) with b > 0 and is interpreted to
stand for a/b. A rational number a/b is said to be in the reduced form if gcd(a,b)=1.
4. A circle in the X-Y plane is specified by three real numbers a,b,c. The real numbers may be
interpreted in two possible ways. The first possibility is that (a,b) represents the center and c the
radius of the circle. In the second representation, we refer to the equation of the circle as:
X2 + Y2 + aX + bY + c = 0.
So a structure holding three double variables together with a flag indicating the particular
interpretation suffices to store a circle.
a. Write a function that converts a circle structure from the first to the second
representation.
b. Write a function that converts a circle structure from the second to the first
representation.
c. Write a function that, upon input a circle and two real numbers x,y, checks whether the
point (x,y) lies inside, on or outside the circle. Note that the input circle may be of any of
the two representations.
d. Write a function that, upon input two circles each with any representation, determines
whether the circles touch, intersect or do not intersect.
e. Write a function that, upon input a circle in any representation, returns the side of a
square that has the same area as the input circle.
[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 LabA7.
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 LabA7.
You should upload your programs to the Moodle course web page. Preferably in ZIP form
latest by 12:45 hrs. today only.
1. Write a recursive function that computes the binomial coefficient C(n,r) using the inductive
definition:
C(n,r) = C(n‐1,r) + C(n‐1,r‐1)
for suitable values of n and r. Supply appropriate boundary conditions.
2. Write a Function to recursively compute the harmonic mean of an array of numbers. In the
main function, create an array of size 10. Input integers from the user till a negative number is
given as input or the 10 elements have been filled up. Find the harmonic mean of the elements
of this array.
3. Write a function to recursively compute the sum of digits of a positive integer. The function
has to be recursive. Function Protype : int sum(int n);
4. A rectangle in the X‐Y plane can be specified by eight real numbers representing the
coordinates of its four corners. Define a structure to represent a rectangle using eight double
variables. Notice that here we do not assume the sides of a rectangle to be necessarily parallel
to the X and Y axes. Notice also that by a rectangle we mean only the boundary (not including
the region inside).
a. Write a function that, upon input a structure of the above kind, determines whether the
structure represents a valid rectangle.
b. Write a function that, upon input a valid rectangle, determines the area of the
rectangle.
c. Write a function that, upon input a valid rectangle and two real numbers x, y,
determines whether the point (x,y) lies inside, on or outside the rectangle.
5. Define a structure customer to specify data of customer in a bank. The data to be stored is:
Account number (integer), Name (character string having at most 50 characters), and Balance in
account (integer).
Assume data for all the 10 customers of the bank are stored in the structure array :
The function, transaction, is used to perform a customer request for withdrawal or deposit to
her account. Every such request is represented by the following three quantities: Account
number of the customer, request type (0 for deposit and 1 for withdrawal) and amount. The
function prototype is as follows:
The transaction function returns 0 if the transaction fails and 1 otherwise. The transaction fails
only when the account balance is less than the withdrawal amount requested.
The array bank (defined above) is another input to the transaction function and is suitably
updated after every request. In case of a failed transaction no change is made in the bank array.
Write a main() function which populates the array bank with values for 5 customers. Also, the
main() should take a withdrawal request from the user (i.e., read values for account number,
amount), and call the transaction function, and thereby print if it is a valid transaction. If valid,
it should print the balance after the withdrawal.
[Submit your solutions to Moodle course management system, latest by 12:55 hrs. toady.]