Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use c not c++ to solve ! Department of computer science at Birzeit University of

ID: 3845113 • Letter: U

Question

use c not c++ to solve !

Department of computer science at Birzeit University offers a set of courses each semester. These courses are listed in a file as follows: course name, course id, number ofregistered student, and max number of students. Number of registered students is initialized to zero always. The file that contains the courses’ information is formatted as follows (i.e. sample course file): Each semester, students are required to register a set of courses. The students are listed in a file along with the courses to be registered as follows: student name, student id, courses to be registered (COMP242, COMP336, COMP231…). The file that contains the students’ information is formatted as follows (i.e. sample student file): Course File Java#COMP231#0#27 Data Structures #COMP2321#0#30 Algorithms#COMP336#0#15 Computer and Programming#COMP142#0#27 Student File Ahmad Ali#1159999#COMP242#COMP336#COMP231#COMP338 Ali Mahmoud#1169999#COMP142#COMP336#COMP231 Ashraf Hasan #1139999#COMP142#COMP336#COMP231#COMP333#COMP432#COMP338#COMP233 Programming Requirements: 1. Your program should use the concept of struct. Hint: create two structs, one for course and the second for student. typedef struct { char sName[30]; int stdNo; int NoRegCourses; }student; typedef struct { char cName[30]; char cNo[10]; int maxRegNo; int currentRegNo; student sList[50]; }course; 2. Your program should use some of string functions, e.g. strlen, strtok. 3. Hint: to convert a string to integer you may use the following function atoi(). Example: int val; char str[20]; strcpy(str, "116987"); val = atoi(str); printf("String value = %s value = %d ", str, val); What to Do: 1. Read the offered courses from the course file into an array of type course. 2. Read the students from the student file, and add the student to the required courses. Before adding a student to any course, you have to make sure that: a. The course is offered by the department (i.e. is available in the course list). For example, Ashraf Hasan want to register COMP333, but it is not offered by the department. b. The student can register for at most 4 courses each semester. c. Number of registered students in each course should not exceed the maximum number of students. For example, if 23 students want to register COMP336, then just the first 15 students should be added to this course. 3. After that, for each course print the list of students.

Explanation / Answer

Here is the code and output for the question. Please don't forget to rate the answer if it helped. Thank you very much.

For the sake of testing , the course 231 is allowed max 2 enrollments in the courses.txt

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char sName[30];
int stdNo;
int NoRegCourses;
}student;

typedef struct
{
char cName[30];
char cNo[10];
int maxRegNo;
int currentRegNo;
student sList[50];
}course;

/*returns a course struct by processing the line and getting tokens using # delimiter*/
course createCourse(char *line)
{
char *token;
course c;
token = strtok(line,"#");/*1st token course name*/
strcpy(c.cName, token);
token = strtok(NULL, "#"); /*2nd token course id*/
strcpy(c.cNo, token);
token = strtok(NULL, "#"); /*3rd token current num of registrations*/
c.currentRegNo = atoi(token);
token = strtok(NULL, "#"); /*4th token max number of registrations*/
c.maxRegNo = atoi(token);
return c;
}

/*searches the list of courses having n courses. Returns the index of the course if the course no. found else returns -1 */
int findCourse(char *courseNo, course list[], int n)
{
int i;

for(i = 0; i < n; i++)
{

if(strcmp(list[i].cNo,courseNo)==0)
return i;
}
return -1;
}

/*process a line from student file. Updates the course list based on student enrollments*/
void processStudent(char *line, course list[],int n)
{
char *token;
student s;

int idx, sidx,len;

/*remove the newline character at the end of line, otherwise the last coursenumber will contain newline character also*/
len = strlen(line);
if(len > 1 && line[len-1] == ' ')
line[len-1] = '';

token = strtok(line,"#");/*1st token student name*/
strcpy(s.sName, token);
token = strtok(NULL, "#"); /*2nd token student no*/
s.stdNo = atoi(token);
s.NoRegCourses = 0;

/*now next tokens are course numbers*/
while((token = strtok(NULL, "#")) != NULL)
{

idx = findCourse(token, list, n);

if(idx == -1) /*course number not offered by department*/
{
printf(" Could not find course %s for student Id %d",token, s.stdNo);
continue;
}
else
{

if(s.NoRegCourses < 4) /*a student can only enroll to max 4 courses*/
{
if(list[idx].currentRegNo < list[idx].maxRegNo) /*check if the course still has room for this student*/
{
s.NoRegCourses++;
list[idx].sList[list[idx].currentRegNo]=s;
list[idx].currentRegNo++;

}
else
{
printf(" Can not enroll Student Id %d to course %s as its already reached max registrations(%d).",s.stdNo,list[idx].cNo,list[idx].maxRegNo);
}
}
else
{
printf(" Can not enroll Student Id %d to course %s as student already has 4 enrollments",s.stdNo, list[idx].cNo);
}
}
}

}

/*prints the course id and list of student ids who have enrolled for the course*/

void printCourses(course list[], int n)
{
int i,j,k;
printf(" Course No. Enrolled Student Ids");
printf(" =========================================");
for(i = 0; i < n; i++)
{
printf(" %-12s",list[i].cNo);
k = list[i].currentRegNo;

for(j = 0; j < k; j++)
{
printf("%d ",list[i].sList[j].stdNo);
}
}
}

int main()
{
FILE *courseFile, *studFile;

char line[500];/*max line length assuming to be 256*/
course courseList[100];
int n = 0;

courseFile = fopen("courses.txt","r");
if(courseFile == NULL)
{
printf(" Could not find file courses.txt!");
exit(1);
}
studFile = fopen("students.txt","r");

if(studFile == NULL)
{
printf(" Could not find file students.txt!");
exit(1);
}


while(fgets(line,500, courseFile))
{
courseList[n] = createCourse(line);
n++;
}
fclose(courseFile);


while(fgets(line,500, studFile))
{

processStudent(line, courseList, n);
}
fclose(studFile);

printCourses(courseList, n);

printf(" Done ");
}

input file : courses.txt

Java#COMP231#0#2
Data Structures #COMP2321#0#30
Algorithms#COMP336#0#15
Computer and Programming#COMP142#0#27

input file: students.txt

Ahmad Ali#1159999#COMP242#COMP336#COMP231#COMP338
Ali Mahmoud#1169999#COMP142#COMP336#COMP231
Ashraf Hasan #1139999#COMP142#COMP336#COMP231#COMP333#COMP432#COMP338#COMP233

output

Could not find course COMP242 for student Id 1159999
Could not find course COMP338 for student Id 1159999
Can not enroll Student Id 1139999 to course COMP231 as its already reached max registrations(2).
Could not find course COMP333 for student Id 1139999
Could not find course COMP432 for student Id 1139999
Could not find course COMP338 for student Id 1139999
Could not find course COMP233 for student Id 1139999
Course No.    Enrolled Student Ids
=========================================
COMP231 1159999 1169999
COMP2321
COMP336 1159999 1169999 1139999
COMP142 1169999 1139999

Done