Another assignment problem

This is a discussion on Another assignment problem within the C Programming forums, part of the General Programming Boards category; This one doesn't sound very hard but I wanted to 2nd opinions on the directions. They don't make very much ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    Another assignment problem

    This one doesn't sound very hard but I wanted to 2nd opinions on the directions. They don't make very much sense to me. Here are the directions:

    "Write a program that finds the "smallest" and "largest" in a series of words. After the user enters the words, the program will determine which words would come first and last if the words were listed in dictionary order. The program must stop accepting input when the user enters a four-letter word. Assume the no word is more than 20 letters long. An interactive session with the program might look like this:

    Enter word: dog
    Enter word: zebra
    Enter word: rabbit
    Enter word: catfish
    Enter word: walrus
    Enter word: cat
    Enter word: fish

    Smallest word: cat
    Largest word: zebra

    Hint: Use two strings named smallest_word and largest_word to keep track of the "smallest" and "largest" words entered so far. Each time the user enters a new word, use strcmp to compare it with the smallest_word; if the new word is "smaller," use strcpy to save it in smallest_word. Do a similar comparison with largest_word. Use strlen to determine when the user has entered a four-letter word."

    Ok, here's my problem. Does it want me to arrange the words in alphabetical order or not? The example doesn't show it printing them out in alphabetical order or anything. Also, does it want the word to be four letters or twenty letters? It says to stop accepting input when four letters are entered but then it says assume no word is more than twenty letters. Does this make any sense to you guys?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Does it want me to arrange the words in alphabetical order or not?
    Does it ask you to or not? The hints section is pretty clear in what it expects of you.

    >>does it want the word to be four letters or twenty letters?
    >>Does this make any sense to you guys?
    Just like it says:
    - no word is more than twenty letters
    - stop accepting input when the user enters a four-letter word

    The answers are in the question!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It makes perfect sense to me. read the hint because its a good hint. You don't have to arrange anything, infact all you have to keep is the smallest and largest word. It tells you to stop when they enter a 4 letter word. The part about assuming to word is more then 20 letters just lets you use a fixed size buffer to read into and copy into.

    Maybe you are getting confused by the smallest and largest part. Lets go with something you should know, numbers:
    0 3 2 8 10 11 6

    Whats the smallest number? The largest number? How did you figure that out? You can use the same process as outlined for the words as you can for the numbers.

    Now go lookup what strcmp() returns

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, I get it then. why do the directions say "after the user enters the words, the program will determine which words would come first and last if the words were listed in dictionary order." doesn't that mean arrange them in alphabetical order? also, on the example they give of the output, the word "zebra" is 5 characters long. Another reason that I am confused because it says stop taking input after 4 characters. Here is the code I have to read 4 characters entered by the user so far.

    Code:
    #include <stdio.h>
    
    #define N 20
    
    int read_line(char str[], int n);
    
    int main()
    {  
       int i=4;
       char word1[N], word2[N], word3[N], word4[N], word5[N], word6[N], word7[N];
       
       printf ("Enter a word: ");
       read_line(word1, i);
       printf("%s\n", word1);
    
    return 0;
    
    }
    
    int read_line (char str[], int n)
    {
       char ch;
       int i = 0;
    
       while ((ch = getchar()) != '\n')
         if (i < n)
           str[i++] = ch;
    
       str[i] = '\0';
       return i;
    }
    Is this what it means by only accepting four characters?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>why do the directions say ...
    >>doesn't that mean arrange them in alphabetical order?
    No, it means work out which words would be first and last. You can do that without storing them all at the same time, therefore you won't need to arrange them.

    >>it says stop taking input after 4 characters
    No, it says stop when the user enters a four-letter word, not when they enter 4 characters.

    Your code needs changing to allow the user to enter upto 20 characters for a word.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I really am confused on this: "No, it says stop when the user enters a four-letter word, not when they enter 4 characters." What's the difference? so should I just change the variable 'i' to 20 and it will be fine? where in the code do I make the 4 letter word rule and how would I do it because I really don't understand what this means?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to do the following:

    - Read a line, maximum 20 characters
    - Check the length of the word entered, if it's 4, stop reading.

    The difference between a four letter word, and four characters, is that a 5 letter word starts with 4 characters Stopping when the user enters these 4 characters of the 5 letter word would be incorrect.

    There's already a function to get a line of data, it's called fgets().
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    You can use strlen() to determine the length of the word entered.

    You don't need all those word1, word2 etc arrays. Just 3 are needed, one each for the current smallest and largest, and one for the word you're currently reading.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I was wondering in the prototype you gave me, what exactly is GOOD_READ supposed to be? Also, do I even need my read_line function anymore?

  9. #9
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    This is just sorting. Try search for 'sorting strings'.

    *edited

    You may wanna look into 2d arrays for storing strings into an array.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    GOOD_READ (refers to some code in a PM), represents a return value that simply means that the read was successfully performed. It could be implemented in any way you see fit.

    And no, you don't need your read_line, you can use fgets(). It's up to you

    @loko: This is not about sorting at all. Please don't confuse the issue anymore than it already is
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    does this not replace my read_line function? I was thinking the two were equivalent or something. I don't understand really what's going on here. do the contents of my read_line function go within here or is this something just completely different? I'm not too familiar with fgets() yet so I wouldn't feel confident using it. Also, I'm not quite sure how to implement the GOOD_READ. How would I make the program know if it properly read a string or not?

    Code:
      if (strlen(current) == 4)
      {
    	/*
    	 * 4 character string entered, 
    	 * set flag to terminate loop
    	 */
    	break;
      }
       return i;
    Last edited by the_winky_files; 10-02-2005 at 07:05 PM.

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Hammer
    @loko: This is not about sorting at all. Please don't confuse the issue anymore than it already is

    Yes my bad lol. I didnt read the problem thouroughly. I just look at it and replied.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char  smallest[SIZE];
    char  largest[SIZE];
    char  current[SIZE];
    
    while (read_line(current, SIZE) == GOOD_READ) < --Read one line
    {
      /*
       * here would be the part that 
       * tests current against smallest and largest
       */
      if (current < smallest)	 //<-- perform test to see if this is the smallest so far
      {
    	copy(smallest, current);
      }
      else if (current > largest) //<-- perform test to see if this is the largest so far
      {
    	copy(largest, current);
      }
    
      if (strlen(current) == 4)   //<-- perform test to see if current is 4 characters long
      {
    	/*
    	 * 4 character string entered, 
    	 * break out of the loop
    	 */
    	break;
      }
    }
    Note: the tests here:
    if (current < smallest)
    need to be done using strcmp(), you cannot do exactly as I've shown (that was for concept only)

    I can't give you much more help without actually writing it all for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Is the code below correct? It runs good but I'm not exactly sure if this is what the question is asking.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define size 20
    
    int main()
    {
    
      char largest_word[size];
      char smalest_word[size];
      char word[size];
      int ch;
    
      printf("Enter a word: ");
      scanf("%s", word);
      strcpy(largest_word, word);
      strcpy(smalest_word, word);
    
      while(strlen(word) != 4 )
      {
        printf("Enter a word: ");
        scanf("%s", word);
        if( strcmp(smalest_word, word) > 0 )
          strcpy(smalest_word, word);
        if( strcmp(largest_word, word) < 0 )
          strcpy(largest_word, word);
      }
    
      printf("The Smalest word: %s\n", smalest_word);
      printf("The Largest word: %s\n", largest_word);
    
      return 0;
    }

  15. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    An string is a series of characters terminated by a \0, so if you have a string say "hello" in C it's stored as "hello\0" so you need to make space for that one extra character that is automatically added to your string.

    So if I had a string that has a maximum of 5 characters, i need an array size of 6 characters to store the extra \0, do you understand what I'm getting at?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment problem
    By cboard_member in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 01:16 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. problem on assignment
    By rodney1001 in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 02:49 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21