Question

In: Computer Science

How to make a random word generator from a list of array? string word ={ "RD","BL",...

How to make a random word generator from a list of array?

string word ={ "RD","BL", "YW", "GR","OR","VL","WH","BL" }

The output should produce 4 random words from the list like;

Expected output: RD YW OR BL

CODE USED: C++

Solutions

Expert Solution

Leave Comments for any doubt.

Code:

#include <iostream>

using namespace std;

// Generating random numbers array so that wordds are non repeating.
void generateSetOfNumbers(string str[], int n)
{
int *p = new int[n];
  
for (int i=0; i<n; ++i)
p[i] = i;
  
//shuffle p
for (int i=n-1; i>0; --i)
{
//get swap index
int j = rand()%i;
//swap p[i] with p[j]
int temp = p[i];
p[i] = p[j];
p[j] = temp;
}

/*Change value below if you want to generate more words from the list,
right now it is generating 4 word, make it 5 if you want 5 words.
CAUTION: Do not increase the value more than size of array. */
for (int i=0; i<4; ++i)
cout << str[p[i]]<<" ";
}


int main() {
string word[] ={ "RD","BL", "YW", "GR","OR","VL","WH","BL" };
  
// Getting size of array
int size = sizeof(word)/sizeof(word[0]);
  
generateSetOfNumbers(word, size);
return 0;
}

Output Screenshot:


Related Solutions

Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Use python There is a revenue list, how to covert it from string to int? Here...
Use python There is a revenue list, how to covert it from string to int? Here is the list ['$8,120,000,000', '$8,085,200,000', '$7,703,000,000', '$7,000,000,000', '$5,410,000,000', '$4,212,000,000', '$3,741,400,000', '$3,500,000,000', '$3,000,000,000', '$2,800,000,000', '$2,800,000,000', '$2,529,000,000', '$2,087,600,000', '$2,000,000,000', '$1,900,000,000', '$1,520,000,000', '$1,500,000,000', '$1,500,000,000', '$1,350,000,000', '$1,300,000,000', '$1,400,000,000', '$1,400,000,000', '$1,395,000,000', '$1,200,000,000', '$1,000,000,000', '$1,000,000,000', '$842,000,000', '$887,000,000', '$860,000,000', '$825,000,000', '$799,000,000', '$757,000,000', '$751,000,000', '$701,600,000', '$660,000,000', '$600,000,000', '$577,000,000', '$559,000,000', '$540,000,000', '$532,000,000', '$518,400,000', '$500,000,000', '$500,000,000', '$437,000,000', '$400,000,000', '$350,000,000', '$300,000,000', '$277,000,000'].
A computer random number generator was used to generate 950 random digits from 0 to 9....
A computer random number generator was used to generate 950 random digits from 0 to 9. The observed frequencies of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 88 82 97 84 87 87 95 93 90 147 Using a 0.05significance level, test the claim that all of the digits are equally likely. (a) Find the rejection region. Reject H0 if χ2> (b) Find the test statistic. (Round your final...
Questions 36-39: Six numbers are selected at random from a random number generator and entered into...
Questions 36-39: Six numbers are selected at random from a random number generator and entered into each of four rows. The summary statistics are presented in the chart below. Mean SD   Row 1 5.8  0.78 Row 2 3.6  0.23 Row 3 5.5  0.32 Row 4 4.0  0.59 Assume the populations are normal with equal variances. It is of interest to test the following: Ho: μ1 = μ2 = μ3 = μ4 36. What are the degrees of freedom for the...
Word Match: From the list of words provided, enter in the table below the word that...
Word Match: From the list of words provided, enter in the table below the word that best describes each of the listed meanings Alveoli Congestion Pleura Anthracosis Exudate Pneumocytes Bronchiole Fibrin Sequelae Bronchus Metamyelocytes Suppuration Term Meaning The process of forming puss that can occur during the process of eliminating stimulus of inflammation. Abnormal accumulation of blood or other fluids in blood vessel or body part. Cells and fluids that has seeped out of blood vessels or an organ. Small...
Word Match: From the list of words provided, enter in the table below the word that...
Word Match: From the list of words provided, enter in the table below the word that best describes each of the listed meanings Alveoli Congestion Pleura Anthracosis Exudate Pneumocytes Bronchiole Fibrin Sequelae Bronchus Metamyelocytes Suppuration Term Meaning The process of forming puss that can occur during the process of eliminating stimulus of inflammation. Abnormal accumulation of blood or other fluids in blood vessel or body part. Cells and fluids that has seeped out of blood vessels or an organ. Small...
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with...
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with the years 1916-2016 and dtype=int; 3) Make a numpy array with the months 1-12 and dtype=int; 4) Print out the shape of the data array and the lengths of the years and month array. If the length of your year array does not equal the number of rows in your data array, or the length of your months array does not equal the number...
How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
Create a method that returns the third character from the String word. Be sure to use...
Create a method that returns the third character from the String word. Be sure to use a try catch block and use the appropriate exception to deal with indexes out of a String’s bound: StringIndexOutOfBoundsException. Return the character 'x' (lowercase) when this exception is caught. Do not use if statements and do not use the general exception handler Exception. Examples: thirdLetter("test") -> 's' public char thirdLetter(String word) { } ​should return the char 'r'
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT