Exported from Notepad++
1 /******************************************************************************************
2 *Filename: main.c
3 *Version: 1
4 *Authors: Mostapha Abdelaziz
5 *Course: C Language CST8234
6 *Assignment Number: 1
7 *Assignment Name: Assignment 1: Student Registration System
8 *Purpose: Create a student registration system using student numbers and courses
9 ******************************************************************************************/
10
11 #include "header.h"
12
13 /******************************************************************************************
14 *Function name: main
15 *Purpose: Main function for the program - Student Registration System
16 *Function In parameters: None
17 *Function Out parameters: An integer for function success
18 *Version: 1
19 *Authors: Mostapha Abdelaziz
20 ******************************************************************************************/
21 int main()
22 {
23 int *studentList;
24 char *course;
25 char **courseList;
26 int **registrationTable;
27
28 int numberOfStudents = -1;
29 int courseLength = 7;
30 int numberOfCourses = -1;
31
32 int menuChoice = -1;
33 int i;
34
35 printf("Welcome to the Student Register System (SRS)\n\n");
36
37 /* ask for number of students */
38 while (numberOfStudents == -1)
39 {
40 printf("How many students would you like to register: ");
41 numberOfStudents = validateInt();
42 }
43
44 /* dynamically allocate the array for student numbers */
45 studentList = calloc(numberOfStudents, sizeof(int));
46 if (studentList == NULL)
47 {
48 printf("Error - could not allocate memory for student list");
49 return EXIT_FAILURE;
50 }
51
52 /* ask for each student number and print */
53 populateStudentList(studentList, numberOfStudents);
54
55 /* ask for courses */
56 while (numberOfCourses == -1)
57 {
58 printf("How many courses are you offering: ");
59 numberOfCourses = validateInt();
60 }
61
62 /* allocate a string for a single course name */
63 course = calloc(courseLength, sizeof(char));
64 if (course == NULL)
65 {
66 printf("Error - could not allocate memory for course name");
67 return EXIT_FAILURE;
68 }
69
70 /* dynamically allocate array for course names */
71 courseList = calloc(numberOfCourses, sizeof(char *));
72 if (courseList == NULL)
73 {
74 printf("Error - could not allocate memory for course list");
75 return EXIT_FAILURE;
76 }
77
78 /* ask for each course name and print */
79 populateCourseList(courseList, course, numberOfCourses);
80
81 /* allocate 2d array for registration table, then set values to -1 */
82 registrationTable = calloc(numberOfStudents, sizeof(int *));
83 if (registrationTable == NULL)
84 {
85 printf("Error - could not allocate memory for registration table");
86 return EXIT_FAILURE;
87 }
88 for (i = 0; i < numberOfStudents; i++)
89 {
90 registrationTable[i] = calloc(numberOfCourses, sizeof(int));
91 if (registrationTable[i] == NULL)
92 {
93 printf("Error - could not allocate memory for registration table");
94 return EXIT_FAILURE;
95 }
96 }
97
98 /* loop through switch implementing the menu */
99 while (menuChoice != 4)
100 {
101 menuChoice = printMenu();
102 printf("\n");
103 switch (menuChoice)
104 {
105 case 1:
106 /* add a course if there are students/courses */
107 if (numberOfStudents == 0)
108 {
109 printf("Error: No students in Students Table.\n");
110 }
111 else if (numberOfCourses == 0)
112 {
113 printf("Error: No courses in Courses Table.\n");
114 }
115 else
116 {
117 changeRegistration(studentList, numberOfStudents, courseList, course, numberOfCourses, registrationTable, 1);
118 }
119 break;
120 case 2:
121 /* drop a course if there are students/courses */
122 if (numberOfStudents == 0)
123 {
124 printf("Error: No students in Students Table.\n");
125 }
126 else if (numberOfCourses == 0)
127 {
128 printf("Error: No courses in Courses Table.\n");
129 }
130 else
131 {
132 changeRegistration(studentList, numberOfStudents, courseList, course, numberOfCourses, registrationTable, 2);
133 }
134 break;
135 case 3:
136 /* print the registration table if there are students or courses */
137 if (numberOfStudents == 0)
138 {
139 printf("Error: No students in Students Table.\n");
140 }
141 else if (numberOfCourses == 0)
142 {
143 printf("Error: No courses in Courses Table.\n");
144 }
145 else
146 {
147 printRegister(registrationTable, numberOfStudents, numberOfCourses, courseList, studentList);
148 }
149 break;
150 case 4:
151 /* exit the loop & quit the program */
152 break;
153 default:
154 return EXIT_FAILURE;
155 }
156 }
157
158 /* free the memory allocated */
159 for (i = 0; i < numberOfCourses; i++)
160 {
161 free(courseList[i]);
162 }
163 free(courseList);
164
165 for (i = 0; i < numberOfStudents; i++)
166 {
167 free(registrationTable[i]);
168 }
169 free(registrationTable);
170
171 free(studentList);
172
173 free(course);
174
175 return EXIT_SUCCESS;
176 }
177
178 /******************************************************************************************
179 *Function name: validateInt
180 *Purpose: Reads input and validates an integer to ensure it is 0 or greater
181 *Function In parameters: None
182 *Function Out parameters: The validated integer value
183 *Version: 1
184 *Authors: Mostapha Abdelaziz
185 ******************************************************************************************/
186 int validateInt()
187 {
188 int returnCode;
189 int input;
190
191 /* read input */
192 returnCode = scanf("%d", &input);
193 /* check if it is an int, then range */
194 if (returnCode != 1)
195 {
196 printf("Error: enter a integer values only \n");
197 input = -1;
198 }
199 else if (input < 0)
200 {
201 printf("Error: enter a positive value \n");
202 input = -1;
203 }
204
205 while (getchar() != '\n')
206 ;
207
208 return input;
209 }
210
211 /******************************************************************************************
212 *Function name: validateSN
213 *Purpose: Reads input and validates a student number
214 *Function In parameters: None
215 *Function Out parameters: The validated student number
216 *Version: 1
217 *Authors: Mostapha Abdelaziz
218 ******************************************************************************************/
219 int validateSN()
220 {
221 int student = -1;
222 int returnCode;
223
224 /* read input and validate value */
225 returnCode = scanf("%d", &student);
226 /* if not a number, otherwise check range */
227 if (returnCode != 1)
228 {
229 printf("Error: student number must consist of integers only \n");
230 student = -1;
231 }
232 else if ((student < 10000) || (student > 99999))
233 {
234 printf("Error: enter a valid 5 digit number that does not start with 0 \n");
235 student = -1;
236 }
237 while (getchar() != '\n')
238 ;
239
240 return student;
241 }
242
243 /******************************************************************************************
244 *Function name: validateCourse
245 *Purpose: Reads input and validates it is correct format for a course name
246 *Function In parameters: None
247 *Function Out parameters: None
248 *Version: 1
249 *Authors: Mostapha Abdelaziz
250 ******************************************************************************************/
251 void validateCourse(char *course)
252 {
253 int i;
254
255 /* read input */
256 scanf("%8[^\n]*c", course);
257 /* if the first char is null, or the 8th is not, print error */
258 if (course[0] == '\0')
259 {
260 printf("Error: enter a course code in the format LLLNNNN where L is a letter and N is a number \n");
261 }
262 else if (course[7] != '\0')
263 {
264 printf("Error: course code is too long. Must be 7 characters. \n");
265 course[0] = '\0';
266 }
267 else
268 {
269 /* check that the first 3 are letters, last 4 are numbers */
270 for (i = 0; i < 3; i++)
271 {
272 if (course[i] >= 'a' && course[i] <= 'z')
273 {
274 /* do nothing */
275 }
276 else if (course[i] >= 'A' && course[i] <= 'Z')
277 {
278 /* do nothing */
279 }
280 else
281 {
282 course[0] = '\0';
283 i = 10;
284 printf("Error: first three characters must be letters \n");
285 }
286 }
287
288 if (i == 3)
289 {
290 for (i = 3; i < 7; i++)
291 {
292 if (course[i] >= '0' && course[i] <= '9')
293 {
294 /* do nothing */
295 }
296 else
297 {
298 course[0] = '\0';
299 i = 10;
300 printf("Error: last four characters must be digits \n");
301 }
302 }
303 }
304 }
305 while (getchar() != '\n')
306 ;
307 }
308
309 /******************************************************************************************
310 *Function name: printMenu
311 *Purpose: Prints the menu for the SRS and validates menu input
312 *Function In parameters: None
313 *Function Out parameters: The validated input for the menu
314 *Version: 1
315 *Authors: Mostapha Abdelaziz
316 ******************************************************************************************/
317 int printMenu()
318 {
319 int returnCode;
320 int menuChoice = -1;
321
322 while (menuChoice == -1)
323 {
324 printf("Please choose one of the following actions:\n1- Register a student in a course\n2- Drop a student's course\n3- Print registration table\n4- Quit\nPlease enter action number: ");
325 returnCode = scanf("%d", &menuChoice);
326 /* validate */
327 if (returnCode != 1)
328 {
329 printf("Error: enter an integer value \n");
330 menuChoice = -1;
331 }
332 else if (menuChoice < 1 || menuChoice > 4)
333 {
334 printf("\nError: unknown action: %d\nTry again...\n", menuChoice);
335 menuChoice = -1;
336 }
337
338 while (getchar() != '\n')
339 ;
340 }
341 return menuChoice;
342 }
343
344 /******************************************************************************************
345 *Function name: studentExists
346 *Purpose: Checks if the student number exists in the array
347 *Function In parameters: The array of student numbers, the number of students, and the student number to check
348 *Function Out parameters: The index at which the student number is or -1
349 *Version: 1
350 *Authors: Mostapha Abdelaziz
351 ******************************************************************************************/
352 int studentExists(int students[], int numberOfStudents, int studentNumber)
353 {
354 int i;
355
356 /* if the student number is found, return index otherwise -1 */
357 for (i = 0; i < numberOfStudents; i++)
358 {
359 if (students[i] == studentNumber)
360 {
361 return i;
362 }
363 }
364 return -1;
365 }
366
367 /******************************************************************************************
368 *Function name: courseExists
369 *Purpose: Checks if the course exists in the array
370 *Function In parameters: The array of courses, the number of courses, and the course name to check
371 *Function Out parameters: The index at which the course is or -1
372 *Version: 1
373 *Authors: Mostapha Abdelaziz
374 ******************************************************************************************/
375 int courseExists(char **courses, int numberOfCourses, char *course)
376 {
377 int i;
378
379 /* if the course is found, return index otherwise -1 */
380 for (i = 0; i < numberOfCourses; i++)
381 {
382 if (strcmp(courses[i], course) == 0)
383 {
384 return i;
385 }
386 }
387 return -1;
388 }
389
390 /******************************************************************************************
391 *Function name: populateStudentList
392 *Purpose: Validates input and adds as many student numbers as indicated into the array
393 *Function In parameters: The array of student numbers, and the number of students
394 *Function Out parameters: None
395 *Version: 1
396 *Authors: Mostapha Abdelaziz
397 ******************************************************************************************/
398 void populateStudentList(int studentList[], int numberOfStudents)
399 {
400 int student = -1;
401 int i;
402
403 for (i = 0; i < numberOfStudents; i++)
404 {
405 /* validate input */
406 while ((student < 10000) || (student > 99999))
407 {
408 printf("Please enter the student ID for student %d: ", (i + 1));
409 /* validates student number */
410 student = validateSN();
411 /* check if in use */
412 if (((student > 10000) && (student < 99999)) && (i != 0))
413 {
414 if (studentExists(studentList, i, student) != -1)
415 {
416 printf("Error: student number %d in use. Try again.\n", student);
417 student = -1;
418 }
419 }
420 }
421 /* store student number */
422 studentList[i] = student;
423 student = -1;
424 }
425 /* print all students */
426 printStudentNumbers(studentList, numberOfStudents);
427 }
428
429 /******************************************************************************************
430 *Function name: populateCourseList
431 *Purpose: Validates input and adds as many courses as indicated inot the array
432 *Function In parameters: The array of courses, a variable for course name, and the number of courses
433 *Function Out parameters: None
434 *Version: 1
435 *Authors: Mostapha Abdelaziz
436 ******************************************************************************************/
437 void populateCourseList(char **courseList, char *course, int numberOfCourses)
438 {
439 int i;
440 course[0] = '\0';
441
442 for (i = 0; i < numberOfCourses; i++)
443 {
444 courseList[i] = (char *)malloc(sizeof(char) * 7);
445 /* ask and store input, loops to validate */
446 while (course[0] == '\0')
447 {
448 printf("Please enter the course code for course %d: ", (i + 1));
449 validateCourse(course);
450 if ((i != 0) && (course[0] != '\0'))
451 {
452 if (courseExists(courseList, i, course) != -1)
453 {
454 printf("Error: course code %s is in use. Try again.\n", course);
455 course[0] = '\0';
456 }
457 }
458 }
459 /* copy validated course to the array */
460 strcpy(courseList[i], course);
461 course[0] = '\0';
462 }
463 /* print all the courses */
464 printCourses(courseList, numberOfCourses);
465 }
466
467 /******************************************************************************************
468 *Function name: printStudentNumbers
469 *Purpose: Prints the added student numbers
470 *Function In parameters: The array of student numbers, and the number of students
471 *Function Out parameters: None
472 *Version: 1
473 *Authors: Mostapha Abdelaziz
474 ******************************************************************************************/
475 void printStudentNumbers(int studentList[], int numberOfStudents)
476 {
477 int i;
478
479 /* loop through array and print each student number if there are students */
480 printf("Students: ");
481 if (numberOfStudents != 0)
482 {
483 for (i = 0; i < numberOfStudents - 1; i++)
484 {
485 printf("%d, ", studentList[i]);
486 }
487 printf("%d\n", studentList[i]);
488 }
489 else
490 {
491 printf("None registered\n");
492 }
493 }
494
495 /******************************************************************************************
496 *Function name: printCourses
497 *Purpose: Prints the added course names
498 *Function In parameters: The array of courses, and the number of courses
499 *Function Out parameters: None
500 *Version: 1
501 *Authors: Mostapha Abdelaziz
502 ******************************************************************************************/
503 void printCourses(char *courseList[], int numberOfCourses)
504 {
505 int i;
506
507 /* loop through array and print each course if there are courses */
508 printf("Courses: ");
509 if (numberOfCourses != 0)
510 {
511 for (i = 0; i < numberOfCourses - 1; i++)
512 {
513 printf("%s, ", courseList[i]);
514 }
515 printf("%s\n", courseList[i]);
516 }
517 else
518 {
519 printf("No courses offered\n");
520 }
521 }
522
523 /******************************************************************************************
524 *Function name: changeRegistration
525 *Purpose: Adds a student to a course or drops them
526 *Function In parameters: Student array, amount of students, course array, course variable, number of courses, registration array and 1 for add or 2 for drop
527 *Function Out parameters: None
528 *Version: 1
529 *Authors: Mostapha Abdelaziz
530 ******************************************************************************************/
531 void changeRegistration(int studentList[], int numberOfStudents, char **courseList, char *course, int numberOfCourses, int **registrationTable, int addDrop)
532 {
533 int student = -1;
534 /* int i; */
535 int studentIndex;
536 int courseIndex;
537
538 /* ask for student number and store index */
539 while ((student < 10000) || (student > 99999))
540 {
541 printf("Please enter the student ID: ");
542 /* validates student number */
543 student = validateSN();
544 /* check if in use */
545 if (((student > 10000) && (student < 99999)))
546 {
547 studentIndex = studentExists(studentList, numberOfStudents, student);
548 if (studentIndex == -1)
549 {
550 printf("Error: student ID %d not found. Try again.\n", student);
551 student = -1;
552 }
553 }
554 }
555
556 /* ask for course number and store index */
557 course[0] = '\0';
558 while (course[0] == '\0')
559 {
560 printf("Please enter the course code: ");
561 validateCourse(course);
562 if ((course[0] != '\0'))
563 {
564 courseIndex = courseExists(courseList, numberOfCourses, course);
565 if (courseIndex == -1)
566 {
567 printf("Error: course code %s not found. Try again.\n", course);
568 course[0] = '\0';
569 }
570 }
571 }
572
573 /* alter registration table */
574 if (addDrop == 1)
575 {
576 registrationTable[studentIndex][courseIndex] = 1;
577 }
578 else
579 {
580 registrationTable[studentIndex][courseIndex] = 0;
581 }
582
583 /* print students courses */
584 printEnrCourses(registrationTable, studentIndex, numberOfCourses, courseList, studentList);
585 }
586
587 /******************************************************************************************
588 *Function name: printEnrCourses
589 *Purpose: Prints the courses a specific student is enrolled in
590 *Function In parameters: The 2d array for registration, the index the student is in, amount of courses, the courses array and the students array
591 *Function Out parameters: None
592 *Version: 1
593 *Authors: Mostapha Abdelaziz
594 ******************************************************************************************/
595 void printEnrCourses(int **array, int position, int columns, char **courses, int students[])
596 {
597 int i;
598
599 /* print student number and their enrolled courses */
600 printf("SN %d - enrolled courses\n", students[position]);
601 for (i = 0; i < columns; i++)
602 {
603 if (array[position][i] == 1)
604 {
605 printf("%s ", courses[i]);
606 }
607 }
608 printf("\n\n");
609 }
610
611 /******************************************************************************************
612 *Function name: printRegister
613 *Purpose: Prints the register of enrollment, ENR indicated enrolled and NER indicates the opposite
614 *Function In parameters: The 2d array for registration, amount of students, amount of courses, the courses array and the students array
615 *Function Out parameters: None
616 *Version: 1
617 *Authors: Mostapha Abdelaziz
618 ******************************************************************************************/
619 void printRegister(int **array, int rows, int columns, char **courses, int students[])
620 {
621 int i;
622 int j;
623
624 /* print header */
625 printf("\n%8s", " ");
626 for (i = 0; i < columns; i++)
627 {
628 printf("%10s", courses[i]);
629 }
630 printf("\n\n");
631
632 /* print rows and table info */
633 for (i = 0; i < rows; i++)
634 {
635 printf("SN %d", students[i]);
636 for (j = 0; j < columns; j++)
637 {
638 if (array[i][j] == 1)
639 {
640 printf("%10s", "X");
641 }
642 else
643 {
644 printf("%10s", "--");
645 }
646 }
647 printf("\n\n");
648 }
649
650 printf("\nAn unenrolled student is indicated with '--', and enrolled student with 'X'\n\n");
651 }