0

I have this code:

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

unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    int c;

    while ((c = *str++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}
int main()
{

    char s[50],ans[50];

    int max_freq = 0,num=0,hash_value,i;
    unsigned long int a[10000],b[10000];


    FILE *fp = fopen("/Users/Brikman23/Desktop/input.txt","r"); //remember to specify correct path of input file..

    if (fp==NULL)
    {
        printf("Error: Input File not found..");
        return -1;
    }

    while(fscanf(fp,"%s",s)==1)
    {
        hash_value = hash(s);
        for(i=0;i<num;i++)
        if(a[i]==hash_value)
        break;

        if(i==num)
        {
            b[num]=1;
            a[num]=hash_value;

            if(b[num]>max_freq)
            {
                max_freq = b[num];
                strcpy(ans,s);

            }   

            num++;  
        }
        else
        {
            b[i]++;
            if(b[i]>max_freq)
            {
                max_freq = b[i];
                strcpy(ans,s);

            }   

        }   


    }

    printf("The word with the highest number of appearances is: %s",ans);
    printf("\nNumber of appearances: %d",max_freq);

    return 0;

}

when I compile this with gcc, it does not give me an error but the output always gives me Abort trap:6 when instead it should be giving me:

The word with the highest number of appearances is:

Number of appearances:

I have looked at other posts but they have not been of help. Any help would be greatly appreciated.

  • Is [this](http://stackoverflow.com/questions/26431147/abort-trap-6-error-in-c) from you too? The answer's accepted. The question does look similar but not the code – Eregrith Apr 22 '15 at 15:24
  • @Eregrith That isn't the same. – 2501 Apr 22 '15 at 15:28
  • nope, that post is not mine.... – Brikman23 Apr 22 '15 at 15:29
  • any ideas to why I am getting this error? – Brikman23 Apr 22 '15 at 15:31
  • 1
    Do you make sure the code is never fed a line of more then 49 `char`s long? Better change `fscanf(fp,"%s",s)==1` to be `fscanf(fp,"%49s",s)==1`. – alk Apr 22 '15 at 15:43
  • how would you initialize it? ...I am unsure – Brikman23 Apr 22 '15 at 15:46
  • You got it ALK! The output is now correct! Thank you! – Brikman23 Apr 22 '15 at 15:49
  • @Brikman23 only if the lines do not differ after the 49th char. – mch Apr 22 '15 at 15:52
  • @Brikman23, and only if the most frequently occuring value is shorter than 50 chars. – John Bollinger Apr 22 '15 at 16:04
  • the does not actually cleanly compile. there are at least 3 warnings that need fixed. the most obvious problems are the 3 comparisons between signed and unsigned values – user3629249 Apr 22 '15 at 16:25
  • there are (at least) 3 magic numbers in the code .. 50, 10000, and 5381. The should be #defined name (value) where value is 50, 10000, and 5381. where name is a meaningful name that indicates what the number means, in the context of the code. – user3629249 Apr 22 '15 at 16:29
  • regarding the line: 'while(fscanf(fp,"%s",s)==1)' there is nothing limiting the length of the string being read. Therefore buffer overflow can occur. This would be undefined behaviour, possibly leading to a seg fault event. suggest placing a length modifier in the '%s' input/conversion specifier. – user3629249 Apr 22 '15 at 16:34
  • these three lines: ' for(i=0;i – user3629249 Apr 22 '15 at 16:39
  • variable names should be meaningful, in the context where they are being used. 'a', 'b', 's' are not meaningful variable names. I,.E. they give no indication of what they contain nor their intended usage. – user3629249 Apr 22 '15 at 16:42
  • the posted code has a bug, wherein if more than one word has the same number of appearances, then the code only displays the first word encountered with that number of appearances. Note: I'm running Linux ubuntu 14.04 and the code ran fine (however, I did not have any words over 49 characters long – user3629249 Apr 22 '15 at 16:53

0 Answers0