Question

In: Computer Science

Create a vector of 50 characters PRG from A to Z. Sort the list in reverse...

Create a vector of 50 characters PRG from A to Z. Sort the list in reverse order (from Z to A). Please use c++. Please use #include<iostream>. Please don't use the ASCII values.

Solutions

Expert Solution

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you have any concerns. Else please upvote.

Code:

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector<char> a; // Declaring the vector

    int random_character; //Variable to store the random character generated

   

    //initiale an alphabet array

    char alphabet[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G',

                          'H', 'I', 'J', 'K', 'L', 'M', 'N',

                          'O', 'P', 'Q', 'R', 'S', 'T', 'U',

                          'V', 'W', 'X', 'Y', 'Z' };

   

    //initialize random seed

    srand (time(NULL));

   

    //Populate the vector with random numbers between 1 and 500

    for(int i = 0; i<50; i++){

        random_character = alphabet[rand() % 26]; //generate the random character

        a.push_back(random_character); //add the random character generated to vector

    }

    // Print the vector before sorting

    cout << "The Vector before sorting is as follows: \n";

   

    for (int i = 0; i < a.size(); i++) { //loop through each elements in vector

        cout << a[i] << " "; //display the vector content

    }

    //sorting the vector

    for(int i=0; i<a.size()-1; i++){

        for(int j=0; j<a.size()-i-1;j ++){

            if(a[j]<a[j+1]) //compare the adjacent items in vector

            {

                //swap the elements

                char temp = a[j];

                a[j] = a[j+1];

                a[j+1]=temp;

            }

         }

    }

        

    // Print the vector after sorting

    cout << "\n\nThe Vector after sorting is as follows: \n";

   

    for (int i = 0; i < a.size(); i++) { //loop through each elements in vector

        cout << a[i] << " "; //display the vector content

    }

   

    return 0;

}

Output:


Related Solutions

Create a vector of 100 integers PRG from 1 to 500. Find the max and min...
Create a vector of 100 integers PRG from 1 to 500. Find the max and min and print those out. Please use c++.
Reverse the following selection sort to sort the items from right to left by computing the...
Reverse the following selection sort to sort the items from right to left by computing the max instead of min in java. (just write the algorithm) public class Selection{ public static void sort(Comparable[] a) { // Sort a[] into increasing order. int N = a.length; // array length for (int i = 0; i < N; i++) { // Exchange a[i] with smallest entry in a[i+1...N). int min = i; // index of minimal entr. for (int j = i+1;...
The forward mutation rate from allele Z to allele z is 0.00001 and the reverse mutation...
The forward mutation rate from allele Z to allele z is 0.00001 and the reverse mutation rate 0.0000001. What is the expected frequency of allele Z at mutational equilibrium?
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application....
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
Use fork() Create a program that reads characters from an input file. Use fork to create...
Use fork() Create a program that reads characters from an input file. Use fork to create parent and child processes. The parent process reads the input file. The input file only has letters, numbers. The parent process calculates and prints the frequency of the symbols in the message, creates the child processes, then prints the information once the child processes complete their execution. The child processes receives the information from the parent, generates the code of the assigned symbol by...
Explain the easiest way to create credit risk algorithm to sort a list of loan applicants?
Explain the easiest way to create credit risk algorithm to sort a list of loan applicants?
Create an example to illustrate how to profit from a “cash and carry” and “reverse cash...
Create an example to illustrate how to profit from a “cash and carry” and “reverse cash and carry” strategy?
Forecasts drive many Operations Management decisions, create a list of business decisions that require some sort...
Forecasts drive many Operations Management decisions, create a list of business decisions that require some sort of forecast. In your opinion, which of the Decision Makings Tools presented in Module A is the most useful? Please state your reasons. **Please state sources.Thank you**
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT