-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
82 lines (71 loc) · 1.38 KB
/
map.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
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
int square(int a)
{
return a*a;
}
int triplex(int a)
{
return a*a*a;
}
int myMap (int array[], int (*f)(int)) // Accepts any array and any function.
{
for (int i = 0; i < 5; i++)
{
int ans = f(array[i]);
printf("%d\n",ans);
}
printf("###############################\n");
}
int isEven (int n)
{
if (n%2 == 0)
{
return 1;
}
else return 0;
}
void arrAdd(int arr[], int numero_avaliado, int index)
{
arr[index] = numero_avaliado;
}
int myFilter(int array[], int (*f)(int), int EV[], int OD[])
{
int even = 0, odd = 0;
for (int i = 0; i < 5; i++)
{
if (f(array[i]) == 1)
{
arrAdd(EV,array[i],even);
even++;
}
else
{
arrAdd(OD,array[i],odd);
odd++;
}
}
printf("PARES: ");
for (int i = 0; i < even; i++)
{
printf("%d ", EV[i]);
}
printf("\nIMPARES: ");
for (int i = 0; i < odd; i++)
{
printf("%d ",OD[i]);
}
printf("\n");
}
int main()
{
int array[]= {1,2,3,4,5};
myMap(array, square); //Squares everything and prints it.
myMap(array, triplex); //Elevates to the third and prints it.
int EV[5];
int ODD[5];
myFilter(array,isEven,EV,ODD);
return 0;
}