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...
Write a C/C++ program which reads in a list of process names and integer times from...
Write a C/C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read the list until end of transmission (^d). You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
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...
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?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT