Instructions Define a struct Course that has a course number and a number of cre
ID: 3760935 • Letter: I
Question
Instructions Define a struct Course that has a course number and a number of credits (integers) Define a struct Schedule that can contain a array of courses, and the number of courses (Since we do not know how many courses ahead of time then the array needs to be dynamic i.e use malloc) Write a function createSchedule that takes the number of courses as a parameter and allocates space for the courses. Write a function destroySchedule to free the memory you used and set its fields to indicate the schedule is empty (You will need to · Write a function printSchedule to print out the schedule information Write a function to compute the total number of credits on the schedule and return it n main: ask the user how many courses they're taking create a schedule fill in the schedule by asking the user for the course number and number of credits of each course print the schedule print the number of credits in the schedule destroy the schedule ·Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
typedef struct Course
{
int courseNumber;
int credits;
}Course;
typedef struct Schedule
{
Course *array;
int numberOfCourses;
}Schedule;
Course *createSchedule(int numOfCourses)
{
Course *a;
a = (Course *)malloc(sizeof(Course) * numOfCourses);
return a;
}
void destroySchedule(Schedule *s)
{
free(s->array);
s->array = NULL;
s->numberOfCourses = 0;
}
void printSchedule(Schedule s)
{
int i;
printf("Schedule(");
for(i = 0; i < s.numberOfCourses; i++)
printf(" Course( courseNumber: %i nCredits: %i )", ((s.array)+i)->courseNumber, ((s.array)+i)->credits);
printf(" ) ");
}
int totalNumberOfCredits(Schedule s)
{
int count = 0;
for(int i = 0; i < s.numberOfCourses; i++)
count += ((s.array)+i)->credits;
return count;
}
int main()
{
Schedule s;
int num, cnum, cc, i;
printf("How many courses are you taking: ");
scanf("%d", &num);
s.array = createSchedule(num);
s.numberOfCourses = num;
printf("Scheduled Instantiated... ");
for(i = 0; i < num; i++)
{
printf("Enter the course number and number of credits: ");
scanf("%i%i", &cnum, &cc);
((s.array)+i)->courseNumber = cnum;
((s.array)+i)->credits = cc;
}
printSchedule(s);
printf("Total credits: %i ", totalNumberOfCredits(s));
destroySchedule(&s);
}