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;
}

Recommended Answers

All 5 Replies

>>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, n, v, p, a2, n2);
variable i is not a random number. you should use variable num instead of i.

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

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. :)

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

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 <= 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.
*/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.