HackerRank/Data Structures
[HackerRank] Arrays : 2D Array - DS
ruming
2021. 4. 10. 19:03
배열을 입력받아 순서가 반대로 된 배열을 출력하는 문제이다.
// Complete the reverseArray function below.
// Please store the size of the integer array to be returned in result_count pointer. For example,
// int a[3] = {1, 2, 3};
//
// *result_count = 3;
//
// return a;
//
int* reverseArray(int a_count, int* a, int* result_count) {
int *b;
b = (int*)malloc(sizeof(int)*a_count);
*result_count = a_count;
for(int i=0; i<a_count; i++){
b[a_count-i-1] = a[i];
}
return b;
}
배열 하나를 a_count 크기만큼 동적으로 할당받고 a배열에 있는 원소들을 b에 거꾸로 저장했다. 그리고 b를 반환해주었다.