We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Amlendra Kumar • 4 years ago

Here I have found another good article on pointer in C.

srinivas nekkanti • 4 years ago

int getMajorityNumber( int *a, int size, int maxNum)
{
int majorNum = size / 2;
int i = 0, maxNum = 100;

int *hash = (int *) malloc (sizeof(int) * (maxNum + 1) );
memset(&hash, 0x0, ( sizeof(int) * (maxNum+ 1) ));

for ( i =0; i < size; i++ )
{
hash[a[i]]++; <<<<<<<<<<<<================segmentation fault here any solution for this...
if ( hash[a[i]] > majorNum )
return a[i];
}

return -1;
}

int main() {
//code
int testCases = 0;
int arraySize = 0;
int maxNum = 0, i = 0, k =0;

scanf("%d",&testCases);
for ( i = 0; i < testCases; i++ )
{
scanf("%d",&arraySize);
int *a = (int *) malloc (sizeof(int) * arraySize);
for ( k = 0; k < arraySize; k++ )
{
scanf("%d", &a[k]);
if ( a[k] > maxNum ) maxNum = a[k];
}
printf("%d\n", getMajorityNumber(a, arraySize, maxNum));
free(a);
}
return 0;

Please read this article if anyone want to learn Pointers in C
https://www.thecprogramming...

zedxin • 6 years ago

In the last loop warning even garbage values (values in pointer) are increasing by 2 and they change after every execution.

Donotalo • 6 years ago

zedxin: What do you mean by "last loop warning"? Which example?