Question

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.

Homework Answers

Answer #1

For this, we create a vector PRG of type string since character array means string and a character 'c' to store a random alphabet generated using rand() function. This character is then appended to a temporary string str in order to push the alphabet to vector PRG (can only push data of same type).

This is repeated 50 times for 50 alphabets using a for loop.

Then using the sort() function, we sort the vector in descending order by giving the 3rd argument of sunction as 'greater<string>()'.

CODE:

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector> //to use vector class
#include <bits/stdc++.h> //to use the sort() function for vectors
using namespace std;

int main()
{
std::vector<std::string> PRG; //creating a vector PRG
int i, j;
string str;
char c;
for(i=0; i<50; i++)
{

c = rand() % 26 + 'A'; //generating a random alphabet
str += c;

PRG.push_back(str); //pushing the random character in PRG
str.clear();
}
  
cout<<"Vector PRG of 50 random alphabets:\n";
for (auto k : PRG)
cout << k << " ";
cout<<endl;
  
sort(PRG.begin(), PRG.end(), greater<string>()); //calling sort() function on PRG
  
cout<<"Vector PRG after sorting in descending order:\n";
  
for (auto k : PRG)
cout << k << " ";
return 0;
}

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Reverse the order of characters in a "text file". For example, computer becomes retupmoc Text file...
Reverse the order of characters in a "text file". For example, computer becomes retupmoc Text file have this String "Computer".. write on other file String "retupmoc". Write this in C++..
In C++ Create a program that uses only Selection Sort and Insertion Sort for the National...
In C++ Create a program that uses only Selection Sort and Insertion Sort for the National Football League list of current players Inputting data from a text file named "NFLplayers.txt" Only want the name of the player(first name then last name), their team name, and position they play. Example is: Patrick Mahomes, Chiefs, Quarterback Output lists to a text file along with how long it took to go through the Selection Sort and Insertion Sort and how many iterations it...
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++.
Q) a) create a matrix named as X of evenly spaced values from 0 to 50,...
Q) a) create a matrix named as X of evenly spaced values from 0 to 50, with an increment of 10. b) a) create a matrix named as Y of evenly spaced values from 500 to 1000, with an increment of 3. c)a) create a matrix named as Z, the value of the 1st row is from 1 to 10, 2nd row from 2 to 20 with increment of 2, 3rd row 3 to 12. using subplot divide the window...
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do this manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please do not copy from stackabuse or stackoverflow, Please explain that algorithm with comments. i really want to learn this concept.
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses the list. You should mutate the original list, without creating any new lists. Do NOT return anything. Do not use any built-in list functions such as reverse(). def reverse(lst):    """Reverses lst in place (i.e. doesn't create new lists).     >>> L = [1, 2, 3, 4]     >>> reverse(L)     >>> L     [4, 3, 2, 1]     """
Create the following vector in R without using the c() function: z = [1 2 2...
Create the following vector in R without using the c() function: z = [1 2 2 3 3 2 4 4 4 4]
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of...
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of 1000 elements each with the following properties: By the way: To perform bucket sort the right way, convert the array elements to a value between 0 and 1 Array1: integers only in the range 0 through 999, already sorted in increasing order Array2: integers only in the range 0 through 999, already sorted in decreasing order Array3: integers only in the range 0 through...