printing an array in reversed order using recursion
Nov 16, 2012 at 10:31pm UTC
I need to print the contents of an char array of any size in reversed order using recursion. I am stuck as I can only get it to print out in the order it is in the array so this is my code i need to know where is my problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include<iostream>
using namespace std;
void printArrayBackward(char arr[],int size)
{
if (size > 0)
printArrayBackward(arr, size - 1);
cout<<arr[size]<<" " ;
}
void main()
{
int size;
cout<<"Please Enter The Size Of The Array ?" <<endl;
cin>>size;
char *arr= new char [size];
cout<<"Please Enter The Elament of the Array :" <<endl;
for (int i=0;i<=size-1;i++)
{
cin>>arr[i];
}
cout<<endl;
cout<<"Printing Array Backward :" <<endl;
printArrayBackward(arr,size);
cout<<endl;
}
Last edited on Nov 16, 2012 at 10:31pm UTC
Nov 16, 2012 at 10:46pm UTC
1 2 3 4 5 6 7 8 9 10 11
#include<iostream>
using namespace std;
void printArrayBackward(char arr[],int size)
{
if ( size > 0 )
{
cout << arr[size-1] << ' ' ;
printArrayBackward(arr, size - 1);
}
}
Alternately:
1 2 3 4 5 6 7 8 9 10
#include<iostream>
using namespace std;
void printArrayBackward(char arr[],int size)
{
if ( size > 0 )
{
printArrayBackward(arr+1, size-1) ;
cout << arr[0] << ' ' ;
}
}
Nov 16, 2012 at 10:52pm UTC
thank you vary mach .... i love this website and i love you people you are amazing ....thanks again :)
Topic archived. No new replies allowed.