Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

****Please use C++ First, start by writing and testing the following functions:

ID: 3821648 • Letter: #

Question

****Please use C++

First, start by writing and testing the following functions:

1. Write a recursive function to display the digits of an arbitrary integer in reverse order. That is, if called with the integer 2364, it would display 4 6 3 2.

2. Write a function that takes in a string and reverses it using a stack. Use an STL stack.

3. Write a recursive function that performs selection sort. Each recursive step should perform another pass on the array.

4. Write a recursive function that performs bubble sort. Each recursive step should perform another pass on the array.

Then, in your main body, test these programs.

- Read in a number from the user. Call your reverse function and output the integer in reverse.

- Prompt the user for a string, reverse it with your function and output it to the screen.

- Create an array of 10 random numbers, display it to the screen, then sort it with your recursive selection sort. Then output the results to the screen.

- Create a new array of 10 random numbers, display it to the screen, then sort it with your recursive bubble sort and output the results to the screen.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;

// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end.
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1])
swap(arr[i], arr[i+1]);

// Largest element is fixed,
// recur for remaining array
bubbleSort(arr, n-1);
}

/* Function to print an array */
void printArray(int arr[], int n)
{
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}

// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array : ");
printArray(arr, n);
return 0;
}