Lab session : Recursive functions II



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)

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)

}

}

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download