A c program that generates random sentences

Posted in: C by musiitwa joseph 2015-03-27 at 16:13:55

image for A c program that generates random sentences The program has 4 character arrays, each with 5 strings. Random numbers are generated from 0 to 5. The generated random numbers are passed as indexes to character arrays hence getting difference strings from the arrays, these are combined to form a sentence in the order: article, noun, verb, preposition, article and noun.

int main(){
the sentence generated should be in the order: article, noun, verb, preposition, article and noun

//declaring variables
//holder is an array used store randomly generated numbers
int holder[6], x, i, j=0;

//charcter arrays to be used
char* article[] = {"the", "a", "one", "some", "any"};
char* noun[] = {"boy", "girl", "dog", "town", "car"};
char* verb[] = {"drove", "jumped", "ran", "walked", "skipped"};
char* preposition[] = {"to", "from", "over", "under", "on"};

srand(time(NULL));//changes the algorithm for generating random numbers every time the function is executed
loop://loop is used with the goto statement to come back to this point, this keeps the program running
for(i=0; i<6; i++)
holder[i] = (int)(rand()%5);//generates random numbers between 0 to 5 five times
for example
first time holder[0] = 4
second time holer[1] may be equal to 1
third time holder[2] may be equal to ....

i=0; //resetting i = 0 to be used in the next statements

printf("\n\tSentence %d: ", ++j);//increments j before printing it

//prints setences using the index stored by the array holder
printf("%s ",article[holder[i++]]);//i++ prints with i = 0 and increments it for the next statement
printf("%s ",noun[holder[i++]]);
printf("%s ",verb[holder[i++]]);
printf("%s ",preposition[holder[i++]]);
printf("%s ",article[holder[i++]]);

//allowing the user to run the program again
printf("\n\tEnter\n\t\t1: To continue.\n\t\t2: To exit.\n\t\t");
scanf("%d",&x); //getting user input
if(x == 1)//checking user input
goto loop;//taking the user back to loop, enables the program to run again
else if(x == 2)
else{//if the user input is not one required
printf("\n\t\tWrong input, Try again\n");
goto again;//taking user back to again
return 0;