Thread: Alphabetically ascended words if frequency of occurrence is equal

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    84

    Alphabetically ascended words if frequency of occurrence is equal

    Hi all,
    I have following code to print the distinct words in its input sorted into descreasing order of
    frequency of occurrence. Now I want to add following scenario: If the frequency of occurrence is
    equal for 2 words, it should be sorted alphabetically ascending.
    How this can be implemented?Could someone help me please.
    Thanks in advance.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define SUCCESS                      0
    #define CANNOT_MALLOC_WORDARRAY      1
    #define NO_WORDS_ON_INPUT            2
    #define NO_MEMORY_FOR_WORDNODE       3
    #define NO_MEMORY_FOR_WORD           4
    #define NONALPHA "1234567890 \v\f\n\t\r+=-*/\\,.;:'#~?<>|{}[]`!\".$%^&()"
     
    typedef struct WORD
    {
            char *Word;
            size_t Count;
            struct WORD *Left;
            struct WORD *Right;
    } WORD;
    
    int ReadInputToTree(
            WORD **DestTree,
            size_t *Treecount,
            FILE *Input);
    int AddToTree(
            WORD **DestTree,
            size_t *Treecount,
            char *Word);
    int WalkTree(
            WORD **DestArray,
            WORD *Word);
    int CompareCounts(
            const void *vWord1,
            const void *vWord2);
    int OutputWords(
            FILE *Dest,
            size_t Count,
            WORD **WordArray);
    void FreeTree(
            WORD *W);
    char *dupstr(
            char *s);
     
    void FreeTree(
            WORD *W)
    {
            if(W != NULL)
            {
                    if(W->Word != NULL)
                    {
                            free(W->Word);
                            W->Word = NULL;
                    }
                    if(W->Left != NULL)
                    {
                            FreeTree(W->Left);
                            W->Left = NULL;
                    }
                    if(W->Right != NULL)
                    {
                            FreeTree(W->Right);
                            W->Right = NULL;
                    }
            }
    }
    
    int AddToTree(
            WORD **DestTree,
            size_t *Treecount,
            char *Word)
    {
            int Status = SUCCESS;
            int CompResult = 0;
            if(*DestTree == NULL)
            {
                    *DestTree = malloc(sizeof **DestTree);
                    if(*DestTree == NULL)
                    {
                            Status = NO_MEMORY_FOR_WORDNODE;
                    }
                    else
                    {
                            (*DestTree)->Left = NULL;
                            (*DestTree)->Right = NULL;
                            (*DestTree)->Count = 1;
                            (*DestTree)->Word = dupstr(Word);
                            if((*DestTree)->Word == NULL)
                            {
                                    Status = NO_MEMORY_FOR_WORD;
                                    free(*DestTree);
                                    *DestTree = NULL;
                            }
                            else
                            {
                                    ++(*Treecount);
                            }
                    }
            }
            else
            {
                    CompResult = strcmp(Word, (*DestTree)->Word);
                    if(0 < CompResult)
                    {
                            Status = AddToTree(&(*DestTree)->Left, Treecount, Word);
                    }
                    else if(0 > CompResult)
                    {
                            Status = AddToTree(&(*DestTree)->Left, Treecount, Word);
                    }
                    else
                    {
                            ++(*DestTree)->Count;
                    }
            }
            return (Status);
    }
    
    int ReadInputToTree(
            WORD **DestTree,
            size_t *Treecount,
            FILE *Input)
    {
            int Status = SUCCESS;
            char *Buf = NULL;
            size_t bufsize = 0;
            char *Word = NULL;
            char *bufbase = NULL;
            char *tmp = NULL;
            if (Input != stdin)
            {
                    size_t tmp = ftell(Input);
                    fseek(Input, 0, SEEK_END);
                    bufsize = ftell(Input);
                    fseek(Input, tmp, SEEK_SET);
                    Buf = (char*)malloc(bufsize);
                    if (Buf == NULL)
                    {
                            return (-1);
                    }
                    if (fread(Buf, sizeof(char), bufsize, Input) != bufsize)
                    {
                            printf("The promised amount was not read.\n");
                    }
            }
            else
            {
                    bufsize = 256;
                    Buf = calloc(bufsize, sizeof(char));
                    if (Buf == NULL)
                    {
                            return (-1);
                    }
                    bufbase = Buf;
                    while (fgets(Buf, (int)(bufsize - (Buf - bufbase)), Input) != NULL)
                    {
                            Buf += (int)(bufsize - (Buf - bufbase)) -1;
                            if (bufbase[bufsize - 2] != 0)
                            {
                                    tmp = (char*)realloc(bufbase, bufsize * 2);
                                    if (tmp == NULL)
                                    {
                                            printf("Failed to realloc\n");
                                            free(bufbase);
                                            return (-1);
                                    }
                                    Buf = tmp + (Buf - bufbase);
                                    bufbase = tmp;
                                    bufsize *= 2;
                                    bufbase[bufsize - 2] = 0;
                            }
                            Buf = bufbase + strlen(bufbase);
                    }
                    Buf = bufbase;
            }
            Word = strtok(Buf, NONALPHA);
            while (Status == SUCCESS && Word != NULL)
            {
                    Status = AddToTree(DestTree, Treecount, Word);
                    if (Status == SUCCESS)
                    {
                            Word = strtok(NULL, NONALPHA);
                    }
            }
            free(Buf);
            return (Status);
    }
    
    int WalkTree(
            WORD **DestArray,
            WORD *Word)
    {
            int Status = SUCCESS;
            static WORD **Write = NULL;
            if(DestArray != NULL)
            {
                    Write = DestArray;
            }
            if(Word != NULL)
            {
                    *Write = Word;
                    ++(Write);
                    if(Word->Left != NULL)
                    {
                            Status = WalkTree(NULL, Word->Left);
                    }
                    if(Word->Right != NULL)
                    {
                            Status = WalkTree(NULL, Word->Right);
                    }
            }
            return (Status);
    }
    
    int CompareCounts(
            const void *vWord1,
            const void *vWord2)
    {
            int Result = 0;
            WORD * const *Word1 = vWord1;
            WORD * const *Word2 = vWord2;
            if((*Word1)->Count < (*Word2)->Count)
            {
                    Result = 1;
            }
            else if((*Word1)->Count > (*Word2)->Count)
            {
                    Result = -1;
            }
            else
            {
                    Result = 0;
            }
            return (Result);
    }
    
    int OutputWords(
            FILE *Dest,
            size_t Count,
            WORD **WordArray)
    {
            int Status = SUCCESS;
            size_t Pos = 0;
            fprintf(Dest, "Total Words : %lu\n", (unsigned long)Count);
            while(Status == SUCCESS && Pos < Count)
            {
                    fprintf(Dest, "%10lu %s\n", (unsigned long)WordArray[Pos]->Count, WordArray[Pos]->Word);
                    ++(Pos);
            }
            return (Status);
    }
    
    char *dupstr(
            char *s)
    {
            char *Result = NULL;
            size_t slen = 0;
            slen = strlen(s);
            Result = malloc(slen + 1);
            if(Result != NULL)
            {
                    memcpy(Result, s, slen);
                    *(Result + slen) = '\0';
            }
            return (Result);
    }
    
    main()
    {
            int Status = SUCCESS;
            WORD *Words = NULL;
            size_t Treecount = 0;
            WORD **WordArray = NULL;
            if(Status == SUCCESS)
            {
                    Status = ReadInputToTree(&Words, &Treecount, stdin);
            }
            if(Status == SUCCESS)
            {
                    if(0 == Treecount)
                    {
                            Status = NO_WORDS_ON_INPUT;
                    }
            }
            if(Status == SUCCESS)
            {
                    WordArray = malloc(Treecount * sizeof *WordArray);
                    if(WordArray == NULL)
                    {
                            Status = CANNOT_MALLOC_WORDARRAY;
                    }
            }
            if(Status == SUCCESS)
            {
                    Status = WalkTree(WordArray, Words);
            }
            if(Status == SUCCESS)
            {
                    qsort(WordArray, Treecount, sizeof *WordArray, CompareCounts);
            }
            if(Status == SUCCESS)
            {
                    Status = OutputWords(stdout, Treecount, WordArray);
            }
            if(WordArray != NULL)
            {
                    free(WordArray);
                    WordArray = NULL;
            }
            if(Words != NULL)
            {
                    FreeTree(Words);
                    Words = NULL;
            }
            if(Status != SUCCESS)
            {
                    fprintf(stderr, "Program failed with code %d\n", Status);
            }
            return (SUCCESS == Status ? EXIT_SUCCESS : EXIT_FAILURE);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You replace the return 0 with a strcmp call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    You replace the return 0 with a strcmp call.
    Which return 0? Could you please give me a small example with the line you mentioned?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Quote Originally Posted by Joe1903 View Post
    Which return 0? Could you please give me a small example with the line you mentioned?
    Is that the best response you can come up with after 3 days?

    Just so we're clear, you didn't write any of that code (you just found it here -> K&R2 solutions:Chapter 6:Exercise 4 - clc-wiki), and in fact you know next to nothing about C or C++.

    You know just enough google and copy/paste, but that by itself isn't enough. You need to apply knowledge by writing your OWN code from scratch.

    Anyone with even the most basic understanding of what the code was doing would at least be able to make a guess as to which function needs to be changed.

    Coupled with my suggestion, the answer should be obvious.

    Sure, you might make a mess of it and post your code changes - which would be fine, it demonstrates effort on your part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Salem View Post
    Is that the best response you can come up with after 3 days?

    Just so we're clear, you didn't write any of that code (you just found it here -> K&R2 solutions:Chapter 6:Exercise 4 - clc-wiki), and in fact you know next to nothing about C or C++.

    You know just enough google and copy/paste, but that by itself isn't enough. You need to apply knowledge by writing your OWN code from scratch.

    Anyone with even the most basic understanding of what the code was doing would at least be able to make a guess as to which function needs to be changed.

    Coupled with my suggestion, the answer should be obvious.

    Sure, you might make a mess of it and post your code changes - which would be fine, it demonstrates effort on your part.
    I have to change the return in line 248 (function OutputWords), is that correct? Additionally, in line 137and 150 I should change return -1 to Status = NO_MEMORY_FOR_WORDNODE to have the same behaviour, is that correct?
    If my thoughts are correct, I will change and post it.

  6. #6
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    No, it is the return (Result) in int CompareCounts..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Final project- Sorting words alphabetically
    By Alvin Chang in forum C Programming
    Replies: 9
    Last Post: 12-11-2014, 06:00 PM
  2. Locates the last occurrence of ch in the string
    By programming123 in forum C Programming
    Replies: 6
    Last Post: 03-17-2014, 04:57 AM
  3. Word occurrence logic
    By Madshan in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2006, 08:53 PM
  4. Counting occurrence of numbers in C
    By stabule in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 01:55 AM
  5. Count occurrence in bin tree
    By ronenk in forum C Programming
    Replies: 2
    Last Post: 12-24-2004, 11:35 AM

Tags for this Thread