Write a C program to generate a course plan for a student which will enable him/
ID: 3875783 • Letter: W
Question
Write a C program to generate a course plan for a student which will enable him/her to graduate and satisfy all requirements. A course plan is a list of courses which the student must take each quarter each year. In order to determine a course plan, your program will need 3 items of information which will be provided in three different files. The course plan will be printed by your program.
Information Files
The following three files contain information which is needed by your program to create a course plan.
1. Offering Information: Each course is offered once a year. The offering file describes the quarter in which each course is offered. Each line of the file contains two items separated by spaces. The first item on each line is the name of the class (which contains no spaces) and the second item on the line is the number of the quarter in which the course is offered. Assume that Fall is quarter number 1, Winter is quarter number 2, and Spring is quarter number 3. There are no summer courses. The following is an example of the contents of an offering file which describes that ICS 31, 32, and 33 are offered in Fall, Winter, and Spring, respectively.
ICS31 1
ICS32 2
ICS33 3
2. Prerequisite Information: The prerequisite file describes the prerequisites of each course. Each line of the file describes the prerequisites of a single course. Each item on the line is the name of a course. All courses on the line, other than the first course, is a prerequisite for the first course on the line. The following is an example of a prerequisite file which describes that ICS 31 is a prerequisite of ICS 32, and that ICS 32 and Math 3A are prerequisites of ICS 33.
ICS32 ICS31
ICS33 ICS32 MATH3A
A course may have no prerequisites, in which case it will not be the first item on any line in the prerequisite file.
3. Requirement Information: The requirement file contains the list of courses that the student must take to graduate (not including their prerequisites). The requirement file contains a single line and each item on the line is the name of a course which the student must take in order to graduate. The following is an example of a requirement file which describes that CS 151, CS 161, and CS 171 must be taken to graduate.
CS151 CS161 CS171
User Interface
Your program will be invoked from the command line and it will accept three arguments on the command line, the names of the offering file, the prerequisite file, and the requirement file, in that order. The names of the three files are arbitrary, so your program should not assume any particular file names.
Your program must create a course plan which describes the courses which a student should take each quarter in order to complete all of his/her graduation requirements. The course plan should be printed to the screen. Each line should list all of the courses which need to be taken in a single quarter. The first two items printed on each line should be the year number and the quarter number, respectively. The remaining items on a line should be the names of the courses which the student should take in that quarter. The quarters of the course plan should be printed in order based on the year number and the quarter number. The following is an example of how a course plan should be printed on the screen which describes a plan to take ICS 31 and Math 3A in the Fall of the first year, ICS 32 in the Winter of the first year, and ICS 33 in the Fall of the second year.
1 1 ICS31 MATH3A
1 2 ICS32
2 1 ICS33
Additional Notes
• The first year is year 1 and there is no limit on the total number of years that a student might require to graduate.
• There can be quarters when no class is scheduled in the course plan.
• Course names must contain no spaces.
• Each line in an information file must end with a ‘ ’ character, except the last line which ends with an EOF character.
Explanation / Answer
Answer: See the code below:
--------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 1000
#define MAX_COURSES 10
typedef struct {
char course_name[5];
int quarter;
char prerequisites[MAX_COURSES][10];
int num_prerequisites;
} Course;
int main(int argc, char *argv[]) {
//check if right number of command-line arguments passed or not
if (argc < 4) {
fprintf(stderr, "Wrong number of arguments!!! Exiting... ");
return EXIT_FAILURE;
}
//name of course plan files
char* offering_filename = argv[1];
char* prerequisite_filename = argv[2];
char* requirement_filename = argv[3];
//open files
FILE* offering_file = fopen(offering_filename, "r");
FILE* prerequisite_file = fopen(prerequisite_filename, "r");
FILE* requirement_file = fopen(requirement_filename, "r");
//check if files opened properly
if (!offering_file) {
fprintf(stderr, "Error opening/reading offering file!!! Exiting... ");
return EXIT_FAILURE;
}
if (!prerequisite_file) {
fprintf(stderr,
"Error opening/reading prerequisite file!!! Exiting... ");
return EXIT_FAILURE;
}
if (!requirement_file) {
fprintf(stderr,
"Error opening/reading requirement file!!! Exiting... ");
return EXIT_FAILURE;
}
//read offering file
char line[MAX_CHAR];
int num_courses = 0; //number of courses offered
//array to hold course info
Course* courses = (Course*) malloc(MAX_COURSES * sizeof(Course));
//populate courses
while (fgets(line, sizeof(line), offering_file)) {
char name[5];
int quar = 0;
sscanf(line, "%s %d ", name, &quar);
//printf("name:%s,quar:%d ",name,quar);
strcpy(courses[num_courses].course_name, name);
courses[num_courses].quarter = quar;
courses[num_courses].num_prerequisites = 0;
num_courses++;
}
int i, j, k;
//read prerequisite file
int num_lines = 0;
while (fgets(line, sizeof(line), prerequisite_file)) {
//strcpy(prerequisites_data[num_lines],line);
char *token;
token = strtok(line, " ");
//search for course matching with token
if (token != NULL) {
for (j = 0; j < num_courses; j++) {
if (strcmp(token, courses[j].course_name) == 0) {
break;
}
}
}
k = 0;
//populate prerequisites
while ((token = strtok(NULL, " ")) != NULL) {
//printf("token:%s ", token);
strcpy(courses[j].prerequisites[k], token);
//printf("j:%d,k:%d,added pre:%s ",j,k,courses[j].prerequisites[k]);
k++;
}
courses[j].num_prerequisites = k;
num_lines++;
}
//print course offerings and their prerequisites
printf("Course offerings: ");
printf("------------------ ");
for (i = 0; i < num_courses; i++) {
printf("Course:%s ", courses[i].course_name);
printf("Quarter:%d ", courses[i].quarter);
printf("Prerequisites:%d ", courses[i].num_prerequisites);
for (j = 0; j < courses[i].num_prerequisites; j++) {
printf("%s ", courses[i].prerequisites[j]);
}
printf("------------------ ");
}
//read requirements file
char requirements[MAX_COURSES][10];
fgets(line, sizeof(line), requirement_file);
char *token = strtok(line, " ");
i = 0;
while (token != NULL) {
strcpy(requirements[i], token);
token = strtok(NULL, " ");
i++;
}
int num_requiremenrs = i;
//printing course plan
printf("Course plan: ");
int num_years = 1;
for (i = 0; i < num_requiremenrs; i++) {
//search for required course in course offerings
for (j = 0; j < num_courses; j++) {
if (strcmp(requirements[i], courses[j].course_name) == 0) {
printf("%s ", requirements[i]);
break;
}
}
if (j == num_courses) {
printf("Course '%s' not among courses offered. ",
requirements[i]);
} else {
if (courses[j].num_prerequisites != 0) {
//search for prerequisites
printf("%d ", num_years);
for (k = 0; k < courses[j].num_prerequisites; k++) {
int l;
for (l = 0; l < num_courses; l++) {
if (strcmp(courses[j].prerequisites[k],
courses[l].course_name) == 0)
break;
}
printf("%d %d %s ", num_years - 1, courses[l].quarter,
courses[l].course_name);
}
printf("%d %d %s ", num_years, courses[j].quarter,
courses[j].course_name);
num_years++;
} else {
printf("%d %d %s ", num_years, courses[j].quarter,
courses[j].course_name);
num_years++;
}
}
}
//close files
fclose(offering_file);
fclose(prerequisite_file);
fclose(requirement_file);
return EXIT_SUCCESS;
}
-----------------------------------------
offering.txt:
--------------------------
ICS31 1
ICS32 2
ICS33 3
--------------------------
prerequisite.txt:
------------------------
ICS32 ICS31
ICS33 ICS32 MATH3A
-------------------------
requirement.txt:
------------------------
CS151 CS161 CS171
-------------------------
Output:
--------------------------
Course offerings:
------------------
Course:ICS31
Quarter:1
Prerequisites:0
------------------
Course:ICS32
Quarter:2
Prerequisites:1
ICS31
------------------
Course:ICS33
Quarter:3
Prerequisites:2
ICS32
MATH3A
------------------
Course plan:
Course 'CS151' not among courses offered.
Course 'CS161' not among courses offered.
Course 'CS171' not among courses offered.
-----------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.