Sunday, March 5, 2023
HomeSoftware EngineeringInvite Extra Girls in C

Invite Extra Girls in C

The problem


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 desires to ensure that there are not less than as many ladies as males at this yr’s get together. He gave you a listing of integers of all of the get together goers.

Arthur wants you to return true if he wants to ask extra girls or false if he’s all set.


  • [input] integer array L ($a in PHP)

An array (assured non-associative in PHP) representing the genders of the attendees, the place -1 represents girls and 1 represents males.

2 <= L.size <= 50

  • [output] a boolean worthtrue if Arthur want to ask extra girls, false in any other case.

The answer in C

Possibility 1:

#embody <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:

#embody <stddef.h>

int invite_more_women(int *arr, size_t depend) {
    int steadiness = 0;
    whereas (count--)
        steadiness += *arr++;
    return steadiness > 0;

Possibility 3:

#embody <stdbool.h>
#embody <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 answer

#embody <stdbool.h>
#embody <stddef.h>
#embody <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);


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments