Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Write a recursive function that takes an array, and

lower index and upper index of a range and search the


elements in the range for a specific key the is passed to
the function as a parameter. The function should
return true if the element is found and false otherwise.
template <class T>
bool searchR(T a[],int l,int u, T key)
{
if(l > u)
return false;
else {
if(a[l]==key)
return true;
else
return searchR(a,l+1,u,key);
}
}
Write a recursive function that takes an array, and
lower index and upper index of a range and return the
sum of the elements in the range.

template <class T>


T sumR(T a[],int l,int u)
{
if(l > u)
return 0;
else {
return a[l] + sumR(a,l+1,u);
}
}
Write a recursive function that takes an array, and
lower index and upper index of a range and search the
elements in the range for a specific key that is passed
to the function as a parameter. The function should
return the index of the element if the element is found
and -1 otherwise.
template <class T>
int searchR(T a[],int l,int u, T key)
{
if(l > u)
return -1;
else {
if(a[l]==key)
return l;
else
return searchR(a,l+1,u,key);
}
}
Write a recursive function that takes an array, and
lower index and upper index of a range and search the
elements in the range for a specific key that is passed
to the function as a parameter. The function should
find all index of all element that matches the key and
store them in the parameter array r.
template <class T>
void searchR(T a[],int l,int u, T key, int r[],int i=0)
{
if(l > u)
return;
else {
if(a[l]==key){
r[i]= l;
searchR(a,l+1,u,key,r,i+1);
}
else
searchR(a,l+1,u,key,r,i);
}
}
Write a recursive function that convert a decimal number to binary and return the binary number
as an integer number.

int DtoB(int n,int d=0)

if(n>0)

return n%2*pow(10,d)+DtoB(n/2,d+1);

}else

return 0;

write a recursive function that orints an array in reverse order

void printArrayReverse(int a[],int l,int u)

if(l>u)

return;

else{

printArrayReverse(a,l+1,u);

cout<<a[l]<<" ";

template <class T>

void printArrayReverse0(T a[],int l,int u)

if(u<l)

return;

else

cout<<a[u]<<" ";
printArrayReverse0(a,l,u-1);

void printArrayReverse1(int a[],int l,int u)

if(l>u)

return;

else{

cout<<a[u]<<" ";

printArrayReverse1(a,l,u-1);

void printArrayReverse2(int a[],int size)

if(size==0)

return;

else{

cout<<a[size-1]<<" ";

printArrayReverse2(a,size-1);

//assuming the array and the size are declared globally

void printArrayReverse3()

if(size==0)
return;

else{

cout<<a[--size]<<" ";

printArrayReverse3();

Rewrite the destroy member function of the class linkedListType using recursion.
template < class Type >
void linkedListType < Type > ::destroyList() {
if (first != NULL) //while there are nodes in the list
{
nodeType <Type> *temp = first; //set temp to the current node
first = first->link; //advance this->first to the next node
delete temp; //deallocate the memory occupied by temp
destroyList();
}else{
last = NULL; //initialize this->last to NULL; this->first has already
//been set to NULL by the while loop
this->count = 0;
}
}

You might also like