OlveraeUNSW
Back to problems
COMP1521

Pointers & Heap: Double an Array

Pointers & Heap: Double an Array

A pointer exercise that needs manual memory management.

Task

  1. Read an integer n, then n integers.
  2. malloc a heap array of n ints and store them.
  3. In double_all(int *arr, int n), use a pointer to double every element.
  4. Print the doubled values in order, space-separated, newline at end.
  5. free the heap memory.

Input: first line n; then n integers.
Output: the n doubled integers, space-separated.

Example: input 3 / 1 2 3 → output 2 4 6

Compiled with c-dcc: forgetting free, going out of bounds, or using an unallocated pointer is caught at runtime.

Samples

stdin
3
1 2 3
expected
2 4 6
stdin
1
10
expected
20
c-dcc
14