Back to problems
COMP1521
Pointers & Heap: Double an Array
Pointers & Heap: Double an Array
A pointer exercise that needs manual memory management.
Task
- Read an integer
n, thennintegers. malloca heap array ofnints and store them.- In
double_all(int *arr, int n), use a pointer to double every element. - Print the doubled values in order, space-separated, newline at end.
freethe 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 withc-dcc: forgettingfree, 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