In: Computer Science
You are to write a C++ program which does the following:
You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.)
You MUST write at least three functions (in addition to 'main') in your program.
Program code Screenshot
Program Input/Output Screenshot
Program code to copy
#include <bits/stdc++.h>
using namespace std;
//prints the character array arr of length size in reverse order
void reverse_print(char arr[], int size)
{
for (int i = size - 1; i >= 0; i--)
{
cout << arr[i] << " ";
}
cout << endl;
}
//prints the character array arr of length size in order read in
void inorder_print(char arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
//sort the list of length = size using bubble sort
void sort(char arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
char temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
const int MAX_SIZE = 1000;
//declare array to store list of character
char arr[MAX_SIZE];
// Reads in the size of a list of characters.
int size;
cin >> size;
// Reads in the list of characters.
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "List in reverse order: ";
// Prints the list of characters in the opposite order read in using reversePrint function
reverse_print(arr, size);
cout << "List in order as read: ";
// Prints the list of characters in order read in using inorder_print function
inorder_print(arr, size);
cout << "Sorting the list... " << endl;
//sort the list using sort function
sort(arr, size);
cout << "Sorted list: ";
//print the sorted list using inorder_print function
inorder_print(arr, size);
}