-
Notifications
You must be signed in to change notification settings - Fork 0
/
28.c
74 lines (65 loc) · 1.69 KB
/
28.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
int multiply(int x){
return x*x;
}
/*
* Apply the function to each element in the array. Make sure the types line up. Requires an int array and int function.
*/
void forLoop(int A[], int n, int (*func)())
{
for(int i = 0; i < n; i++)
A[i] = func(i);
}
/*
* It requires as a parameter a function that takes an int and returns an int.
*/
/*void forLoop2(int A[], int n, int (*func)(int x))
{
for(int i = 0; i < n; i++)
A[i] = func(x);
}*/
void forLoop3(int A[], int n, int (*func)(), int param)
{
for(int i = 0; i < n; i++)
A[i] = func(param);
}
void forLoop5(void *datastructure, int (*func)(const int *), const int *x)
{
while(*datastructure->next != NULL)
*datastructure = func(x);
*datastructure
}
/*void forLoop4(int A[], int n, int (*func)(), ...)
{
va_list args;
va_start(args, A[], n, func);
for(int i = 0; i < n; i++)
va_end(args);
//
}*/
/*
* Apply the function to each element in the array. Make sure the types line up.
*/
void GenericForLoop(void A[], int n, void (*func)())
{
for(int i = 0; i < n; i++)
A[i] = func(i);
}
void GenericDatastructureForLoop(void *A, int n, void (*func)())
{
for(int i = 0; i < n; i++)
A[i] = func(i);
}
int main()
{
int size = 10;
int array[size];
forLoop(array, size, multiply);
// forLoop2(array, size, multiply(size));
forLoop3(array, size, multiply, 3);
GenericForLoop((int)array, size, multiply);
GenericDatastructureForLoop(array, size, multiply); // yes function should work on arrays
// create datastructure
// GenericDatastructureForLoop(&datastructure, size, multiply);
return 0;
}