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
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
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...
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]
Write an assembly program to compute the following expressions Create a list named ‘z’ of size...
Write an assembly program to compute the following expressions Create a list named ‘z’ of size 3 using DUP operator. Leave the list ‘z’ uninitialized. You can denote the items in the list as [z0, z1, z2]. z0 =x+13 z1 = y-x z2= r+z1-13 Where x, y, r are 16-bit integer memory variables. x = 7, y = 20, r = 4 Use the debugger to verify your answer. Please answer using this format for code: .386 .model flat, stdcall...
Create a file that has a series of ten strings in it, all accepted from the...
Create a file that has a series of ten strings in it, all accepted from the user through the keyboard. Then, open that file in read mode, and reverse the order of the characters in each line, saving the reversed line to a new file. Display the completed reversed file when done. Need help this done in PERL language. Thank you
Wtite a Python script to print a range of alphabetic characters ‘a’ to ‘z’ using the...
Wtite a Python script to print a range of alphabetic characters ‘a’ to ‘z’ using the custom range() function. You will need to use the ASCII character value and then convert the ASCII value to a alphabetic letter using a chr() function. Expected Output (submit code and output): Generate the characters from a to z, inclusive.
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...