

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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)
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) } }