C++ program to reverse an array within given indexes using recursion (logic explained)

C++ program to reverse an array within given indexes using recursion.

Here is the C++ program to reverse an array within given indexes using recursion. It takes inputs two indexes from user and convert the array between that indexes. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter size of array
2: User asked to enter array elements
3: User asked to enter two indexes (second should be greater than first)
Developer side
1: Declare variables that are going to be used in program 
2: Input size of array
3: Input elements of array using for loop 
4: Input start and stop indexes 
5: Pass array , start and stop indexes to a function reverse
6: Check if left index is less than right
7: if above condition holds swap elements using swap function and
8: and call reverse function and pass array , left index +1 and right index -1
9: Back in the main show the array 
10: Program ends here.
Code:


#include<iostream>
using namespace std;

void swap(char *array, int leftIndex, int rightIndex);
void reverse(char *array, int leftIndex, int rightIndex);

int main(){
       int  elementCount, counter,start,stop;
       char inputArray[500];
       cout<<"Enter number of elements in array: ";
       cin>>elementCount;

       cout << "Enter chars  ";

       for (counter = 0; counter < elementCount; counter++){
              cin>>inputArray[counter];
       }
       cout << "Enter start index";
       cin >> start;
       cout << "Enter stop index";
       cin >> stop;
       reverse(inputArray, start, stop );

       /* Print Reversed array */
       cout<<"Reversed Array\n";
       for (counter = 0; counter < elementCount; counter++){
              cout<<inputArray[counter];
       }
       system("pause");
      
       return 0;
}
/*
*  Function to swap two numbers in array
*/

void swap(char *array, int leftIndex, int rightIndex){
       int temp;
       temp = array[leftIndex];
       array[leftIndex] = array[rightIndex];
       array[rightIndex] = temp;
}
/*
* Function to reverse an Array using recursion
*/
void reverse(char *array, int leftIndex, int rightIndex){
       if (NULL == array){
              cout<<"Invalid Input";
              return;
       }
       /*
       * Swap left and right elements and recursively call reverse
       * function on subArray [leftIndex + 1, rightIndex - 1]
       */
       if (leftIndex < rightIndex){
              swap(array, leftIndex, rightIndex);
              reverse(array, leftIndex + 1, rightIndex - 1);
       }
}


 

Output:



Comments

  1. Hello Sir
    In the directory of this game there is one file named as SHAPES.ZIP which is password protected but i dont know the pass beacause of this i cant extract it and hence the game is not working so please email me the password if u know..
    my gmail id is:
    nwzsayyed@gmail.com

    sir please do something.......

    ReplyDelete
  2. How to Make a Jackpot at The Wynn Casino in Las Vegas - JT
    With 고양 출장안마 a Jackpot, there is 세종특별자치 출장안마 a way to bet a few $10k or more. There are three 삼척 출장안마 or four games and a 이천 출장마사지 Jackpot is always the 강원도 출장마사지 winner. With this combination of

    ReplyDelete

Post a Comment