DZ8_2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
int g(int i) {
return i*3;
}
int* rec(int* arr, int n, int (*f)(int), int* newArr) {
if (n==0) { return newArr; }
newArr[n-1] = (*f)(arr[n-1]);
return rec(arr, n-1, f, newArr);
}
int* map(int* arr, int n, int (*f)(int)) {
return rec(arr, n, f, (int*)malloc(n*sizeof(int)));
}
int main() {
int arr[12] = { 1000, 3, 5, 2000, 5, 6, 113, 6, 3, 111, 10, 400 };
int (*p)(int);
p = g;
int* newArr = map(arr, 12, p);
for(int i=0; i<12; i++)
printf("arr[%i] = %d\n", i, newArr[i]);
getch();
}