loops - Abort trap: 6 error in C -


i have 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 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 highest number of appearances is: %s",ans);     printf("\nnumber of appearances: %d",max_freq);      return 0;  } 

when compile gcc, not give me error output gives me abort trap:6 when instead should giving me:

the word highest number of appearances is:

number of appearances:

i have looked @ other posts have not been of help. appreciated.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -