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

A photo of what the code at current looks like comparred to what it should:

image text in transcribed

results Input cmd line arg stdin 23402 54234 Result Your Output Instructor Output Please make a selection from the fo | Please make a selection from the fo 1. Simple Sum 2. Simple Division 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus 1. Simple Sum 2. Simple Division 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus cmd line arg stdin 0 2344 205 2 5435 20000 eration you wish to perform: eration you wish to perform: Please cmd line arg 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus 9. Quit 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus 9. Quit stdin 234 234 234 234 Enter the number of the operation y Enter the number of the operation y 244 244 maximum 244 cmd line arg stdin 6 4.0 5.0 6.0 7.0 8.0 6 0.0 0.0 4.0 8.0 1.0 6 3.0 4.0 6.0 7.00.0 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus 9. Quit 3. Complex Division 4. Max 5. Hypotenuse 6. Cubic Equation 7. MinMax 8. Modulus 9. Quit Enter the number of the operation y Enter the number of the operation y cubic result 12 cmd line arg Please make a selection from the fo | Please make a selection from the fo

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

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions