Skip to content

Instantly share code, notes, and snippets.

@CallumHoward
Created January 2, 2018 07:27
Show Gist options
  • Save CallumHoward/907ba42dcc1589df909e2dfdf2f65e22 to your computer and use it in GitHub Desktop.
Save CallumHoward/907ba42dcc1589df909e2dfdf2f65e22 to your computer and use it in GitHub Desktop.
// allEven.c
// Callum Howard, 2017
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// function prototypes
bool allEven(int numbers[], int left, int right);
bool isEven(int input);
void testAllEven(void);
int main() {
testAllEven();
printf("All tests passed!\n");
return EXIT_SUCCESS;
}
// checks if all elements in the numbers are even
bool allEven(int numbers[], int left, int right) {
// Step 01: Base case(s)
if (left > right) { return false; }
if (left == right) { return isEven(numbers[left]); }
// Step 02: Reduction step
int mid = (left + right) / 2;
// Step 03: Recursive call(s)
bool firstHalfAllEven = allEven(numbers, left, mid);
if (!firstHalfAllEven) { return false; }
bool secondHalfAllEven = allEven(numbers, mid + 1, right);
if (!secondHalfAllEven) { return false; }
return true;
}
bool isEven(int input) { return input % 2 == 0; }
void testAllEven() {
int numElements = 0;
int testArray = {};
assert(allEven(testArray, 0, numElements - 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment