Find a single integer in an array

Please refer to this hackerrank if you can.

The challenge is to find a single integer in the array, since the array consists of only pairs except one single integer.

The problem is this test case.

9 4 9 95 93 57 4 57 93 9 

9 - the size of the array, and below - the array

See the code part highlighted // ------

If I put scanf ("% d", & n) above int arr [n], the code works fine, but gives terrible results in the reverse order. Please help me

 #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; } 
+5
source share
4 answers

Do you want to use:

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

Thus, arr gets a dynamic allocation at runtime, so it can be of any size depending on input n .

What you originally did (that is, declaring arr[n] after receiving n via scanf: scanf("%d", &n); int arr[n]; ) is not a good idea, because it uses variable-length Arrays , function C, which is optional in the latest standard C.

You see that arr is created at compile time, and you can usually initialize it only with a constant expression known at compile time, which n , the variable obtained as user input, is not explicitly. Arrays of variable length are a sign of a language that basically allows you to bypass this rule, that is, they allow you to initialize an array with a length not known at compile time. It was standardized in C99 , but was listed as "optional" as C11 .

What you did after that ( int arr[n]; scanf("%d", &n); ) is pretty illogical, because you declare arr as an array of integers n before you get the value n as user input, and, know its value. It prints garbage because n initially initialized with an undefined garbage value, and this is what your VLA 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! 
+1
source

The range n asked in question 1 <= N < 100 , which is small and an array of variable length can be used. But you are doing wrong here.

 int arr[n]; // n is uninitialized. Its value is indeterminate. scanf("%d", &n); 

You need to initialize n before using it as the size of the array

 scanf("%d", &n); int arr[n]; 
+1
source

Allocating an array with an uninitialized variable will lead to undefined behavior, and the compiler will display a warning "variable uninitialized in this function"

If you get the size of the array at runtime, it would be wise to use dynamic memory allocation, as @ Mints97 sent

 int data_size; int *data_array; scanf("%d", &data_size); data_array = (int*)calloc(data_size,sizeof(int)); /* . */ // Free the memory at the end free(data_array); data_array = NULL; 

If you want to set the size of the array at compile time, you can define a macro

 #define DATA_SIZE 9 

or set the macro when compiling the code

 gcc test.c -o test -DDATA_SIZE=9 -Wall 
0
source

The value of 'n' must be specified before using it. For example, you use

int arr [n];

before reading the value of "n". Thus, the compiler does not know how many elements are present in the array "n", may be the value of garbage. How much memory it should allocate to the array.

therefore, you must read the value of "n" before using its array definition.

0
source

Source: https://habr.com/ru/post/1212382/


All Articles