In: Computer Science
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
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;
}