The assignment was to form random sentences from this words in the order of: art
ID: 3543406 • Letter: T
Question
The assignment was to form random sentences from this words in the order of: article, noun, verb, preposition, article , and noun.
The sentences will begin with an uppercase letter and end in a period.
#include<iostream>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
using namespace std;
void main()
{
char article[5][5] = {"the", "a", "one", "some", "any"};
char noun[5][5] = {"boy", "girl", "dog", "town", "car"};
char verb[5][8] = {"drove", "jumped", "ran", "walked", "skipped"};
char preposition[5][6] = {"to", "from", "over", "under", "on"};
char sent[36];
int i;
srand(time(NULL));
for (i = 1; i <= 10; i++)
{
int word = rand()%5;
char sentence;
sentence = article[word];
word = rand()%5;
sentence += noun[word];
cout<<"The random numbers are :"<< word <<endl;
cout<< article[i] <<" "<< noun[i] <<" "<< verb[i] <<" "<< preposition[i] <<" "<<article[i]<<" "<<noun[i]<<endl;
}
}
Explanation / Answer
#include<iostream>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
using namespace std;
int main()
{
char article[5][5] = {"the", "a", "one", "some", "any"};
char noun[5][5] = {"boy", "girl", "dog", "town", "car"};
char verb[5][8] = {"drove", "jumped", "ran", "walked", "skipped"};
char preposition[5][6] = {"to", "from", "over", "under", "on"};
//char sent[36];
int i, j, word[6];
srand(time(NULL));
for (i = 1; i <= 10; i++)
{
for (j = 0; j <= 5; j++)
word[j] = rand()%5;
cout<<"The random numbers are :";
for(j = 0; j < 6; j++)cout << word[j] <<" ";
j = 0;
cout << endl;
string phrase =article[word[j]];
phrase.at(0) = toupper(phrase.at(0));
cout << phrase << " ";
cout << noun[word[j++]] << " ";
cout << verb[word[j++]] << " ";
cout << preposition[word[j++]] << " ";
cout << article[word[j++]] << " ";
cout << noun[word[j++]] << ".";
cout << endl << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.