piby180 - 1 year ago 449
C Question

# Find the lonely integer in an array

Please refer to this hackerrank challenge if you can.

The problem is to find the lonely integer in an array, given an array consists of only pairs except one lonely integer.

The problem is with this test case

``````9
4 9 95 93 57 4 57 93 9
``````

9 is array size and below is the array

See the part of code highlighted by //------

If I place scanf("%d", &n) above int arr[n] code works fine, but gives horrible results the other way round. Please help me out

``````#include <stdio.h>

int lonely_integer(int* a, int size);

int main(){
//n is size of array, i is counter variable
int n, i, result;
// ---------------------
int arr[n];
scanf("%d", &n);
// ---------------------
printf("%d\n", n);
for(i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
result = lonely_integer(arr, n);
printf("%d", result);
return 0;
}

int lonely_integer(int* a, int size){
int i;
int res = 0;
for(i = 0; i < size; i++){
res = res ^ a[i];
}

return res;
}
``````

You'd want to use:

``````#include <stdlib.h>
/* ... */
int *arr;
scanf("%d", &n);
arr = malloc(sizeof(int) * n);
``````

This way, `arr` gets dynamically allocated at runtime, so it can be of any size depending on the input `n`.

What you were originally doing (i.e. declaring `arr[n]` after recieving `n` via scanf: `scanf("%d", &n); int arr[n];`) is not a good idea because it makes use of Variable-Length Arrays, a feature of C that is not mandatory in the latest C standard.

You see, `arr` gets created at compile-time, and you normally can only initialize it with a constant expression known at compile-time, which `n`, a variable recieved as user input, obviously isn't. Variable-length arrays are a feature of the language that basically allows you to bypass this rule, i.e. they make you able to initialize an array to a length not known at compile-time. This has been standartized in C99, but was listed as "optional" as of C11.

What you did after that (`int arr[n]; scanf("%d", &n);`) is quite illogical because, well, you declare `arr` as an array of `n` integers before you recieve the value of `n` as user input, and, well, know its value. It prints garbage because `n` is originally initialized to an unspecified "garbage" value, and this is what your VLA's size becomes when you declare it:

``````int arr[n]; //n is garbage at this point, you have no idea how large arr will be!
scanf("%d", &n); //you got the value of n that you needed, but too late, alas!
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download