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

Dani AI

Generated

The root problem in the original code was index usage and a few unsafe/unused bits. indexed the arrays with i (so the same element from each array appears each loop), used num = 1 + rand() % 5 (that produces 1..5 — index 5 is out of bounds for a 0..4 array) and left sent[num]; which does nothing. 's answer fixes the immediate issue by choosing a random element for each position; that is the simplest correct approach.

A couple of robustness notes that add value beyond the working example already posted:

  • Seed once at program start with srand((unsigned)time(NULL)); before any rand() calls.
  • Prefer computing array sizes programmatically and a small helper to map rand() into 0..n-1 (avoids hard-coding 5 and reduces off-by-one bugs):
#define COUNT(x) (sizeof(x)/sizeof((x)[0]))

static int rand_index(size_t n) {
    return (int)((double)rand() / ((double)RAND_MAX + 1) * n); /* uniform 0..n-1 */
}
  • If assembling the sentence into a single C buffer (AncientDragon suggested strcpy/strcat), use snprintf instead to avoid buffer overflows and to let the format control spacing/punctuation:
char out[128];
snprintf(out, sizeof out, "%s %s %s %s %s %s.", art, subj, verb, prep, art2, obj);

Extra tips: if the second noun should not repeat the first, re-roll that index or shuffle indices (Fisher–Yates) to sample without replacement. For small arrays, rand()%n is usually acceptable, but the rand_index method above avoids subtle modulo bias. For thread-safety or stronger randomness on modern systems, consider platform APIs (rand_r, random, or arc4random_uniform) appropriate to the target environment.

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.