Find the lonely integer in an array

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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; } 

回答1:

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! 


回答2:

Range of n is given in the question is 1 <= N < 100 which is small and a variable length array 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 array size

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


回答3:

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

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

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 size of array at compile time, you can define a macro

#define DATA_SIZE 9 

or set the macro while compiling the code

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


回答4:

Value of 'n' has to be defined before it has used.like you are using

int arr[n];

before reading the value of 'n'. So compiler will not know, how many number of elements are present in the array 'n' can be a garbage value.how much amount of memory it should allocate to an array.

hence you gotta read the value of 'n' before using it an array definition.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!