Question

In: Computer Science

You are to write a C++ program which does the following: Reads in the size of...

You are to write a C++ program which does the following:

  1. Reads in the size of a list of characters.
  2. Reads in the list of characters.
  3. Prints the list of characters in the opposite order read in.
  4. Prints the list of characters in the order read in.
  5. Sorts the list.
  6. Prints the sorted list.

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.

Solutions

Expert Solution

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

Related Solutions

IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
Write a C program that does the following In this part, you will write more complicated...
Write a C program that does the following In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following: 1.(20 pts.) Allows the user...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that does the following:  Assume the canvas size of 500X500.  The...
Write a program that does the following:  Assume the canvas size of 500X500.  The program asks the user to enter a 3 digit number.  The program then checks the value of the first and last digit of the number.  If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer.  If the two digits are odd, it makes the background red and displays...
Write a program that does the following:  Assume the canvas size of 500X500.  The...
Write a program that does the following:  Assume the canvas size of 500X500.  The program asks the user to enter a 3 digit number.  The program then checks the value of the first and last digit of the number.  If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer.  If the two digits are odd, it makes the background red and displays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT