954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create sentences using random num generation

Can anyone help me write a program that will print a sentence using 5 different words that are in 6 different array's based on a random number generated by the computer. Right now my code will print 5 sentences using each elimate from each array, but it doesn't pick the words at random from each array. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
 const char *a[5] = {"The", "A", "One", "Some", "Any"};
 const char *n[5] = {"boy", "girl", "dog", "town", "car"};
 const char *v[5] = {"drove", "jumped", "ran", "walked", "skipped"};
 const char *p[5] = {"to", "from", "over", "under", "on"};
 const char *a2[5] = {"the", "a", "one", "some", "any"};
 const char *n2[5] = {"boy", "girl", "dog", "town", "car"};
 char sent[35] = {0};
 int num = rand();
 int i = 0;
 
 srand(time(NULL));
 for( i = 0; i <= 4; i++){
  num = 1 + rand() % 5;
  sent[num];
  
 printf("\nThe random number is %d.", num);
 
 printf("\n\n%s %s %s %s %s %s.\n\n", a[i], n[i], v[i], p[i], a2[i], n2[i]);
 
}
return 0;
}
Little E
Newbie Poster
7 posts since Sep 2006
Reputation Points: 18
Solved Threads: 0
 

>>sent[num];
what you should do here us use strcpy() to copy the first random string into sent variable, then strcat() to copy each of the other strings.

>>printf("\n\n%s %s %s %s %s %s.\n\n", a[i], n[i], v[i], p[i], a2[i], n2[i]);
variable i is not a random number. you should use variable num instead of i.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

To AncientDragon : the a[num],v[num]... doesn't work,because num is the same in that insant moment(i tried that).

To Little E :1.Offtopic: next time try to type the code between code.../code tags ;)
2.What you need is this :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
const char *a[5] = {"The", "A", "One", "Some", "Any"};
const char *n[5] = {"boy", "girl", "dog", "town", "car"};
const char *v[5] = {"drove", "jumped", "ran", "walked", "skipped"};
const char *p[5] = {"to", "from", "over", "under", "on"};
const char *a2[5] = {"the", "a", "one", "some", "any"};
const char *n2[5] = {"boy", "girl", "dog", "town", "car"};

int i = 0;

srand((unsigned)time(NULL));
for( i = 0; i <= 4; i++)
{

printf("\n\n%s %s %s %s %s %s.\n\n", a[rand()%5], n[rand()%5], v[rand()%5], p[rand()%5], a2[rand()%5], n2[rand()%5]);

}
return 0;
}



Now,i've deleted the sent string,and the num variable,because u don't need them.
I hope the code above answers your problem

Good luck

Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
 
To AncientDragon : the a[num],v[num]... doesn't work,because num is the same in that insant moment(i tried that).

You are right -- my suggestion was not what he wanted. :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you EKO, your awesome. It worked great!!

Little E
Newbie Poster
7 posts since Sep 2006
Reputation Points: 18
Solved Threads: 0
 

Can anyone help me write a program that will print a sentence using 5 different words that are in 6 different array's based on a random number generated by the computer. Right now my code will print 5 sentences using each elimate from each array, but it doesn't pick the words at random from each array. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
 const char *a[5] = {"The", "A", "One", "Some", "Any"};
 const char *n[5] = {"boy", "girl", "dog", "town", "car"};
 const char *v[5] = {"drove", "jumped", "ran", "walked", "skipped"};
 const char *p[5] = {"to", "from", "over", "under", "on"};
 const char *a2[5] = {"the", "a", "one", "some", "any"};
 const char *n2[5] = {"boy", "girl", "dog", "town", "car"};
 char sent[35] = {0};
 int num = rand();
 int i = 0;
 
 srand(time(NULL));
 for( i = 0; i <= 4; i++){
  num = 1 + rand() % 5;
  sent[num];
  
 printf("\nThe random number is %d.", num);
 
 printf("\n\n%s %s %s %s %s %s.\n\n", a[i], n[i], v[i], p[i], a2[i], n2[i]);
 
}
return 0;
}


/* This code only prints the same element from each of the arrays , element[i], < = i <= 5.
You dont't use the random number at all to print the data, and your sent[num] is not used for anything. What is it for?
To do what you said you wanted, you must use the random number to choose the element from each array. Set it up in pseudocode and then you will see exactly how to do what you want.
*/

CPLUSCPLUS
Newbie Poster
6 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You