Question

In: Computer Science

In this program, you will generate a random “sentence” constructed of random “words”. Quotes are used...

In this program, you will generate a random “sentence” constructed of random “words”. Quotes are used in the preceding sentence because the “words” will mostly be nonsense words that do not exist in the English language.

Step 1. The average number of words in an English sentence is about 17 words. First generate a pseudorandom number NW between 10 and 20 for the number of words in your random “sentence”. Use srand( ) to set the initial value in the iterative algorithm within the rand( ) function. Given NW, initialize a for-loop for(i=0; i

Step 2. To generate a pseudorandom “word” within the for-loop above, you first need to generate a pseudorandom number NL for the number of letters in the “word”.
The number of letters in a word follows the following probability table. two-letter words 0.20 three-letter words 0.27 four-letter words 0.22 five-letter words 0.14 six-letter words 0.09 seven-letter words 0.08 -------------------------------- Total = 1.00
Think of generating a pseudorandom value NL between 0.0 and 1.0 and choosing the number of letters in the “word” using the pseudorandom value according to the following: If NL >= (0.00) and NL <= (0.20), then the number of letters in the word will be two. If NL> (0.20) and NL <= (0.20+0.27), then the number of letters in the word will be three. If NL> (0.20+0.27) and NL <= (0.20+0.27+0.22), then the number of letters in the word will be four. If NL> (0.20+0.27+0.22) and NL <= (0.20+0.27+0.22+0.14), then the number of letters in the word will be five. If NL> (0.20+0.27+0.22+0.14) and NL <= (0.20+0.27+0.22+0.14+0.09), then the number of letters in the word will be six.
If NL> (0.20+0.27+0.22+0.14+0.09) and NL <= (0.20+0.27+0.22+0.14+0.09+0.08), then the number of letters in the word will be seven.
Notice that there are 6 intervals, representing NL=2 to NL=7. To assign a number of letters NL in the “word”, declare a 6-cell 1-D float array prob_letters[ ] and load the 1-D array with the probability sums from above. float prob_interval[6]={ 0.2, 0.47, 0.69, 0.83, 0.92, 1.0};
Now generate a pseudorandom value x between 0.0 and 1.0 x=rand( )/(float)RAND_MAX; Initialize the number of letters NL to 2: NL=2; Now check if x lies in one of the other five intervals for NL=3 to NL=7. If so, assign NL to the number of letters for that interval. for(i=1; i<6; i++){ if(x>= prob_interval[i-1] && x<= prob_interval[i])NL=i+2; }
Two important Notes : Important Note 1 : A better way to generate the 1-D array of interval values prob_interval[ ] is to start from the probabilities for the occurrencess of NL=2 through NL=7. float prob_interval[6]={ 0.2, 0.27, 0.22, 0.14, 0.09, 0.08}; Then form the array elements as the sums for(i=1; i<6; i++){ prob_interval[i]= prob_interval[i]+ prob_interval[i-1]; } Use this approach in the

Step 3 below. Important Note 2. : The array prob_interval[ ] should be generated only one time, before you begin any looping. Do not generate this array over-and-over-again, by incorrectly placing it inside the loop in Step 1. Step 3. Print out a “word” of length NL letters to the display. This “word” will be constructed from pseudorandom letters a-z. Start with a for-loop for(j=0; j a 0.085 b 0.021 c 0.045 d 0.034 e 0.112 f 0.018 g 0.025 h 0.030 i 0.075 j 0.002 k 0.011 l 0.055 m 0.030 n 0.067 o 0.07 p 0.032 q 0.002 r 0.076 s 0.057 t 0.070 u 0.036 v 0.011 w 0.013 x 0.002 y 0.018 z 0.002 ---------------- total 1.000
Use the method from Step 2. You can make use of the sequential ascii character codes for letters ‘a’ through ‘z’. float prob_let[26]= {0.085, 0.021, 0.045, 0.034, 0.112, 0.018, 0.025, 0.030, 0.075, 0.002, 0.011, 0.055, 0.030, 0.067, 0.071, 0.032, 0.002, 0.076, 0.057, 0.070, 0.036, 0.011, 0.013, 0.002, 0.018, 0.002}; Important Note: The first letter of the first word in the sentence should be printed as capitalized; i.e. uppercase. Step 4. Place a blank space after the “word” printed in Step 3, or place a period after the “word” printed in Step 3 if it is the last “word” in the sentence. Your results should look like: Elfh gfk llae mjlodp noc tjvjs mlknko si. Note, we haven’t applied any rules such as: 1. a “minimum of one vowel per word”; 2. ‘t’ is often followed by ‘h to form ‘th’; 3. ‘s’ often occurs at the end of a word to form a plural; 4. etc.. so actual words will only appear infrequently

Solutions

Expert Solution

Thank you for the question, I really loved it, Hope you like the answer.

// Online C++ compiler to run C++ program online
#include <iostream>
#include<ctime>
using namespace std;
int main() {
    // Write C++ code here
    //std::cout << "Hello world!";
    srand(time(0));
    int nw=rand()%10+10;
    //cout<<nw<<endl;
    for(int i=0;i<nw;i++){
        float prob_interval[6]={ 0.2, 0.47, 0.69, 0.83, 0.92, 1.0};
        int nl=2;
        float x=(float)rand()/RAND_MAX;
        for(int j=1; j<6; j++){ 
            if(x>= prob_interval[j-1] && x<= prob_interval[j])
                nl=j+2; 
        }
        string word="";
        //cout<<"nl"<<nl<<endl;
        for(int j=0;j<nl;j++){
            float prob_let[26]= {0.085, 0.021, 0.045, 0.034, 0.112, 0.018, 0.025, 0.030, 0.075, 0.002, 0.011, 0.055, 0.030, 0.067, 0.071, 0.032, 0.002, 0.076, 0.057, 0.070, 0.036, 0.011, 0.013, 0.002, 0.018, 0.002}; 
            for(int k=1; k<26; k++){ 
                prob_let[k]= prob_let[k]+ prob_let[k-1];
                
            }
            
            float x=(float)rand()/RAND_MAX;
            char c='a';
            for(int k=1;k<26;k++){
                //cout<<k<<" "<<x<<" "<<prob_let[k-1]<<" "<<prob_interval[k]<<endl;
                if(x>= prob_let[k-1] && x<= prob_let[k]){
                    //cout<<"ok"<<endl;
                    c=c+k;
                }
            }
            word+=c;
        }
        cout<<word<<" ";
        
    }
    cout<<endl;
    return 0;
}

Related Solutions

c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. Ex: If the input is: 3 automobile car manufacturer maker children kids 15 The automobile manufacturer recommends car seats for children...
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
Pig Latin is a language constructed by transforming English words. The following rules are used to...
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin: *If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”. *If the word begins with a vowel, then “way” is added to the end of the...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
Program in R: Generate 1000 exponential random deviates with the mean equal to ½. Be careful!...
Program in R: Generate 1000 exponential random deviates with the mean equal to ½. Be careful! The exponential is sometimes parameterized directly with the mean, B, using 1/B*exp(-x/B) and sometimes it is parameterized with the rate lambda = 1/B. Know what your software is doing. After generating the data, compute the mean and variance of the data. The mean and variance is all I need to see.
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
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...
Suppose you ran your elevator simulation program N times with different random number seeds to generate...
Suppose you ran your elevator simulation program N times with different random number seeds to generate N independent measurements of the average travel time X1, X2,⋅⋅⋅, XN a. State the correct formula for the 95% confidence interval for the global average travel time across all N runs assuming that each run of your simulation program was long enough to assume that the average travel times across different runs have an i.i.d. normal distribution. [HINT: Remember to use the t-distribution, because...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences...
A computer random number generator was used to generate 550 random digits (0,1,...,9). The observed frequences of the digits are given in the table below. 0 1 2 3 4 5 6 7 8 9 58 55 45 50 53 50 57 57 46 79 Test the claim that all the outcomes are equally likely using the significance level ?=0.05. The expected frequency of each outcome is E= The test statistic is ?2= The p-value is Is there sufficient evidence...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT