Strings rand

Write a program that uses random-number generation to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. The words should be separated by spaces. The program should generate 20 sentences. You can use the following declaration:
string article[5] = { "the", "a", "one", "some", "any" };
string noun[5] = { "boy", "girl", "dog", "town", "car" };
string verb[5] = { "drove", "jumped", "ran", "walked", "skipped" };
string preposition[5] = { "to", "from", "over", "under", "on" };
string sentence ;
******I dont know why its not generating 20 sentences...can some one help me*******


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  //#include<iostream>
//#include<cstdlib>
//#include<ctime>
//using namespace std;
//int main(){
//    string article[5] = { "the", "a", "one", "some", "any" };
//   string noun[5] = { "boy", "girl", "dog", "town", "car" };
//   string verb[5] = { "drove", "jumped", "ran", "walked", "skipped" };
//   string preposition[5] = { "to", "from", "over", "under", "on" };
//   string sentence;
//   int sum;
//   srand(time(0));
//   for(int i=0;i<20;i++){
//    sum=1+rand()%20;
//    sentence[sum];
//    cout<<"The randome numbers are :"<<sum<<endl;
//    cout<<article[i] <<" "<< noun[i] <<" "<< verb[i] <<" "<< preposition[i] <<" "<<article[i]<<" "<<noun[i]<<endl;
//   }
//    return 0; }
you need 4 other variables to generate randomly for the noun,verb...
Last edited on
i didn't get it still!
did you get it ? if not i will wright the code for you tomorrow, it's pretty late where I am now.
I dont know why its not generating 20 sentences...

Do you mean your variable sentence or the sentence that you print out?

 
sentence[sum];

This is meaningless. This instruction returns the character in the variable sentence at the (sum+1)th position, and the returned character is never used.

There is no randomization in the sentence printed out from line 17 because you're iterating through the arrays consecutively.
Consider this piece of code:
1
2
3
4
int word = rand()%5;
sentence = article[word];
word = rand()%5;
sentence += noun[word];

This will generate a random pairing of an article and noun from your arrays. I've broken up the code into smaller bits to make things clear, but you could probably condense it into one line.
Topic archived. No new replies allowed.