Docsity

Computer Science I - Lab session: Recursive Functions II | COP 3502, Lab Reports of Computer Science

Toggle sidebar
bg1
Lab session : Recursive functions II
1. Write a recursive algorithm called PrintOdd, that prints only the odd numbers from
1 to n in increasing order. The value of n may be either even or odd.
2.. Given a global array of numbers X[1..n], you are to write a recursive function
SumEven that will return the sum of the even array elements in the range specified by
the parameters to the function. Assume that the array was already initialized and that the
SumEven function will be called as shown below and answer will be equal to the sum of
the even elements in the array X in between the indices of m and n, inclusive. Assume
that m <= n for each function call. Also, if there are no even elements in the array in the
specified range, your function should return 0.
SumEven(X,m, n)
3. Write a recursive function, called prob, that will correctly print the index positions of
all the occurrences of a specified character c, within the first m locations of an array
X. You may assume that X is a global array which includes locations that range
from 1 to n and is already populated with characters. Assume that m ๏‚ฃ n. The initial
call is prob(X,c, m).
4. Write a recursive function to count number of odd integers in the array dd
containing k elements.
5. The Binomial function C(n,k) is usually defined in terms of factorials as follows:
C(n,k) = n ! / ( k! (n โ€“ k )! )
This involves lot of computations as it needs to find factorials of 3 numbers.
It can be shown that it is also possible to compute C(n,k) through simpler functions:
C(n,k) = C(n โ€“ 1, k โ€“ 1 ) + C(n โ€“ 1, k )
C(n,n ) = 1
C(n,1) = 1
Write a recursive function to compute C(n,k) without using factorials.
6.Write a recursive function MaxArray that will return the largest array element in the
array A of size n.
Solution Q 1
pf2

Partial preview of the text

Download Computer Science I - Lab session: Recursive Functions II | COP 3502 and more Lab Reports Computer Science in PDF only on Docsity!

Lab session : Recursive functions II

1. Write a recursive algorithm called PrintOdd , that prints only the odd numbers from 1 to n in increasing order. The value of n may be either even or odd. 2.. Given a global array of numbers X[1..n] , you are to write a recursive function SumEven that will return the sum of the even array elements in the range specified by the parameters to the function. Assume that the array was already initialized and that the SumEven function will be called as shown below and answer will be equal to the sum of the even elements in the array X in between the indices of m and n, inclusive. Assume that m <= n for each function call. Also, if there are no even elements in the array in the specified range, your function should return 0. SumEven(X,m, n)

  1. Write a recursive function, called prob , that will correctly print the index positions of all the occurrences of a specified character c , within the first m locations of an array X. You may assume that X is a global array which includes locations that range from 1 to n and is already populated with characters. Assume that m ๏‚ฃ n. The initial call is prob(X,c, m). 4. Write a recursive function to count number of odd integers in the array dd containing k elements.
    1. The Binomial function C(n,k) is usually defined in terms of factorials as follows: C(n,k) = n! / ( k! (n โ€“ k )! ) This involves lot of computations as it needs to find factorials of 3 numbers. It can be shown that it is also possible to compute C(n,k) through simpler functions: C(n,k) = C(n โ€“ 1, k โ€“ 1 ) + C(n โ€“ 1, k ) C(n,n ) = 1 C(n,1) = 1 Write a recursive function to compute C(n,k) without using factorials. 6.Write a recursive function MaxArray that will return the largest array element in the array A of size n. Solution Q 1

void PrintOdd(n) { realPrint(1, n); } void realPrint(current, n) { if (current > n) return; else { printf(โ€œ %d โ€œ, current); realPrint(current+2, n); } } Solution Q 2 int SumEven( int X[ ], int i, int j) { if ( i > j ) return 0; else if (X [i ] %2 == 0) return X[i] + SumEven(X,i+1, j); else return SumEven(X,i+1, j); } Solution Q 3 Void prob( char X[ ], char c, int j ) { if (j = 0) return; else { if (X[j] == c) print("Found ", c, " at ", j); prob( X, c, j -1) } }