Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP DEBUGGING CODE C++ : Assignment Instructions: First - this program's whole purpose is to be able to follow directions to create silly sentences. Many

HELP DEBUGGING CODE C++ :

Assignment Instructions:

First - this program's whole purpose is to be able to follow directions to create silly sentences. Many times a programmer needs to create something that is specific to user requirements even when it may not be useful elsewhere. This is that program!

What we are going to do is create four word sentences that consist of an article, noun, verb, and adverb. The words will be predefined and stored in arrays. The program will randomly select words and put them together in a sentence. For example: The article array may contain the values This That A The the program would then need to generate a random number between 0 and 3 (the size of the array). If the random number is a 2, then the first word in the sentence would be A. The program would then randomly select a noun from the array of nouns, a verb, and an adverb respectively. These will then be concatenated together into one sentence (in that order).

After creating the silly sentence it may need some grammatical correction and adjustments to correct it then after all sentences are created, they will be printed. Well be doing some functions to make this program easier to do.

It is imperative you follow these instructions if you miss a step your program may fail.

Step 1: The word arrays: These arrays will be set up as const arrays before int main. For example, the article array you will have:

const int NUMART=4; // number of items in the article array

const string ARTICLES[]={The, This, A, That};

You will need to set up the constants for the other types of words. Be SURE to keep the words in the same order!! Note: While we are using static arrays here - these are still considered data. You may not use these values or the positions of the values in the program itself. That is considered hard coding data.

nouns: "giraffe","tiger","fox","panther","aardvark","bear"

verbs: "ate", "marched","slept"

adverbs: "heartily", "quickly","backward", slowly

Step 2: The functions we will be building functions to make this program easier to do. You must create and use these functions. The functions are listed below under the "Functions Required" section.

Step 3: The main program will create an array of sentences (max of 20 sentence). The user will input how many sentences to generate. The sentences will be constructed and then corrected. Some sentences may start with the article "A" followed by a noun that starts with a vowel. That is grammatically incorrect. So, if the main program determines a sentence begins with the article A, it will call the correct sentence function to finalize it. Otherwise, the sentence is unaltered. As the sentences are finalized, they will be stored in the array of sentences and printed using the print function.

Functions Required

Do NOT name your functions function1, function 2, etc. Use good names that indicate what they do.

Function 1 - Pick a word. This function takes one of the word arrays (article, noun, etc) as an array of strings (remember its const), and the number of words in the array as parameters. The function will then randomly select one of the words and return it as a string. You will seed the random number generator in the main program not in this function.

Function 2 Create a sentence. This function takes no parameters and returns a string that contains a sentence of four words. The first word is an article, the second word a noun, the third word is a verb and the fourth would is an adverb. You must use function 1 to generate the words. Concatenate the words into a sentence and return the sentence.

IMPORTANT NOTE: It is very important to force the computer to execute function1 for the articles, noun, verb, and adverb in that order so the random numbers work correctly. There are several ways to do this and the safest is probably to create temp variables such as string article then call the function to get the article. After you have created each of the words THEN concatenate them together.

Function 3 Correct a sentence. The article An is deliberately left out of the list of articles. The main program is going to check to see if the sentence starts with the article A. If it does, this function will be called. This function is going to correct the sentences where the noun begins with a vowel. The rules for this function are as follows:

If the noun in the sentence begins with a vowel, replace the article A with An. For our purposes, vowels will be a, e, i, o, u.

Otherwise, if the noun does NOT begin with a vowel, we are going to generate a new verb and replace the verb in the sentence with a new verb (you will need to use function 1 to get a new verb). Why are we doing this? Just because the instructions say to.

This function will be a void function. There will be one parameter the string sentence. Since the corrected sentence will need be returned in the original string, the parameter will need to be a reference parameter.

Function 4 Print the array of sentences: This is a void function that takes two parameters: the array of sentences (strings) as well as the integer indicating how many sentences there are in the array. It will go through the array printing out each final sentence. You may ONLY print the sentences from this function. They may NOT be printed directly in the main program, but rather by calling this function.

My Code:

I am currently stuck on function 3, I can't seem to figure out how to correct the A to an An if it is applicable, PLEASE help, I have commented out the code I have been playing around with that hasn't worked for me.

#include #include #include #include #include using namespace std;

//--------------------------------------------------------------------------------- const string NOUNS[]={ "giraffe", "tiger", "fox", "panther", "aardvark", "bear"}; const int numNOUN = 6; const string VERBS[]={"ate", "marched", "slept"}; const int numVERB = 3; const string ADVERBS[]={"heartily", "quickly", "backwards", "slowly"}; const int numADVERB = 4; const string ARTICLES[]= {"The", "This", "A", "That"}; const int numART = 4; //--------------------------------------------------------------------------------- string random_word(const string a[], const int x); string create_a_sentence(); void correct(string &); void print_sentences(string a[], int x); //----------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////// int main(){

int i, seed; int num_sentences = 0; int max_size = 20; string new_word; //seed the random number gen

cout > seed; srand(seed);

do{ cout > num_sentences; }while (num_sentences > max_size || num_sentences

//construct the sentences string sentence; string sentences[num_sentences]; for(i = 0; i

//print the sentences print_sentences(sentences, num_sentences); }

//////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////// string random_word(const string a[], const int x){

string randomword; //int i = rand() % x + 1); //cout

randomword = a[rand() % x]; //cout

////////////////////////////////////////////////////////////////////// string create_a_sentence(){

string rand_sentence; string s1, s2, s3, s4;

//article s1 = random_word(ARTICLES, numART); //cout

/oun s2 = random_word(NOUNS, numNOUN); //cout

///////////////////////////////////////////////////////////////////// void correct(string &a){ a.replace(a.find("A"), 1, "An"); cout

//////////////////////////////////////////////////////////////////////// void print_sentences( string a[], int x){

int i = 0;

for(i = 0; i

image text in transcribed

cmd line arg Enter a seed value for the random num| Enter a seed value for the random num This bear marched slowly That giraffe ate quickly The giraffe marched slowly The giraffe slept slowly This bear marched slowly That giraffe ate quickly The giraffe marched slowly The giraffe slept slowly 7114 4 cmd line arg Enter a seed value for the random n Enter a seed value for the random n That fox marched backwards The panther ate quickly That giraffe marched backwards The giraffe marched quickly A bear marched backwards That fox marched backwards The panther ate quickly That giraffe marched backwards The giraffe marched quickly A bear marched backwards This aardvark marched quickly The bear marched backwards An aardvark slept heartily The aardvark marched backwards A fox marched heartilv stdin 5432 20 panther marched quickly is aardvark slept heartily A giraffe marched backwards The giraffe marched heartily A aardvark slent auicklv cmd line arg Enter a seed value for the random num| Enter a seed value for the random num A fox slept backwards That giraffe slept quickly A fox marched backwards A bear ate quickly stdin 543 50 -1 2 cmd line arg Enter a seed value for the random n Enter a seed value for the random n That tiger marched slowly The fox ate quickly That tiger marched slowly The fox ate quickly An aardvark marched heartily That panther slept heartily This bear slept backwards A panther marched heartily A panther marched quickly This aardvark marched quickly The fox ate heartily This hear marched harkwards 987 20 aardvark marched heartily That panther slept heartily This bear slept backwards A panther marched heartily That aardvark ate quickly This aardvark marched heartily The panther slept heartily That fox marched auicklv cmd line arg Enter a seed value for the random n Enter a seed value for the random n This tiger ate slowly That panther marched backwards The giraffe ate slowly A fox marched slowly This tiger ate slowly That panther marched backwards The giraffe ate slowly A fox marched slowly An aardvark marched heartily A fox slept backwards The giraffe marched slowly A fox ate heartily This aardvark slept slowly The hear slent slowlv 5843 0 15 he giraffe marched slowly e aardvark slept heartily A bear marched heartily That bear marched heartily A fox ate quickly The hear marched heartilv cmd line arg Enter a seed value for the random num| Enter a seed value for the random num That aardvark ate slowly That aardvark ate slowly stdin

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the difference between set point and basal metabolic rate.

Answered: 1 week ago

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago