MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file:
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
#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.
Solution:-
Source code:-
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
const int SIZE = 27; //variable for 26 chars + 1
const int WORDSIZE = 25; // max word size allowed
/************************************************************************************
FUNCTIONS
************************************************************************************/
int data( string tArray[], int s, char m, char d )
{
int c = 0; // counter
for (int i = 0; i < s; i++)
{
int j = 0;
while ( tArray[i][j] != '\0'
)
{
if (
tArray[i][j] == m && tArray[i][j + 1] == d )
c++;
j++;
}
}
return c;
}
int evalChar( string tArray[], int s, char m )
{
int c = 0;
for ( int i = 0; i < s; i++ )
{
int d = tArray[i].length();
if ( tArray[i][d - 1] == m )
c++;
}
return c;
}
int buildChar(string tArray[], int s, char m) //builds first
character row
{
int c = 0;
for (int i = 0; i < s; i++)
{
if (tArray[i][0] == m)
c++;
}
return c;
}
int getRandom(int s) //gets the random
{
int random = ( rand() % s ) + 1;
return random;
}
char getChar(char cArray[], int i) { return cArray[i]; }
/************************************************************************************
FUNCTIONS
************************************************************************************/
int main()
{
char cArray[SIZE]; // char for array of characters
in set
string tArray[SIZE]; //string of training array
int firstChars[SIZE]; //variable for array of first
character multiplicities
int transArray[SIZE][SIZE]; //variable for transition
array of multiplicities
char word[WORDSIZE]; //char for word being built
string input; // user-specified file
int x , y;
cout << "Input File: "; cin >> input;
ifstream infile( input.c_str(), ios :: in );
infile >> x;
for (int i = 0; i < x; i++) infile >> cArray[i];
cArray[x] = '\0';
cout << cArray[x];
infile >> y;
for (int i = 0; i < y; i++) infile >>
tArray[i];
srand(time(0));
for (int i = 0; i < x; i++) // builds
matrix
{
for (int j = 0; j < x + 1;
j++)
{
if (j == x)
transArray[i][j] = evalChar(tArray, y, cArray[i]);
else
transArray[i][j] = data(tArray, y, cArray[i],
cArray[j]);
}
}
for (int i = 0; i < x; i++)
{
firstChars[i] = buildChar(tArray,
y, cArray[i]);
}
int iterate; // how many times it iterates
cout << "# of Iterations? ";
cin >> iterate;
string inputTwo;
cout << "Output File: ";
cin >> inputTwo;
ofstream outfile( inputTwo.c_str(), ios :: out );
for (int i = 0; i < iterate; i++)
{
int numbers = 0, end, first;
while (numbers < 25)
{
first =
getRandom(y);
if (numbers
== 0)
{
int j = 0, total = 0;
while (total < first)
{
j = j % x;
total += firstChars[j];
j++;
}
j--;
word[numbers++] = getChar(cArray, j);
end = j;
}
else
{
int j = 0, total = 0;
while (total < first)
{
j = j % (x + 1);
total +=
transArray[end][j];
j++;
}
j--;
word[numbers++] = getChar(cArray, j);
end = j;
}
if (end == x)
break;
}
outfile << word <<
endl;
}
cout << endl;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
input.file
n.txt
6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear
----------------------------------------------------------------------------------------------------------
Output file:-
t
m
atrete
rateate
Output:-
***********************************************************************************************************
if you are satisfy with my answer then please,please like my answer.
MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...
Write a program that uses the random number generator to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The sentences are constructed in the following order: article, noun, verb, preposition, article, noun. The program should generate 20 sentences. Capitalize the first letter. The arrays should contain: article "the", "one", "a", "some", "any" noun "boy", "girl", "dog", "town", "car" verb "drove", "jumped", "walked", "ran", "skipped" preposition "to", "from", "over", "under", "on"...
Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...
Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...
may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...
You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...
Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...