The problem
Job
King Arthur and his knights are having a New Years get together. Final yr Lancelot was jealous of Arthur, as a result of Arthur had a date and Lancelot didn’t, and so they began a duel.
To stop this from occurring once more, Arthur needs to be sure that there are at the least as many ladies as males at this yr’s get together. He gave you an inventory of integers of all of the get together goers.
Arthur wants you to return true if he wants to ask extra ladies or false if he’s all set.
Enter/Output
[input]integer arrayL($ain PHP)
An array (assured non-associative in PHP) representing the genders of the attendees, the place -1 represents ladies and 1 represents males.
2 <= L.size <= 50
[output]a boolean worthtrueif Arthur want to ask extra ladies,falsein any other case.
The answer in C
Possibility 1:
#embrace <stddef.h>
int invite_more_women(int *arr, size_t depend) {
int sum = 0;
for (int i = 0; i < depend; ++i) sum += arr[i];
return (sum>0)? 1 : 0;
}
Possibility 2:
#embrace <stddef.h>
int invite_more_women(int *arr, size_t depend) {
int steadiness = 0;
whereas (count--)
steadiness += *arr++;
return steadiness > 0;
}
Possibility 3:
#embrace <stdbool.h>
#embrace <stddef.h>
bool invite_more_women(const int attendee_genders[], size_t depend) {
ptrdiff_t sum = 0;
for (size_t i = 0; i < depend; i++)
sum += attendee_genders[i];
return sum > 0;
}
Check circumstances to validate our resolution
#embrace <stdbool.h>
#embrace <stddef.h>
#embrace <criterion/criterion.h>
extern void do_test (size_t depend, const int array[count], bool anticipated);
#outline ARR_LEN(array) (sizeof(array) / sizeof *(array))
#outline sample_test(array, anticipated) do_test(ARR_LEN(array), array, anticipated)
Check(tests_suite, sample_tests)
{
sample_test(((int[]){1, -1, 1}), true);
sample_test(((int[]){-1, -1, -1}), false);
sample_test(((int[]){1, -1}), false);
sample_test(((int[]){1, 1, 1}), true);
do_test(0, NULL, false);
}