Part 1: Recursive Conversion Convert the following function to one that uses rec
ID: 3634403 • Letter: P
Question
Part 1: Recursive ConversionConvert the following function to one that uses recursion.
void sign (int n)
{
while (n > 0)
cout << “No Parking ”;
n--;
}
Demonstrate the function with a driver program.
Part 2: isMember Function
Write a recursive Boolean function named isMember. The function should accept two arguments, an array and a value. The function should return true if the value is found in the array or false if the value is not found in the array. Demonstrate the function in a driver program.
Part 3: String Reverser
Write a recursive function that accepts a string object as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Part 4: Recursive Array Sum
Write a function that accepts an array of integers and a number indicating the number of elements as arguments. The function should recursively calculate the sum of all the numbers in the array. Demonstrate the function in a driver program.
NOTE: You can use the same driver program to test all parts of this assignment. You do not need to create a separate driver program for each part.
Explanation / Answer
Dear,
1)
#include<iostream>
using namespace std;
//Function prototype
void sign(int);
//main function
void main()
{
int num;
//Inputting data
cout<<"Enter number:";
cin>>num;
//Function call
sign(num);
//Pause system for a while
system("pause");
}
//Function definition
void sign(int n)
{
if(n>0)
{
cout<<"No Parking ";
sign(n-1);
}
}//end main
2.
#include <iostream>
using namespace std;
// Function prototype
bool isMember(int [], int, int, int);
const int SIZE = 20; // Array size
int main()
{
// Define an array of employee ID numbers
int tests[SIZE] = {101, 142, 147, 189, 199, 207, 222,
234, 289, 296, 310, 319, 388, 394,
417, 429, 447, 521, 536, 600};
int empID; // To hold an ID number
bool results; // To hold the search results
// Get an employee ID number to search for.
cout <<"Enter the Employee ID you wish to search for: ";
cin >> empID;
// Search for the ID number in the array.
results = isMember(tests, 0, SIZE - 1, empID);
// Display the results of the search.
if (results == false)
cout << "That number does not exist in the array. ";
else
{
cout << "That ID is found " <<" in the array ";
}
//To pause system for a while
system("pause");
return 0;
}
//Returns false if not found
bool isMember(int array[], int first, int last, int value)
{
int middle; // Mid point of search
if (first > last)
return false;
middle = (first + last)/2;
if (array[middle]==value)
return true;
if (array[middle]<value)
return isMember(array, middle+1,last,value);
else
return isMember(array, first,middle-1,value);
}
//end isMember
3.
#include<iostream>
using namespace std;
//Function prototype
void Reverser(char [],int ,int);
//main function
int main()
{
char names[20];
cout<<"Enter string:";
gets(names);
int len=strlen(names);
//Function call
Reverser(names,0,len-1);
//To pause system for a while
system("pause");
return 0;
}//End main
//Function definition
void Reverser(char array[],int pos,int size)
{
if(pos<=size)
{
cout<<array[size];
Reverser(array,pos,size-1);
}
}
4.
#include<iostream>
using namespace std;
//Function Definition
int ArraySum(int arr[],int n)
{
if(n<0)
return 0;
else
return(arr[n]+ArraySum(arr,n-1));
}//End ArraySum
//Main function
void main()
{
//Declaring variable
int array[5] = {1,2,3,4,5};
int sum;
//Function call
sum=ArraySum(array,4);
//Outputting sum of elements
cout<<"Sum of Array is:"<<sum<<endl;
//To pause system for a while
system("pause");
}
Hope this will help you..