
Untitled
By: a guest on
Jun 27th, 2012 | syntax:
None | size: 0.72 KB | hits: 9 | expires: Never
Why does a negative value give segmentation fault for the program which is to find pairs of value from the array which has the given sum?
#include <stdio.h>
void findpairs(int arr[], int arr_size, int sum)
{
int i, temp;
int hash[100] = {0};
for(i = 0; i < arr_size; i++)
{
temp = sum - arr[i];
if(hash[temp] == 1)
{
printf("Pair with given sum %d is (%d, %d) n", sum, arr[i], temp);
}
hash[arr[i]] = 1;
}
}
int main()
{
int A[] = {4,-4,9,2,1,6,5,11};
int sum =7;
int arr_size = 8;
findpairs(A, arr_size, sum);
return 0;
}
hash[arr[i]] = 1;
...
hash[-4] = 1;
temp = 7 - 9;
if (hash[temp] == 1)
temp = sum - arr[i];
if(hash[temp] == 1)
hash[arr[i]] = 1;