CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    ( Plain C ) Am I using everything correctly?

    This is ' C ' not C++. If there is a plain C board please redirect me. Thanks.

    My instructions for the program
    Code:
    Problem 8.11 on page 366  (35 points)
    
        You will need to include time.h, string.h, and ctype.h in addition to stdio.h and stdlib.h
    
    Also you will have 4 arrays of char pointers. I am giving an example for one of them, the rest you can figure it out.
    
                    char *article[]={"the","a","one","some","any"};
    
    Also please user 100 characters for complete sentence. So, char sentence[100]="";
    
    Make sure you capitalize first letter of each sentence and end it with a period.
    
    Needless to say you will be using rand() % 5 to pick up a random word from each array. 
    
    To construct a sentence, you will do the following:
    
    article <space> noun <space> verb <space> preposition <space> article <space> noun 
    
    After that capitalize the first letter of sentence and end the sentence with a period
    
    e.g. One town walked on a girl.
    
    About modifying the program to create short story: you can choose to do it or not. If you do it that would be great otherwise if you are short on time, don't bother about it.
    
    Using the rand function properly for each array to pick up the word (8 points)
    
    To construct the sentence by putting the words concatenated into array in the specified order (15 points)
    
    To make sure capitalize the first letter of each sentence and end with a period ( 5 points)
    
    To loop so that 20 sentences are successfully created ( 5 points)
    
    Program run successfully without infinite loop, No logical errors, no runtime errors, no syntax errors (2 points)

    My Code
    Code:
    /*  Gerald Eckert
        10/19/09
        Assignment 8
    
        Input: No Input.
        Output: This program will display 20 random sentances by selecting words at random.
        Functionality: Using 4 arrays article, noun, verb and preposition
            this program will produce 20 random sentances and display them to the screen.
    
    
        Sentance Structure: article<space>noun<space>verb<space>preposition<space>article<space>noun
            1) First word will be capitalized.
            2) There will be a period at the end of the sentance.
    */
    
    
    #include <stdio.h>   /* printf() */
    #include <stdlib.h>  /* srand() , rand() */
    #include <time.h>    /* time() */
    #include <string.h>  /* strcat()  */
    #include <ctype.h>   /* toupper() */
    
    /* WORDS */
    const char *ARTICLE[]={"the","a","one","some","any"};
    const char *NOUN[]={"boy","girl","dog","town","car"};
    const char *VERB[]={"drove","jumped","ran","walked","skipped"};
    const char *PREPOS[]={"to","from","over","under","on"};
    
    
    int main()
    {
        /* Sentance */
        char sentance[100]="";
    
        /* seed for the rand() function */
        srand( time(NULL));
    
        /* Produce 20 random sentances */
        int i;
        for(i=0;i<20;i++)
        {
            /* Constructing the sentance using random words*/
            strcat(sentance, ARTICLE[rand()%5]);
            strcat(sentance, " ");
            strcat(sentance, NOUN[rand()%5]);
            strcat(sentance, " ");
            strcat(sentance, VERB[rand()%5]);
            strcat(sentance, " ");
            strcat(sentance, PREPOS[rand()%5]);
            strcat(sentance, " ");
            strcat(sentance, ARTICLE[rand()%5]);
            strcat(sentance, " ");
            strcat(sentance, NOUN[rand()%5]);
    
            /* Capitalize the first Letter */
            sentance[0]= toupper(sentance[0]);
    
            /* Adding a period at the end */
            strcat(sentance,".");
        
            /* Print the sentance */
            printf("%d: %s\n",i+1,sentance);
    
            /* Clear the sentance */
            sentance[0]='\0';
        }
    
        return(0);
    }
    Is it OK to put the \0 at index 0 of sentance as a way of "clearing" the array.
    I used that because I am using strcat() to change the array and strcat() looks for the \0 and starts writing at that point. Anything else
    Google is your friend.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: ( Plain C ) Am I using everything correctly?

    Yes, in C, it's ok to write sentance[0]='\0';
    No, in English, it's not ok, because it is "sentence" with "e" instead of "a".

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: ( Plain C ) Am I using everything correctly?

    No, in English, it's not ok, because it is "sentence" with "e" instead of "a".
    ****, I need a spellchecker
    Google is your friend.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured