Thursday 2 December 2010

Write a recursive function that takes three arguments (an integer array, starting subscript ‘s’ and ending subscript ‘e’ )

In first recursive call, the function should display the array from subscript ‘s’ (s = 0)
to ‘e’ (e =
size of array). In each successive call, the function should print the array from index
s+1 to e. T
function should stop processing and return when starting subscript becomes equal to
ending
subscript.
For example, if user enters values for array 2, 3, 4, 5, 6 then the recursive function
must display the following output.
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
answer
#include <iostream.h>;
void PrintArray(int arrayInput[], int &s, int &e);
main ( ) 
{
int pause;
int TestArray [6] = {1,2,3,4,5,6};
int StartPoint = 0;
int EndPoint = 5;
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cout<<"\n";
PrintArray(TestArray , StartPoint, EndPoint);
cin >> pause;
}
void PrintArray(int arrayInput[], int& s, int& e)

{
for (int i = s; i<= e; i++)
{
cout<<  arrayInput[i];

s=s+1;

}

No comments:

Post a Comment