Algorithm 19 - Beginner - Lost Without a Map

Beast from the east·2021년 10월 6일
0

Algorithm

목록 보기
19/27

Q.

Description:
Given an array of integers, return a new array with each value doubled.

For example:

[1, 2, 3] --> [2, 4, 6]

A)

#include <stddef.h>

// return a *new, dynamically allocated* array with each element doubled.
int *maps(const int *arr, size_t size)
{
  int *tmp = (int*)malloc(sizeof(int) * size);
  if (!tmp)
    return NULL;
  for (size_t i = 0; i < size; i++)
    tmp[i] = arr[i] * 2;
  return tmp;
}

another solution
int *maps(const int *arr, size_t size) {
  int* a = (int*)malloc(size * sizeof(int));
  for (int i = 0; i < size; a[i++] = arr[i] * 2);
  return a;
} -> 이런것도 되넹
profile
Hello, Rabbit

0개의 댓글