Created
April 19, 2020 10:06
-
-
Save craftybones/fe5503fbb7501e15587408a3bc044773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include "filter.h" | |
IntArray *create_int_array_from(int *values, int length) | |
{ | |
IntArray *array = (IntArray *)malloc(sizeof(IntArray)); | |
array->values = (int *)malloc(sizeof(int) * length); | |
array->length = length; | |
for (size_t i = 0; i < array->length; i++) | |
{ | |
array->values[i] = values[i]; | |
} | |
return array; | |
} | |
void display_int_array(IntArray array) | |
{ | |
for (size_t i = 0; i < array.length; i++) | |
{ | |
printf("%d ", array.values[i]); | |
} | |
printf("\n"); | |
} | |
IntArray *filter_even(int *numbers, int length) | |
{ | |
int temp_evens[length]; | |
int number_of_evens = 0; | |
for (size_t i = 0; i < length; i++) | |
{ | |
if (numbers[i] % 2 == 0) | |
{ | |
temp_evens[number_of_evens] = numbers[i]; | |
number_of_evens++; | |
} | |
} | |
return create_int_array_from(temp_evens, number_of_evens); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __FILTER_H_ | |
#define __FILTER_H_ | |
typedef struct | |
{ | |
int *values; | |
int length; | |
} IntArray; | |
IntArray *create_int_array_from(int *values, int length); | |
void display_int_array(IntArray array); | |
IntArray *filter_even(int *numbers, int length); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "filter.h" | |
int main(void) | |
{ | |
int a[5] = {1, 2, 3, 4, 5}; | |
IntArray *evens = filter_even(a, 5); | |
display_int_array(*evens); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment