Thread: Using dynamically allocated strings to sort words

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    9

    Using dynamically allocated strings to sort words

    Here are the instructions:

    Write a program that sorts a series of words entered by the user:

    Enter word: foo
    Enter word: bar
    Enter word: baz
    Enter word: quux
    Enter word:

    In sorted order: bar baz foo quux

    Assume that each word is no more than 20 characters long. Assume there are no more than 50 words to be sorted. Stop reading when the user enters an empty word (i.e., press Enter without entering a word). Store each word in a dynamically allocated string, using an array of pointers to keep track of the strings. After all the words have been read, sort the array and then print the words in a sorted order.

    Hint:
    1) Use the read_line function (textbook, page 287 or slide 20, July 2 lecture) to read each word.
    2) Use qsort function in <stdlib.h> to sort an array of strings.

    I have been getting the following error and warning when I attempt to compile:

    sort_words.c:18: error: invalid operands to binary *
    sort_words.c:30: warning: comparison between pointer and integer

    Where exactly did I go wrong? What do I have to do to get the words to sort in order?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define WORD_LEN 50
    #define WORD_LIMIT 20
    
    int read_line(char str[], int n);
    
    int int_compare(const void* p, const void* q);
    
    int main()
    {
    char n[WORD_LIMIT];
    int i;
    int *a;
    
    a = malloc(n * sizeof(char));
    
    for(i = 0; i < WORD_LEN; i++)
    {
    printf("Enter a word: ");
    scanf("%c", &n[i]);
    } 
    
    qsort(n, WORD_LEN, sizeof(char), int_compare);
    
    printf("In sorted order:\n");
    
    for(i = 0; i < n; i++)
    printf("%c\t", a[i]);
    
    printf("\n");
    
    return 0;
    }
    
    int int_compare(const void* p, const void* q){
    
    int n1 = *(int *) p;
    int n2 = *(int *) q;
    
    if (n1 < n2)
    return -1;
    
    if (n1 == n2)
    return 0;
    
    return 1;
    }
    
    
    
    int read_line(char str[], int n)
    {
    int ch, i = 0;
    while ((ch = getchar()) != '\n') {
    if (i < n)
    str[i++] = ch;
    }
    str[i] = '\0';
    return i;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Variables with more than one letter names are nice. INDENTING is even nicer!

    Code:
    char n[WORD_LIMIT];
    int i;
    int *a;
    
    a = malloc(n * sizeof(char));
    Why do you use "n" in the 2nd case? What is "a" supposed to be for? I think you have misunderstood a few significant things, it is hard to decide exactly how or why, but if you can answer those two questions it will be easier.

    The other error is related to that
    Code:
    for(i = 0; i < n; i++)
    n is not an integer, it is a character string.
    Last edited by MK27; 07-12-2009 at 08:35 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This is incorrect as well:


    Code:
     printf("Enter a word: ");
     scanf("%c", &n[i]);     /* n alone will suffice here no need for '&' */

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also in looking at this a bit closer what you need is two demensional array allocation. You need to dynamically allocate for


    Code:
    For instance:
    
    int **a = mallocMy2dArray(WORD_LIMIT, WORD_LEN);
    
     ...
    
    int **mallocMy2dArray(int WLim, int Wlen)
    {
    
      your code here to accomplish this
    
    }

    Also are you expecting your users to try typing in WORD_LEN number of words, this is what your for loop is doing.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slingerland3g View Post
    Also in looking at this a bit closer what you need is two demensional array allocation. You need to dynamically allocate for


    Code:
    For instance:
    
    int **a = mallocMy2dArray(WORD_LIMIT, WORD_LEN);
    
     ...
    
    int **mallocMy2dArray(int WLim, int Wlen)
    {
    
      your code here to accomplish this
    
    }

    Also are you expecting your users to try typing in WORD_LEN number of words, this is what your for loop is doing.
    Nah, he just needs to make the n[WORD_LIMIT] be a *n[WORD_LIMIT]. Why over complicate the matter?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocated strings??
    By CS_Student8337 in forum C Programming
    Replies: 18
    Last Post: 03-19-2009, 05:14 AM
  2. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  3. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  5. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 03:49 PM