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

C++ Language In this assignment you will be writing several functions to practic

ID: 3822408 • Letter: C

Question

C++ Language In this assignment you will be writing several functions to practice your skills in writing recursive functions and using stacks/queues. First, start by writing and testing the following functions: a. 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. b. Write a function that takes in a string and reverses it using a stack. Use an STL stack. c. Write a recursive function that performs selection sort. Each recursive step should perform another pass on the array. d. 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. 1) Read in a number from the user. Call your reverse function and output the integer in reverse. 2) Prompt the user for a string, reverse it with your function and output it to the screen. 3) 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. 4) 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 <iostream>
#include <stack>
#include <stdlib.h>

using namespace std;


void printReverse(int n)
{
if (n == 0)
return;

int last = n%10;

cout << last << " ";
printReverse(n/10);
}


string reverseString(string s)
{
stack<char> strStack;
for(int i = 0; i < s.length(); i++)
{
strStack.push(s[i]);
}
string reversed = "";
while(!strStack.empty())
{
reversed = reversed + strStack.top();
strStack.pop();
}
return reversed;
}


void selection(int list[], int i, int j, int size, int flag)
{
    int temp;

    if (i < size - 1)
    {
        if (flag)
        {
            j = i + 1;
        }
        if (j < size)
        {
            if (list[i] > list[j])
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
            selection(list, i, j + 1, size, 0);
        }
        selection(list, i + 1, 0, size, 1);
    }
}

void printArr(int arr[], int n)
{
   for(int i = 0; i < n; i++)
   {
       cout << arr[i] << " ";
   }
   cout << endl;
}

// 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);
}

int main()
{
int n;
cout << "Enter n: ";
cin >> n;
printReverse(n);
cout << endl;

string str;
cout << "Enter a string : ";
cin >> str;
cout << "Original string " << str << endl;
str = reverseString(str);
cout << "Reversed string: " << str << endl;
cout << str << endl;

int arr[10];
for(int i = 0; i < 10; i++)
{
   arr[i] = rand()%100;
}

cout << "Array before selection sort: " << endl;
printArr(arr, 10);

selection(arr, 0, 0, 10, 1);

cout << "Array after selection sort: " << endl;
printArr(arr, 10);


for(int i = 0; i < 10; i++)
{
   arr[i] = rand()%100;
}

cout << "Array before bubbleSort sort: " << endl;
printArr(arr, 10);

bubbleSort(arr, 10);

cout << "Array after bubbleSort sort: " << endl;
printArr(arr, 10);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote