Thread: random sentence generator

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    made some changes and built what I believe is a random seed generator , only a few errors now but still wont compile.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void sentenceGenerator(  char **article,  char **noun,  char **verb,
           char **preposition, char **article2,  char **noun2 );				 
    int main()
    {
     
    	 char *article[5]=    { "the", "a", "one", "some", "any",  };
    	 char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
    	 char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
    	 char *preposition[5]={ "to", "from", "over", "under", "on",  };  
    
           sentenceGenerator( article, noun, verb, preposition, article, noun );
    
    	   return 0;
    
    
    }
    
     sentenceGenerator( char **article,  char **noun,  char **verb,
           char **preposition, char **article2,  char **noun2 )
     {
         int i;
    	 int word;
    
    	 srand( time(NULL) );
    	 word = rand()%6;
    	
     	  for (i = 0; i <20; i++)
    	
    	{
            
    		printf( "%s%s%s%s%s%s\n", article[word], noun[word],  verb[word], preposition[word], 
    			article[word], noun[word] );
    	}
    
     }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You're not gaining anything here:
    Code:
     sentenceGenerator( char **article,  char **noun,  char **verb,
           char **preposition, char **article2,  char **noun2 )
    /* ... */
    sentenceGenerator( article, noun, verb, preposition, article, noun );
    You're just passing two separate copies of the same pointers.

    I might do something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 5
    
    void sentenceGenerator(const char *const art[],  const char *const noun[],
                           const char *const verb[], const char *const prep[])
    {
       int i;
       for ( i = 0; i < 20; i++ )
       {
          printf( "%s %s %s %s %s %s\n",
                  art  [rand() % SIZE],
                  noun [rand() % SIZE],
                  verb [rand() % SIZE],
                  prep [rand() % SIZE],
                  art  [rand() % SIZE],
                  noun [rand() % SIZE]);
       }
    }
    
    int main()
    {
       const char *art [SIZE] = { "the", "a", "one", "some", "any",};
       const char *noun[SIZE] = { "boy", "girl", "dog", "town", "car",};
       const char *verb[SIZE] = { "drove","jumped", "ran", "walked", "skipped",};
       const char *prep[SIZE] = { "to", "from", "over", "under", "on",};
       srand(time(0));
       sentenceGenerator( art, noun, verb, prep );
       return 0;
    }
    
    /* my output
    the town ran over the boy
    a girl skipped under a boy
    a car drove on some town
    .
    .
    .
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  2. Random Sentence From A File
    By gandil in forum C Programming
    Replies: 3
    Last Post: 01-08-2006, 09:50 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM