Please read the entire question!! The other answers on chegg do not work, so ple
ID: 3700666 • Letter: P
Question
Please read the entire question!! The other answers on chegg do not work, so please do not just copy and paste it!
Write a procedural C++ program that generates random words from a training set. Do not hard-code the training set! Read it from a file. A suggested format for the input file is
6
a e m r s t
10
ate
eat
mate
meet
rate
seat
stream
tame
team
tear
Here are some suggestions for constants, array declarations, and helper functions (note that use of global variables will be heavily penalized):
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
const int SIZE = 27; // 26 chars + 1
const int WORDSIZE = 25; // max word size
// read from input file
char cArray[SIZE]; // array of characters in set
char tArray[SIZE][SIZE]; // training array
//constructed by your program
int firstChars[SIZE]; // array of first character multiplicities
int transArray[SIZE][SIZE]; // transition array of multiplicities
char word[WORDSIZE]; // word being built
// helper functions
int getRandom(int); // get next random integer
int getIndex(char [SIZE], char); // get index of char in cArray
char getChar(char [SIZE], int); // get char at index in cArray
Be sure to seed the random number generator with the current time (ONCE) before using the rand() function:
srand(time(NULL)); // seed random number generator
Prompt the user for the name of the input file and the number of iterations (this program will run forever without this limit!). Either prompt the user for the name of the output file, or inform the user of the name before exiting.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main(){
string word;
int count = 0;
ifstream infile;
infile.open("trainingset.txt");
if(!infile){
cout<<"Unable to open file";
exit(1);
}
int c;
infile >> c;
string characters;
infile >> characters;
infile >> count;
string tArray[count];
int i=0;
while(infile >> word){
tArray[i] = word;
i++;
}
for(int j=0;j<i;j++){
int k = getRandom(count);
cout<<tArray[k];
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.