Skip to content
Snippets Groups Projects
Commit ddd6e0f2 authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

add Vector map

parent c40cb171
No related branches found
No related tags found
No related merge requests found
...@@ -30,5 +30,6 @@ void vector_insert(vector vec, type element, int index); ...@@ -30,5 +30,6 @@ void vector_insert(vector vec, type element, int index);
void vector_empty(vector vec); void vector_empty(vector vec);
void vector_free(vector vec); void vector_free(vector vec);
void vector_print(vector vec, void (*print)(type)); void vector_print(vector vec, void (*print)(type));
void vector_map(vector vec, type (*f)(type));
#endif #endif
\ No newline at end of file
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "../header/vectors.h" #include "../header/vectors.h"
void print_type(type val); void print_type(type val);
int times_two(type a);
int main() int main()
{ {
...@@ -57,8 +58,12 @@ int main() ...@@ -57,8 +58,12 @@ int main()
vector_push(test, i); vector_push(test, i);
} }
vector_print(test, &print_type); vector_print(test, &print_type);
printf("\n");
//Test Vector map
vector_map(test, &times_two);
vector_print(test, &print_type);
//Test Vector Free //Test Vector Free
vector_free(test); vector_free(test);
...@@ -68,4 +73,9 @@ int main() ...@@ -68,4 +73,9 @@ int main()
void print_type(type val) void print_type(type val)
{ {
printf("%d \n", val); printf("%d \n", val);
}
int times_two(type a)
{
return 2 * a;
} }
\ No newline at end of file
...@@ -135,4 +135,12 @@ void vector_print(vector vec, void (*print)(type)) ...@@ -135,4 +135,12 @@ void vector_print(vector vec, void (*print)(type))
{ {
print(vec->content[i]); print(vec->content[i]);
} }
}
void vector_map(vector vec, type (*f)(type))
{
for (int i = 0; i < vec->length; i++)
{
vec->content[i] = f(vec->content[i]);
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment