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....
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.
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**
Calculate E(rxy) for (50%X + 50%Y) portfolio E(rxz) for (50%X + 50%Z) portfolio from the following...
Calculate E(rxy) for (50%X + 50%Y) portfolio E(rxz) for (50%X + 50%Z) portfolio from the following data:                                                             E(rxy)                 E(rxz)                                                                                       (50%X + 50%Y)      (50%X + 50%Z) Yr (t)                  E(rx) E(ry) E(rz)         2012                 8.0    24.0 8.0                                       2013                   10.0 20.0 12.0                                    2014                   12.0 16.0 16.0                                    2015                   14.0 12.0 20.0                  2016                   16.0 8.0    24.0                  ...
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2?...
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2? with steps of ?/16. The numbers should have three digits to the right of the decimal. Determine an appropriate field width. Use fprintf to display the output in tabular format with the following headers. x sin(x) cos(x) tan(x) Also, plot the three equations in the same figure window, but on separate axes. Plot all thee equations from 0 to 2? on the x-axis. Plot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT