Thread: random sentence generator

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    63

    random sentence generator

    I've been working on a program over the last few days that asks to create an array of pointers to char called article noun verb and preposition. Basically I need to create random sentences in the format article noun verb preposition article noun verb. The sentences need to start with a capital letter and end with a period.

    It sounded pretty simple at first but I seem to be getting lost in how to generate random sentences where the pointers have different rows and collums. There is nothiong similar to this in my book. This s what I have so far, any "pointers" would be greatly appreciated! (bet you haven't heard that on before. )
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] );
    
    int main()
    {
    	const char *article[5]=    { "the", "a", "one", "some", "any",  };
    	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
    	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
    	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
    	int i;
    
        
    	for ( i = 0; i < 6; i++ );
    	
    		*( artical)     = i;
    		*( noun)        = i;
                    *( verb)        = i;
    		*( preposition) = i;
    
          srand( time( 0 ) );
    
          void sentenceGenerator( artical, noun, verb, preposition, artical, noun )
    
        }
    }
    
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] )
    {
    
    	int i;
    	for (i = 0; i <20; i++)
    	{
    		printf( "%s%s%s%s%s%s.\n", *article, *noun, *verb, *preposition, *article, *noun );
    	}
    }
    Last edited by jsbeckton; 10-11-2005 at 02:51 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    	for ( i = 0; i < 6; i++ );<<<do-nothing loop because of trailing semicolon
    	
    		*( artical)     = i;What is this supposed to do ????
    		*( noun)        = i;
            *( verb)        = i;
    		*( preposition) = i;
    Code:
    printf( "%s%s%s%s%s%s.\n", *article, *noun, *verb, *preposition, *article, *noun );
    The above will always print the same thing on each iteration. Remove the stars because *article is only one character but the format string %s expects a null-terminated string.
    Last edited by Ancient Dragon; 10-11-2005 at 02:58 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    My intent on the for loop was to initialize the four arrays of pointers.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    in my prototype, its giving me errors because of "redefinition" in "article" and "noun"

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by jsbeckton
    in my prototype, its giving me errors because of "redefinition" in "article" and "noun"
    Because you have used the same variable name for two separate parameters:

    Code:
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] );
    Also, check your spelling of article in main().

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by jsbeckton
    My intent on the for loop was to initialize the four arrays of pointers.
    the arrays are already initialized when declared. No need for further initialization.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    [QUOTE=cwr]Because you have used the same variable name for two separate parameters:

    Code:
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] );
    My alogarithm will need to use the article and noun pointer arrays twice.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I removed the initializers and modified the pointer arrays to include additional article and noun pointer arrays, however it seems that there must be another way to use those pointer arrays twice.

    updated code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void sentenceGenerator(  char article1[],  char noun1[],  char verb[],
    					    char preposition[], char article[],  char noun[] );
    
    int main()
    {
        const char *article1[5]=    { "the", "a", "one", "some", "any",  };
    	const char *noun1[5]=       { "boy", "girl", "dog", "town", "car", };
    	const char *article[5]=    { "the", "a", "one", "some", "any",  };
    	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
    	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
    	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
        int i;
    
        
    	
    
          srand( time( 0 ) );
    
          void sentenceGenerator( article, noun, verb, preposition, artical, noun )
    
        }
    }
    
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] )
    {
        int i;
    	
    	for (i = 0; i <20; i++)
    	{
            
    		printf( "%s%s%s%s%s%s\n", article1, noun1, verb, preposition, article, noun );
    	}
    }
    Last edited by jsbeckton; 10-11-2005 at 08:49 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jsbeckton
    My alogarithm will need to use the article and noun pointer arrays twice.
    Perhaps your alogarithm needs to use separate indices?
    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.*

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your declaration is now:
    Code:
    void sentenceGenerator(  char article1[],  char noun1[],  char verb[],
    					    char preposition[], char article[],  char noun[] );
    Your function definition now begins with:
    Code:
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] )
    These don't match.

    Hint: only the definition needs to contain the names of the parameters, the declaration can just contain the types.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    Just made some changes, down to one error but cant remove on of the "}" w/o a bunch of errors?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void sentenceGenerator(  char ,  char ,  char , char , char ,  char  );
    					   
    
    int main()
    {
     
    	const char *article[5]=    { "the", "a", "one", "some", "any",  };
    	const char *noun[5]=       { "boy", "girl", "dog", "town", "car", };
    	const char *verb[5]=       { "drove","jumped", "ran", "walked", "skipped", };
    	const char *preposition[5]={ "to", "from", "over", "under", "on",  };
       
    
        
    	
    	
          
    
           void sentenceGenerator( article, noun, verb, preposition, artical, noun );
           return 0;
    }
    }
    
    void sentenceGenerator(  char article[],  char noun[],  char verb[],
    					    char preposition[], char article[],  char noun[] )
    {
        int i;
    	
    	for (i = 0; i <20; i++)
    	{
            
    		printf( "%s%s%s%s%s%s\n", rand(article), rand(noun),  rand(verb++), rand(preposition++), 
    			rand(article++), rand(noun++) );
    	}
    }
    Last edited by jsbeckton; 10-13-2005 at 08:50 AM.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    You probably get an error because you have an 'artical' in there

  13. #13
    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] );
    	}
    
     }

  14. #14
    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.*

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    That was my problem I didn't understand how to generate the random words. I will work on making the sentences begin with capitol letters and end with periods. I believe that there are special functions for that.

    Have you ever heard of MatLab by the way? I believe that the reason for me taking this class is to use MatLab. I just hope its more intuitive than C.

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