Skip to content

How to Categorize a New Member in C

The challenge

The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed.

To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.

Input

Input will consist of a list of pairs. Each pair contains information for a single potential member. Information consists of an integer for the person’s age and an integer for the person’s handicap.

Output

Output will consist of a list of string values (Open or Senior) stating whether the respective member is to be placed in the senior or open category.

Example

input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]] output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]
Code language: C++ (cpp)

The solution in C

Option 1:

#include <stddef.h> enum membership {OPEN = 1, SENIOR = 2}; void open_or_senior (size_t n, const int members[n][2], enum membership memberships[n]) { for (size_t i = ; i < n; i++) memberships[i] = (members[i][] >= 55 && members[i][1] > 7) ? SENIOR : OPEN; }
Code language: C++ (cpp)

Option 2:

#include <stddef.h> #include <stdio.h> enum membership {OPEN = 1, SENIOR = 2}; void open_or_senior (size_t n, const int members[n][2], enum membership memberships[n]) { for(size_t i = ; i < n; i++){ if (members[i][] >= 55 && members[i][1] > 7){ memberships[i] = SENIOR; } else { memberships[i] = OPEN; } } }
Code language: C++ (cpp)

Option 3:

#include <stddef.h> enum membership {OPEN = 1, SENIOR = 2}; void open_or_senior (size_t n, const int members[n][2], enum membership memberships[n]) { for(int i = ; i < n; i++){ if(members[i][] >= 55 && members[i][1] > 7) memberships[i] = SENIOR; else memberships[i] = OPEN; } }
Code language: C++ (cpp)

Test cases to validate our solution

#include <stddef.h> #include <stdbool.h> #include <criterion/criterion.h> enum membership {OPEN = 1, SENIOR = 2}; extern void get_membership_type (size_t n, const int members[n][2], enum membership memberships[n]); static void do_test (size_t n, const int members[n][2], const enum membership expected[n], enum membership user_array[n]); #define ARR_LEN(array) (sizeof(array) / sizeof(*(array))) #define sample_test(members, expected) do_test(ARR_LEN(members), members, expected, (enum membership[ARR_LEN(members)]){0}) Test(tests_suite, sample_tests) { sample_test(((const int[][2]){ {45, 12}, {55,21}, {19, -2}, {104, 20} }),((const enum membership[]) {OPEN, SENIOR, OPEN, SENIOR} )); sample_test(((const int[][2]){ {3, 12}, {55,1}, {91, -2}, {53, 23} }),((const enum membership[]) {OPEN, OPEN, OPEN, OPEN} )); sample_test(((const int[][2]){ {59, 12}, {55,-1}, {12, -2}, {12, 12} }),((const enum membership[]) {SENIOR, OPEN, OPEN, OPEN} )); } extern void open_or_senior (size_t n, const int members[n][2], enum membership memberships[n]); static void do_test (size_t n, const int members[n][2], const enum membership expected[n], enum membership user_array[n]) { open_or_senior(n, members, user_array); for (size_t i = ; i < n; i++) { if (expected[i] != user_array[i]) { cr_assert_fail("for age = %d and handicap = %d, expected %s", members[i][], members[i][1], expected[i] == SENIOR ? "SENIOR" : "OPEN" ); } } cr_assert(true); }
Code language: C++ (cpp)

See also  How to Find the Capitals in C
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x