***PROCEDURAL CODE C++ *** C++ no c please!! please include any header files if
ID: 3697751 • Letter: #
Question
***PROCEDURAL CODE C++
*** C++ no c please!! please include any header files if needed to run
*** Please add comments if you can
write a c++ program that generates random words from a training set. Do not hard-code the training set! Read it from a file. 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.
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
#include
#include
#include
#include
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 before using the rand() function:
srand(time(NULL)); // seed random number generator
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
const int SIZE = 27; // 26 chars + 1
const int WORDSIZE = 25; // max word size
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
int main(){
int numbiter=0, cpynumbiter=0;
// read from input file
char cArray[SIZE]; // array of characters in set
char tArray[SIZE][SIZE]; // training array
string line, mini_line, m;
int count=0, i=0;
int lenght_file=0;
FILE *f, *outf;
while (true){
cout << "Enter file name: ";
cin >> cArray;
cout << "Enter number of iterations: ";
cin >> numbiter;
cpynumbiter = numbiter;
f = fopen(cArray,"r");
if (f == NULL)
exit(1);
while (feof(f) == 0)
{
if (cpynumbiter>0){
strcpy(tArray[i],cArray);
cpynumbiter--;
i++;
}else
break;
}
fclose(f);
cout << "Output: file output.txt ";
outf = fopen("output.txt","w");
for (i=0; i<SIZE; i++){
strcpy(cArray,tArray[getRandom(SIZE)]);
cout << cArray;
fwrite( tArray[i], sizeof(char), sizeof(tArray[i]), outf );
}
fclose(outf);
}
system("pause");
return EXIT_SUCCESS;
}
// helper functions
// get next random integer
int getRandom(int a){
return rand() % a;
}
// get index of char in cArray
int getIndex(char word[SIZE], char c){
int index=0, i;
for (i=0; i<SIZE; i++)
if (word[i] == c){
index = i;
break;
}
return index;
}
// get char at index in cArray
char getChar(char word[SIZE], int i){
char c;
if (i<SIZE && i>0)
c = word[i];
return c;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.