The code under discussion is to check whether or not two arrays are equal. There are several ways of solving this question, hashing being one of them. I've tried to implement the same. If the inputs are in the range of 1 to 9 then I get a correct output. But if the inputs are above 100 then there is a segmentation fault error. How do I solve this error?
Thanks in advance.
Here's the code for your reference:
#include<stdio.h>
int main()
{
int a[5],b[5],i,f=0;
int freq[5]={0};
printf("Enter elements for first array\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nEnter elements for second array\n");
for(i=0;i<5;i++)
scanf("%d",&b[i]);
for(i=0;i<5;i++)
freq[a[i]]++;
for(i=0;i<5;i++)
freq[b[i]]--;
for(i=0;i<5;i++)
{
if(freq[i]!=0)
{
f=1;
break;
}
}
if(f==1)
printf("Not same");
else
printf("Same");
return 0;
}
int freq[5]can handle inputs above 100, or in the range 1 to 9 for that matter. Oh and there is no trace of a hash table in there. You are building a frequency table, which has nothing to do with a hash table. - n. 1.8e9-where's-my-share m.freqarray. Your array has a fixed size of 5. Any integer outside of [0;4] is an index error and a potential segfault. - AugustinLopez