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.
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:
Get Answers For Free
Most questions answered within 1 hours.