Skip to content

Commit

Permalink
Update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf99 committed Jul 28, 2021
1 parent 135cc9a commit e744fdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
32 changes: 15 additions & 17 deletions exercises/practice/grade-school/.meta/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

static roster_t roster = { 0 };

static bool is_student_on_roster(char *name, roster_t roster)
{
bool is_on_roster = false;
for (size_t i = 0; i < roster.count; ++i) {
if (!strncmp(roster.students[i].name, name, MAX_NAME_LENGTH)) {
is_on_roster = true;
break;
}
}
return is_on_roster;
}

static int compare_student_names(const void *s1, const void *s2)
{
const student_t *student1 = (const student_t *)s1;
Expand All @@ -24,24 +36,13 @@ static int compare_student_grades_and_names(const void *s1, const void *s2)
return compare_student_names(student1, student2);
}

static bool are_students_in_multiple_grades(roster_t students)
{
for (size_t i = 0; i < students.count; ++i)
for (size_t j = 0; j < students.count; ++j)
if (i != j
&& !strncmp(students.students[i].name, students.students[j].name,
MAX_NAME_LENGTH)
&& students.students[i].grade != students.students[j].grade)
return true;

return false;
}

bool add_student(char *name, uint8_t grade)
{
bool added = false;

if (roster.count < MAX_STUDENTS && strlen(name) < MAX_NAME_LENGTH) {
if (is_student_on_roster(name, roster)) {
added = false;
} else if (roster.count < MAX_STUDENTS && strlen(name) < MAX_NAME_LENGTH) {
strcpy(roster.students[roster.count].name, name);
roster.students[roster.count].grade = grade;
++roster.count;
Expand All @@ -62,9 +63,6 @@ roster_t get_grade(uint8_t grade)
{
roster_t grade_roster = { 0 };

if (are_students_in_multiple_grades(roster))
return grade_roster;

for (size_t i = 0; i < roster.count && grade_roster.count < MAX_STUDENTS;
++i) {
if (roster.students[i].grade == grade) {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/grade-school/test_grade_school.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void populate_roster(roster_t * expected)
for (size_t i = 0; i < expected->count; ++i) {
char name_buffer[MAX_NAME_LENGTH];
strcpy(name_buffer, expected->students[i].name);
TEST_ASSERT_TRUE_MESSAGE(add_student(name_buffer, expected->students[i].grade), "Could not add student");
add_student(name_buffer, expected->students[i].grade);
}
}

Expand Down

0 comments on commit e744fdc

Please sign in to comment.