Last active
January 18, 2023 04:43
-
-
Save hanwinbi/b9dbf422f43b8a2799c10480ab7d6e9e to your computer and use it in GitHub Desktop.
implementation of course.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "course.h" | |
#include <stdlib.h> | |
typedef struct course | |
{ | |
int refcount; | |
uint16_t code; | |
enum subject sub; | |
}; | |
/** | |
* Create a new Course. | |
* | |
* Returns an object with a refcount of 1. | |
*/ | |
struct course *course_create(enum subject sub, uint16_t code) | |
{ | |
struct course *m_course = (struct course *)malloc(sizeof(struct course)); | |
m_course->refcount = 1; | |
m_course->code = code; | |
m_course->sub = sub; | |
return m_course; | |
} | |
/** Retrieve a course's subject. */ | |
enum subject course_subject(const struct course *m_course) | |
{ | |
return m_course->sub; | |
} | |
/** Retrieve a course's course code. */ | |
uint16_t course_code(const struct course *m_course) | |
{ | |
return m_course->code; | |
} | |
/** Increment a course's refcount. */ | |
void course_hold(struct course *m_course) | |
{ | |
m_course->refcount++; | |
} | |
/** Decrement a course's refcount (optionally freeing it). */ | |
void course_release(struct course *m_course) | |
{ | |
m_course->refcount--; | |
} | |
/** Retrieve the current reference count of a course. */ | |
int course_refcount(const struct course *m_course) | |
{ | |
return m_course->refcount; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file student.h | |
* @brief Header file for student types in ENGI 8894/9875 assignment 1 | |
*/ | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include "student.h" | |
#include "course.c" | |
#include <stdlib.h> | |
typedef struct student | |
{ | |
struct student_id sid; | |
bool grad_student; | |
int course_count; | |
struct course* course_taken[10]; | |
}; | |
/** | |
* Create a new student object. | |
* | |
* The caller is responsible for freeing the returned memory. | |
*/ | |
struct student *student_create(struct student_id sid, bool grad_student){ | |
struct student* m_stu = (struct student*)malloc(sizeof(struct student)); | |
m_stu->sid = sid; | |
m_stu->grad_student = grad_student; | |
m_stu -> course_count = 0; | |
return m_stu; | |
} | |
/** | |
* Release a student object. | |
*/ | |
void student_free(struct student *m_stu){ | |
free(m_stu); | |
} | |
/** | |
* Note that a student has taken a course. | |
* | |
* This student will now take a reference to (i.e., increment the refcount of) | |
* the course that is passed in. | |
*/ | |
void student_take(struct student *m_stu, struct course * m_course, uint8_t grade){ | |
m_course->grade = grade; | |
m_stu->course_taken[m_stu->course_count] = m_course; | |
course_hold(m_course); | |
} | |
/** | |
* Retrieve a student's mark in a course. | |
* | |
* This will retrieve the most recent grade that a student achieved in a | |
* particular course. | |
* | |
* @returns a grade, or -1 if the student has not taken the course | |
*/ | |
int student_grade(struct student *m_stu, struct course *m_course){ | |
for(int i = 0; i < m_stu->course_count; i++){ | |
if(m_stu->course_taken[i]->code == m_course->code){ | |
return m_stu->course_taken[i]->grade; | |
} | |
} | |
return -1; | |
} | |
/** | |
* Determine the average grade in the courses this student has passed. | |
* | |
* This will average the grades in courses that the student has passed, meaning | |
* that they scored at least 50 (if undergraduate) or 65 (if graduate). | |
* | |
* @returns the average, or 0 if no courses have been passed | |
*/ | |
double student_passed_average(const struct student *m_stu){ | |
int sum = 0; | |
int pass_grade = m_stu -> grad_student ? 65 : 50; | |
int pass_num = 0; | |
for(int i = 0; i < m_stu->course_count; i++){ | |
int grade = m_stu->course_taken[i]->grade; | |
if(grade >= pass_grade){ | |
sum += grade; | |
pass_num++; | |
} | |
} | |
double ave = sum * 1.0 / pass_num; | |
return ave; | |
} | |
/** | |
* Determine whether or not this student is promotable. | |
* | |
* (note: this is not how promotion really works... simplified for assignment) | |
* | |
* For graduate students, determine whether they have passed all of their | |
* courses apart from (up to) one. | |
* | |
* For undergraduate students, determine whether or not the cumulative average | |
* of all courses is at least 60%. | |
*/ | |
bool student_promotable(const struct student *m_stu){ | |
int pass_num = 0; | |
double sum = 0; | |
int pass_grade = m_stu -> grad_student ? 65 : 50; | |
for(int i = 0; i < m_stu->course_count; i++){ | |
int grade =m_stu->course_taken[i]->grade; | |
sum += grade; | |
if(grade >= pass_grade) pass_num++; | |
} | |
if(m_stu -> grad_student){ | |
int failed = m_stu -> course_count - pass_num; | |
return failed <= 1 ? true : false; | |
} | |
double cumulative_ave = sum * 1.0 / (m_stu->course_count*100); | |
return cumulative_ave > 0.6 ? true : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment