Вы находитесь на странице: 1из 2

Programming & Data Structures Tutorial 4

February 05, 2010 Total marks = 60 Give meaningful comments to explain the functionality of the method used in your program All Questions carry equal marks Programs without comments will be evaluated to zero marks On non-recursive and recursive Functions Submit problems 2 & 7 in the class, submit the rest on February 12, 2010 1. (a) Write a non-recursive C function which takes two unsigned integers and nds their greatest common divisor (gcd) using the following denition gcd (x, y) = y, if x = 0, = gcd (x % y, y), if x > y, = gcd (y % x, x), if y > x (b) Write a main program which takes two unsigned integers and prints their gcd and lcm (the least common multiple) which are computed using the above function. [5] 2. It is required to evaluate an , where n is non-negative integer, using the observation that
k1 k1 n = i=0 bi .2i and the consequent equality an = a( i=0 bi .2 ) = i=0 a(2 .bi ) . In other i words, the scheme consists in multiplying to the partial product the terms a2 , i 0 provided bi is 1. Write a non-recursive C function for computing an using the above denition. Write a C main function which reads an integer a and a non-negative number n, invokes the function to obtain an and prints it. [10]
k1 i i

3. Two non-negative numbers n1 and n2 are said to constitute an amicable pair if the sum of factors of n1 including 1 and excluding itself equals n2 and vice-versa. For example 220 and 284 are amicable pairs. (a) Write a C function sumFactors which takes a positive number n as its only argument and returns the sum of ns factors including 1 and excluding itself. (b) Write a C function isAmicable which takes two positive numbers n1 and n2 as its arguments and returns 1 if they constitute an amicable pair; otherwise, it returns 0. It is to use the function sumFactors for this purpose. (c) Write a C main function which reads two positive numbers m and n prints proper messages depending upon whether they are amicable or not. [10] 4. Write a C function which takes a oat variable x, 0 < x < 1, and a non-negative number precision as its parameters. It computes eciently and returns the sum of the following series up to the specied precision, that is, the error is restricted to 0.510precision : x2 x3 x [10] 1 2 + 3 (ln(1 + x) series) 1

5. Converting any number in a given base to its decimal equivalent (a) Write a C function which i. reads a base b, where 1 < b 16 it is not needed to check this range in the function code, ii. reads the digits of a positive number n to the base b as characters, iii. echoes back the characters in one line, and iv. returns the equivalent of this number in decimal when EOF (ctrl-D) keypress is read. Assume that for all values of b, the digits are the same as the hexadecimal digits. Thus, for b = 12, the digits are {0, , 9, A, B}. The function should use neither the pow function nor any loop to compute any power term, if needed. Hint: Corresponding to each character (digit) read, the program has to update the equivalent of the number obtained so far. (b) Write a C main function which invokes the function and then prints the decimal equivalent returned by the function. [10] 6. Give the recursive denition of the following (mathematical) functions and hence write the recursive C functions for them: (a) no of digits(n) = Number of digits of n, n 0 (b) sum of digits(n) = sum of digits of n, n 0. [5] 7. Consider the following denition of an , n 0. an = 1, for n = 0, = an/2 .an/2 , for n even = a.an/2 .an/2 , for n odd. Write a non-recursive C function for computing an using the above denition. Write a C main function which reads an integer a and a non-negative number n, invokes the function to obtain an and prints it. Can you identify that the non-recursive formulation of the computation of an as given in problem 2 is the same as the computation represented by the recursive denition given in this problem? Assess how many time the respective functions are called. [5] 8. Write a recursive C function which takes a positive decimal number n and prints its hexadecimal equivalent in a single line without using any array. [5]

Вам также может понравиться