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 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...
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?
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...
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...
Write a C++ Program that does the following: As you can see, there is a file...
Write a C++ Program that does the following: As you can see, there is a file named "invoice1_test1.txt". You are to use this file as your input file for your program. Do the following: 1. Sort the file by last name using an array. You can use any of the sorting algorithms we have previously used in class. You may use the string data type to store text data. 2. Compute the following: a. The total balance due using the...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT