CSCE 1040 Homework 2 For this assignment we are going to build a simple Grade Bo
ID: 3757439 • Letter: C
Question
CSCE 1040 Homework 2 For this assignment we are going to build a simple Grade Book using classes For the purposes of this grade book you should first provide a menu with the following options: 1. Add a new course 2. Add a new student 3. Add a student to a course (add enrollment) 4. Add grades for a student in a course 5. Print a list of all grades for a student in a course 6. Print a list of all students in a course 7. Compute the average for a student in a course 8. Print a list of all courses 9. Print a list of all students 10. Compute the average for a course 11. Store Grade book (to a disk file) 12. Load Grade book (from a disk file) Each of these menu items should correspond to a function you will write. For this program you will creat a class to represent each of the basic entities, as we as a class to represent each of the collections. For the collections you should use a dynamically-sized arrays of objects.. Student should have an ID, a name, and a classification and any other fields you need Course should have and ID, a name, a location and a meeting time and any other fields you feel are necessary. Enrolment should have a ID, a student ID, a course ID and a list of up to 10 grades. an average grade and a letter grade based on a standard 90/80/70 grading rubric with rounding Grades will be whole numbers only (no fractional part). Averages will be saved and displayed as floating point values to the hundredths position.Explanation / Answer
//main.c
#include "func_header.h"
int main(){
int option = 0, i = 0, j = 0, k = 0;
char choice[30];
initial_print();
printf(" Welcome to the Grade Book! ");
courses *cours;
students *studs;
studs = (students *) malloc(sizeof(students) * CHUNKSIZE);
cours = (courses *) malloc(sizeof(courses) * CHUNKSIZE);
studs->student_count = 0;
cours->course_count = 0;
studs->student_max = cours->course_max = 0;
do{
option = menu();
switch(option){
case 1: new_course(cours);
break;
case 2: new_student(studs);
break;
case 3: add_course_student(cours, studs);
break;
case 4: add_course_grades(cours, studs);
break;
case 5: print_student_grades(cours, studs);
break;
case 6: print_course_students(cours, studs);
break;
case 7: student_average(cours, studs);
break;
case 8: list_courses(cours);
break;
case 9: list_students(studs);
break;
case 10: course_average(cours);
break;
case 11: store_grade_book(cours, studs);
break;
case 12: load_grade_book(cours, studs);
break;
}
printf(" Do you wish to continue (y/n):");
scanf("%s", choice);
if(choice[0] == 110 || choice[0] == 78)
option = 0;
}while(option != 0);
printf(" Thank you for using the Grade Book program. ");
return 0;
}
//func_header.h
#include "header.h"
// Func prototypes
void initial_print(void);
int menu(void);
void new_course(courses * c);
void new_student(students * s);
void add_course_student(courses * c, students * s);
void add_course_grades(courses * c, students * s);
void print_student_grades(courses * c, students * s);
void print_course_students(courses * c, students * s);
void student_average(courses * c, students * s);
void list_courses(courses * c);
void list_students(students * s);
void course_average(courses * c);
void store_grade_book(courses * c, students * s);
void load_grade_book(courses * c, students * s);
int student_match(students * s);
int course_match(courses * c);
//funcs.c
#include "func_header.h"
// Functions
void initial_print(void){
printf(" %s %s %s %s %s %s %s ",
"Department of Computer Science and Engineering" );
return;
}
int menu(void){
int option = 0;
printf(" %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
"Menu Options",
"1. Add a new course",
"2. Add a new student",
"3. Add a student to a course",
"4. Add grades for a student in a course",
"5. Print a list of all grades for a student in a course",
"6. Print a list of all students in a course",
"7. Compute the average for a student in a course",
"8. Print a list of all courses",
"9. Print a list of all students",
"10. Compute the average for a course",
"11. Store Grade Book (to a disk file)",
"12. Load Grade Book (from a disk file)",
"Please use a number 1-12:");
scanf("%d",&option);
getchar();
if (option < 1 || option > 12){
printf("Please use a number 1-12! Try again.");
menu();
}
return option;
}
void new_course(courses * c){
int i, course_id;
char option = 'n';
if (c->course_count == c->course_max)
{
course *temp;
temp = (course *) realloc(c->course_list, sizeof(course) *(c->course_max + CHUNKSIZE));
c->course_max += CHUNKSIZE;
c->course_list = temp;
grade *grade_temp;
grade_temp = (grade *) malloc(sizeof(grade) * CHUNKSIZE);
c->course_list[c->course_count].grades_max += CHUNKSIZE;
c->course_list[c->course_count].grades = grade_temp;
c->course_list[c->course_count].grades_count = 0;
c->course_list[c->course_count].grades[0].grades_count = c->course_list[c->course_count].grades[0].grades_count = c->course_list[c->course_count].grades[0].student_id = 0;
}
printf(" What is the course id number:");
scanf("%d%*c", &(c->course_list[c->course_count].course_id));
printf(" What is the course name:");
scanf("%s", c->course_list[c->course_count].course_name);
printf(" %s %d %s ", c->course_list[c->course_count].course_name, c->course_list[c->course_count].course_id, "added.");
c->course_count += 1;
return;
}
void new_student(students * s){
int i, j, id_number;
if (s->student_count == s->student_max)
{
student *temp;
temp = (student *) realloc(s->student_list, sizeof(student) *(s->student_max + CHUNKSIZE));
s->student_max += CHUNKSIZE;
s->student_list = temp;
}
printf(" What is the student's id number:");
scanf("%d", &(s->student_list[s->student_count].student_id));
for(i = 0; i < s->student_count; i++){
if(s->student_list[i].student_id == s->student_list[s->student_count].student_id){
printf(" The student id already exists! Try again.");
s->student_max -= CHUNKSIZE;
return;
}
}
printf(" What is the first name of the student:");
scanf("%s", s->student_list[s->student_count].first_name);
printf(" What is the last name of the student:");
scanf("%s", s->student_list[s->student_count].last_name);
printf(" %d %s %s %s ", s->student_list[s->student_count].student_id, s->student_list[s->student_count].first_name, s->student_list[s->student_count].last_name, "added.");
s->student_count += 1;
return;
}
void add_course_student(courses * c, students * s){
int i = 0, j = 0, k = 0, course_id, student_id;
i = course_match(c);
if(i == -1)
return;
j = student_match(s);
if(j == -1)
return;
for(k = 0; k <= c->course_list[i].grades_count; k++){
if(c->course_list[i].grades[k].student_id == s->student_list[j].student_id){
printf(" %s %s is already in the class.", s->student_list[j].first_name, s->student_list[j].last_name);
return;
}
if(c->course_list[i].grades[k].student_id == 0){
c->course_list[i].grades[k].student_id = s->student_list[j].student_id;
printf(" %s %s added to %s.", s->student_list[j].first_name, s->student_list[j].last_name, c->course_list[i].course_name);
if (c->course_list[i].grades_count == c->course_list[i].grades_max)
{
grade *grade_temp;
grade_temp = (grade *) realloc(c->course_list[i].grades, sizeof(int) *(c->course_list[i].grades_max + CHUNKSIZE));
c->course_list[i].grades_max += CHUNKSIZE;
c->course_list[i].grades = grade_temp;
c->course_list[i].grades[c->course_list[i].grades_count + 1].grades_count = c->course_list[i].grades[c->course_list[i].grades_count + 1].grades_max = c->course_list[i].grades[c->course_list[i].grades_count + 1].student_id = 0;
}
if (c->course_list[i].grades[k].grades_count == c->course_list[i].grades[k].grades_max)
{
int *temp;
temp = (int *) malloc(sizeof(int) * CHUNKSIZE);
c->course_list[i].grades[k].grades_max += CHUNKSIZE;
c->course_list[i].grades[k].grades = temp;
}
break;
}
if(k == 20){
printf(" The class is full. Try another class.");
return;
}
}
c->course_list[i].grades_count += 1;
return;
}
void add_course_grades(courses * c, students * s){
int i, j, k, l, f = 1;
char choice = 'y';
i = course_match(c);
if(i == -1)
return;
j = student_match(s);
if(j == -1)
return;
for(k=0; k<c->course_list[i].grades_count; k++)
if(c->course_list[i].grades[k].student_id == s->student_list[j].student_id && c->course_list[i].grades[k].student_id != 0){
while(choice == 'y'){
if (c->course_list[i].grades[k].grades_count == c->course_list[i].grades[k].grades_max)
{
int *temp;
temp = (int *) realloc(c->course_list[i].grades[k].grades, sizeof(int) *(c->course_list[i].grades[k].grades_max + CHUNKSIZE));
c->course_list[i].grades[k].grades_max += CHUNKSIZE;
c->course_list[i].grades[k].grades = temp;
}
printf(" Grade %d:",f);
scanf("%d", &c->course_list[i].grades[k].grades[l]);
f++;
getchar();
printf(" Any more grades (y/n):");
choice = getchar();
c->course_list[i].grades[k].grades_count++;
}
}
return;
}
void print_student_grades(courses * c, students * s){
int i, j, k, l, f = 1;
i = course_match(c);
if(i == -1)
return;
j = student_match(s);
if(j == -1)
return;
for(k=0; k<c->course_list[i].grades_count; k++){
if(c->course_list[i].grades[k].student_id == s->student_list[j].student_id && c->course_list[i].grades[k].student_id != 0){
printf(" %s %s Grades ", s->student_list[j].first_name, s->student_list[j].last_name);
for(l=0; l<c->course_list[i].grades[k].grades_count; l++){
printf(" Grade %3d: %5d", f, c->course_list[i].grades[k].grades[l]);
f++;
}
}
if(k==19 && c->course_list[i].grades[k].student_id != (0 && s->student_list[j].student_id))
printf(" %s %s is not in %s! Try again. ", s->student_list[j].first_name, s->student_list[j].last_name, c->course_list[i].course_name);
}
return;
}
void print_course_students(courses * c, students * s){
int i, j, k, course_id, student_id;
i = course_match(c);
if(i == -1)
return;
printf(" %s %d ", c->course_list[i].course_name, c->course_list[i].course_id);
printf(" %15s%15s%15s ", "ID Number", "First Name", "Last Name");
for(j=0; j<100; j++)
for(k=0; k<20; k++)
if(c->course_list[i].grades[k].student_id == s->student_list[j].student_id && c->course_list[i].grades[k].student_id != 0)
printf(" %15d%15s%15s ", s->student_list[j].student_id, s->student_list[j].first_name, s->student_list[j].last_name);
return;
}
void student_average(courses * c, students * s){
int i, j, k, l, total = 0, average;
i = course_match(c);
if(i == -1)
return;
j = student_match(s);
if(j == -1)
return;
for(k=0; k<c->course_list[i].grades_count; k++){
if(c->course_list[i].grades[k].student_id == s->student_list[j].student_id && c->course_list[i].grades[k].student_id != 0){
for(l=0; l<c->course_list[i].grades[k].grades_count; l++){
total += c->course_list[i].grades[k].grades[l];
}
break;
}
if(k==(c->course_list[i].grades_count - 1) && c->course_list[i].grades[k].student_id != s->student_list[j].student_id)
printf(" %s %s is not in %s! Try again. ", s->student_list[j].first_name, s->student_list[j].last_name, c->course_list[i].course_name);
}
average =(int)total / (int)l;
printf(" %s %s Average: %d", s->student_list[j].first_name, s->student_list[j].last_name, average);
return;
}
void list_courses(courses * c){
int i = 0;
printf(" %15s%10s","Course Name", "Course ID");
for(i=0;i<c->course_count;i++){
printf(" %15s%10d", c->course_list[i].course_name, c->course_list[i].course_id);
}
printf(" ");
return;
}
void list_students(students * s){
int i = 0;
printf(" %s %15s%15s%15s", "Student List", "ID Number", "First Name", "Last Name");
for(i=0;i<s->student_count;i++){
printf(" %15d%15s%15s", s->student_list[i].student_id, s->student_list[i].first_name, s->student_list[i].last_name);
}
printf(" ");
return;
}
void course_average(courses * c){
int i, j, k, grades_count = 0, total = 0, average;
i = course_match(c);
if(i == -1)
return;
for(j=0; j<c->course_list[i].grades_count; j++){
for(k=0; k<c->course_list[i].grades[j].grades_count; k++){
total += c->course_list[i].grades[j].grades[k];
grades_count++;
}
}
average = (int)total / (int)grades_count;
printf(" Course Average: %d", average);
return;
}
void store_grade_book(courses * c, students * s){
return;
}
void load_grade_book(courses * c, students * s){
char input[30];
printf(" Grade Book %s Loaded! ", input);
return;
}
int student_match(students * s){
int i, student_id;
printf(" What is the student id:");
scanf("%d", &student_id);
for(i = 0; i < s->student_count; i++){
if(s->student_list[i].student_id == student_id){
printf(" Student Found! ");
return i;
}
if(i == (s->student_count -1) && s->student_list[i].student_id != student_id){
printf(" The student id does not exist. Try again! ");
return -1;
}
}
return 0;
}
int course_match(courses * c){
int i, course_id;
printf(" What is the course id:");
scanf("%d", &course_id);
for(i = 0; i < c->course_count; i++){
if(c->course_list[i].course_id == course_id){
printf(" Course Found! ");
return i;
}
if(i == (c->course_count -1) && c->course_list[i].course_id != course_id){
printf(" The course id does not exist. Try again! ");
return -1;
}
}
}
//header.h
#include <stdio.h>
#include <stdlib.h>
#define CHUNKSIZE 2
typedef struct
{
int student_id;
int grades_count;
int grades_max;
int *grades;
} grade;
typedef struct
{
int student_id;
char first_name[30];
char last_name[30];
} student;
typedef struct
{
int course_id;
char course_name[30];
int grades_count;
int grades_max;
grade *grades;
} course;
typedef struct
{
int student_count;
int student_max;
student *student_list;
} students;
typedef struct
{
int course_count;
int course_max;
course *course_list;
} courses;