Monday, March 6, 2023
HomeSoftware EngineeringEasy methods to Categorize a New Member in C

Easy methods to Categorize a New Member in C


The problem

The Western Suburbs Croquet Membership has two classes of membership, Senior and Open. They want your assist with an software kind that may inform potential members which class they are going to be positioned.

To be a senior, a member should be not less than 55 years previous and have a handicap better than 7. On this croquet membership, handicaps vary from -2 to +26; the higher the participant the decrease the handicap.

Enter

Enter will encompass a listing of pairs. Every pair incorporates info for a single potential member. Data consists of an integer for the particular person’s age and an integer for the particular person’s handicap.

Output

Output will encompass a listing of string values (Open or Senior) stating whether or not the respective member is to be positioned within the senior or open class.

Instance

enter =  [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]

The answer in C

Choice 1:

#embrace <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 = 0; i < n; i++)
    memberships[i] = (members[i][0] >= 55 && members[i][1] > 7) ? SENIOR : OPEN;
}

Choice 2:

#embrace <stddef.h>
#embrace <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 = 0; i < n; i++){
    if (members[i][0] >= 55 && members[i][1] > 7){
      memberships[i] = SENIOR;
    } else {
      memberships[i] = OPEN;
    }
  }
}

Choice 3:

#embrace <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 = 0; i < n; i++){
      if(members[i][0] >= 55 && members[i][1] > 7)
        memberships[i] = SENIOR;
      else
        memberships[i] = OPEN;
  }
}

Check instances to validate our resolution

#embrace <stddef.h>
#embrace <stdbool.h>
#embrace <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 anticipated[n], enum membership user_array[n]);

#outline ARR_LEN(array) (sizeof(array) / sizeof(*(array)))

#outline sample_test(members, anticipated) do_test(ARR_LEN(members), members, anticipated, (enum membership[ARR_LEN(members)]){0})

Check(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 anticipated[n], enum membership user_array[n])
{
	open_or_senior(n, members, user_array);

	for (size_t i = 0; i < n; i++) {
		if (anticipated[i] != user_array[i]) {
			cr_assert_fail("for age = %d and handicap = %d, anticipated %s",
				members[i][0], members[i][1], anticipated[i] == SENIOR ? "SENIOR" : "OPEN"
			);
		}
	}
	cr_assert(true);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments