>>I do not get how to show in ASCII order the words with equal frequency.

To continue the scenario from post #29, once you have the array of uniqueWords you can search allWords to determine the frequency with which each uniqueWord occurs. Rather than printing the uniqueWord and frequency to the screen you can keep a third array, wordFrequency to hold the frequency with which each uniqueWord occurs. The index of each uniqueWord in uniqueWords will be the same index in wordFreuency. Once you have completed the freuqency array you can sort the frequency array by frequency. If you use a bubble sort to sort by frequency every time you swap two elements in wordFrequency you need to swap the two element with the same indexes in uniqueWords. At the end of the sort by frequency all words in uniqueWords will be grouped by frequency, but not by ASCII order.

To sort words in uniqueWords by ASCII order within each unique frequency determine the first and last element with the same frequency withing wordFrequency. Then do a bubble sort on just that range of wordFrequency and uniqueWords exchanging elements within both arrays at the same time as before. Repeat for each new value of frequency within wordFrequency. When all sorted, print to screen by index each element in uniqueWord and wordFrequency.

All the hassle regarding keeping the uniqueWords and wordFrequency arrays in parallel order goes away if you could store both the frequency and word within a given object like an instance of a class/struct, but since you can't do that, using parallel arrays can get the job done.

Sir I appreciate your suggestions but I'm really a noob with regards to C such that i don't know how to implement it. Could you give me any sample/hint. Sorry for being very slow with this part. Hope you could understand.

Sir I appreciate your suggestions but I'm really a noob with regards to C such that i don't know how to implement it. Could you give me any sample/hint. Sorry for being very slow with this part. Hope you could understand.

Follow Dave's suggestion at your GID post. With all the help you've been given so far here and there you should have this program done. Or are you getting confused because the two threads are telling you to do different things?

Thanks sir. I was really confused with the ASCII part. Sorry for my shortcomings. I'll do my code as soon as my exam tomorrow is over. I'll get back with my results. Thanks again.

hi again sir. I have tried sir dave's suggestion but i have not succeeded with it. I really dont know what i did wrong. The sorting part is another thing that should be done and I'm having a hard time doing it (Should I BubbleSort, qsort, char-by-char sort ?) I'm really confused right now. Hope you could help. Thanks.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char Words[5000][500];
int WordCounter[5000];
int UniqWords;
int TotWords;
char Holding[5000][500];
int CountHolding[5000];
int Maxcount;
int num;

void NewWord(char *Word){

    int i;

    if (strlen(Word)==0){
        return;
    }

    if ((Word[0]=='-')&&(Word[1]=0)){
        return;
    }
    TotWords++;

    for (i=0;i<UniqWords;i++){

        if (strcmp(Words[i],Word)==0){

            WordCounter[i]++;
            return;
        }

    }

    strcpy(Words[UniqWords],Word);

    WordCounter[UniqWords]=1;

    UniqWords++;
}

int main()
{

    char Character;

    char CurrentWord[500];

    int index;

    int i,j,Max;


    UniqWords=0;
    TotWords=0;
    index=0;


    while ((Character=getchar())!=EOF){

        Character=tolower(Character);

        if (((Character>='0')&&(Character<='9'))||
                ((Character>='a')&&(Character<='z'))||
                (Character=='-')||(Character=='\'')||(Character=='&'))
        {


            CurrentWord[index++]=Character;
        }
        else
        {
            CurrentWord[index]=0;
            NewWord(CurrentWord);
            index=0;
        }
    }

    Maxcount = 0;
        for (i = 0; i < UniqWords; i++) {
        if (WordCounter[i] > Maxcount) {
            Maxcount = WordCounter[i];    
        }
    }


    num = 0;
    for (i = 0; i <UniqWords; i++) {
        if (WordCounter[i] == Maxcount) {
        strcpy(Holding[num],Words[i]);
        num++;
        }
    }
         
    for (num = 0; num < Maxcount; num++){
        printf("%s - %d\n", Holding[num], Maxcount);
    }
    
    printf("There are %d words with frequency = %d\n", num, Maxcount);
    

    return 0;
}

Below is your code after the while loop in main() has completed. I have added comments and suggestions based on your previous posts.

/*
 find maximum frequency within WordCounter 
*/
Maxcount = 0;
for (i = 0; i < UniqWords; i++) {
   if (WordCounter[i] > Maxcount) {
      Maxcount = WordCounter[i];    
   }
}

/*
find all elements of Words with frequency equal to Maxcount and store in Holding
*/
num = 0;
for (i = 0; i <UniqWords; i++) {
   if (WordCounter[i] == Maxcount) {
      strcpy(Holding[num],Words[i]);
      num++;
   }
}

/*
Print elements of Holding.

Note the following:
      1) elements of Holding have not been sorted alphabetically yet.  Do that before printing out the elements of Holding, as previously indicated, if that's truly what you want to do.  You can use whatever sorting algorhithm you want to sort Holding.
      2) You don't want to change the value of num obtained in the last section in the following loop since it is the number of words in Holding that you need to print.   Instead you could use index i starting at 0 to index i less than num and you want to print Holding[i], not Holding[num]
*/
for (num = 0; num < Maxcount; num++){
   printf("%s - %d\n", Holding[num], Maxcount);
}

printf("There are %d words with frequency = %d\n", num, Maxcount);

Now you want to put all that in a loop and redo it for every unique frequency in WordCounter starting with the maximum unique frequency in WordCounter and working down to lowest unique frequency in WordCounter. Each time you loop through the above you have to lower the value of Maxcounter to the next lowest unique frequency in WordCounter.

Good luck.

Sir I have researched on algorithms and have used qsort. Problem is I can't get it to run correctly. I dont know what I've done wrong..:c

Hope that you guys could point out to me what I should revise/add to make this thing work. I think i'm almost there. Thanks in advance.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char Words[5000][500];
int WordCounter[5000];
int UniqWords;
int TotWords;
char Holding[5000][500];
int CountHolding[5000];
int Maxcount;
int num;

void NewWord(char *Word){

    int i;

    if (strlen(Word)==0){
        return;
    }

    if ((Word[0]=='-')&&(Word[1]=0)){
        return;
    }
    TotWords++;

    for (i=0;i<UniqWords;i++){

        if (strcmp(Words[i],Word)==0){

            WordCounter[i]++;
            return;
        }

    }

    strcpy(Words[UniqWords],Word);

    WordCounter[UniqWords]=1;

    UniqWords++;
}

void print_array(char *v[], int y, int z)
{
     int i;
     
      for (i=0; i<y; i++)
      {
        printf("%s - %d\n ",Holding[i] ,z);
      }
}

void swap(char *v[], int i, int j)
{
     char *temp;
     
     temp = v[i];
     v[i] = v[j];
     v[j] = temp;
     
}

void qsort(char *v[], int left, int right)
{
     int i, last;
     void swap(char *v[], int i, int j);
     if (left >= right)
     return;
     swap(v, left, (left+right)/2);
     last = left;
     for (i = left + 1; i <= right; i++){
         if (strcmp(v[i], v) < 0)
             swap(v, ++last, i);
             }
     swap(v, left, last);
     qsort(v, left, last-1);
     qsort(v, last+1, right);
     
}

void sort_array(char *v[], int x)
{
     int num = 0;
     int i;
     
    for (i = 0; i <UniqWords; i++) {
        if (WordCounter[i] == x) {
        strcpy(Holding[num],Words[i]);
        num++;
        print_array(Holding, num, x);
            }
        }
     
}

int main()
{

    char Character;

    char CurrentWord[500];

    int index;

    int i,j,t;


    UniqWords=0;
    TotWords=0;
    index=0;
    
    while ( (Character=getchar()) != EOF){

    Character = tolower(Character);
    
           
            if (((Character>='0')&&(Character<='9'))||
                    ((Character>='a')&&(Character<='z'))||
                    (Character=='-')||(Character=='\'')||(Character=='&'))
            {
    
    
                CurrentWord[index++]=Character;
            }
            else
            {
                CurrentWord[index]=0;
                NewWord(CurrentWord);
                index=0;
            }
        }
    
        Maxcount = 0;
            for (i = 0; i < UniqWords; i++) {
            while (WordCounter[i] > Maxcount) {
                Maxcount = WordCounter[i];    
                sort_array(Holding, Maxcount);
            }
        }   
    
    

    return 0;
}
Member Avatar for iamthwee

why don't you use qsort from the library?

Sir I have i think my last question(hopefully:p) regarding printing the sorted array.

Input.txt:

This is line 1.
I have 1 more line.
The quick brown fox jumps, etc.
This is the pits.
From Yoda: The last line is this.
This is it.

Problem: In the output after redirecting input.txt those with lower frequency has either repeated words or lacks several words. In addition to that several <null> were printed.

Actual Output:

is – 4
this – 4
<null> - 4
line – 3
the - 3
<null> - 3
1 - 2
brown - 1
There are 0 words with frequency = 0

Hope you could help me. Thanks in advance.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char Words[5000][500];
int WordCounter[5000];
int UniqWords;
int TotWords;
char Holding[5000][500];
int CountHolding[5000];
int Maxcount;
char *w[1000];
int num;

void NewWord(char *Word){

    int i;

    if (strlen(Word)==0){
        return;
    }

    if ((Word[0]=='-')&&(Word[1]=0)){
        return;
    }
    TotWords++;

    for (i=0;i<UniqWords;i++){

        if (strcmp(Words[i],Word)==0){

            WordCounter[i]++;
            return;
        }
    }

    strcpy(Words[UniqWords],Word);

    WordCounter[UniqWords]=1;

    UniqWords++;
}

void swap(char **p, char **q) {
    char *tmp;
    
    tmp = *q;
    *q = *p;
    *p = tmp;
    return;
}

void bubble(char *a[], int n) {
    int i, j;    

    for(i=0;  i < n-1;  ++i)
        for(j=n-1; j > i;  --j)
            if( strcmp(a[j-1],a[j])>0)
                swap(&a[j-1], &a[j]);
    return;
}

void sort(int x)
{
     int j;
     int num = 0;
    for (j = 0; j <UniqWords; j++) {
        if (WordCounter[j] == x) {
        strcpy(Holding[num],Words[j]);
        w[num] = Holding[num];
        num++;
        }
    }
         
         bubble(w, num); 
    for (num = 0; num < x; num++){
        printf("%s - %d\n", w[num], x);
    }
}

int main()
{

    char Character;

    char CurrentWord[500];

    int index;

    int i;

    UniqWords=0;
    TotWords=0;
    index=0;

    while ( (Character=getchar()) != EOF){

    Character = tolower(Character);

        if (((Character>='0')&&(Character<='9'))||
                ((Character>='a')&&(Character<='z'))||
                (Character=='-')||(Character=='\'')||(Character=='&'))
        {


            CurrentWord[index++]=Character;
        }
        else
        {
            CurrentWord[index]=0;
            NewWord(CurrentWord);
            index=0;
        }
    }

    Maxcount = 0;
    num = 0;    
       for (i = 0; i < UniqWords; i++) {
        if (WordCounter[i] > Maxcount) {
            Maxcount = WordCounter[i];    
        }
    }

    while (Maxcount != 0){
    sort(Maxcount);
    Maxcount--;
    }
    printf("There are %d words with frequency = %d\n", num, Maxcount);
    

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.