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

The goal is to maintain a file of students and their registered courses using st

ID: 3868941 • Letter: T

Question

The goal is to maintain a file of students and their registered courses using structures, pointers to structures, dynamic memory allocation and file manipulation. A structure called “studentInfo” is used with the following member elements defined:

•    StudentID - 9 char long (unique and 9 characters of numbers such as 123456789, 103449977)

•    First Name - 20 char long (started with capital letters)

•    Last Name - 25 char long (started with capital letters)

•    Number of Courses Attending – integer ()

•    Array of CourseInfo - a 10 element array of courseInfo elements

•    next - Pointer to the next student structure in the list

where the “CourseInfo” structure is defined as:

struct CourseInfo{

int courseID;

char courseName[30];

};

The program MUST implement at least the following functions:

a)   addStudent () : To add a new student and his/her registered courses.

-              Make sure the first letter of the first and last names is upper case (capital letter).

-              The student ID should be unique; cannot have two students with the same StudentID. So, before adding a student, search for the studentID to be ensure it has not been previously entered.

-              If the linked list is empty, the new student is the head of the list. If not, search through the linked list to find the appropriate spot within the sorted linked list.

b) deleteStudent(): To delete a student information using its StudentID.

-              Search the linked list for a student matching the studentID passed in. If it is found, delete it from the linked list.

-              Note that the linked list is sorted based on studentID

c)   searchStudentID(): To search for a student using studentID

-              Not necessary to search all the linked list as it is sorted based on studendID’s.

-              This function can be called from addStudent() and deleteStudent() functions.

d) searchStudentlName(): To search for a student using his/her last name and display the related information if the student exists.

-              It has to search all the linked list as it is not sorted based on last names

-              Before starting the search, the name supplied by the user must have a capital letter as the first letter.

e)   displayStudentInfo(): To display the current student information that exists in the linked list

f)   saveStudentInfo(): To save student information from the sorted linked list to a file (inputStudents.txt)

g) loadStudentinfo(): To read all the student information from an input file (studentList.txt)

-              This function should be called at the beginning of the program to load all previous student information saved in the input file

-              Student information should be formatted and stored in a sorted linked list.

h) terminate(): To save student information in a file (inputStudents.txt) and exit from the program

A data file called “studentList.txt” is provided, the file has the data for a number of students. The data in this file needs to be read from the input file into a sorted linked list data structure, the linked list must be sorted based on the StudentID.

The sample input file will end with a line that has three stars. For each student, the data file is formatted line by line in the following manner:

<student ID>

<first name>

<last name>

<number of courses they are taking>

<course name> <course id>

An example is shown below:

studentList.txt

111111111

Lisa

Porter

3

ENEE 114

CMSC 412

NME 515

333333333

Alex

Simpson

1

CMSC 412

***

After loading the student list, the program should be able to interactively ask the user for input and an interactive menu with the following inputs:

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

NOTE: Student information should be sorted based on the student ID’s both in the linked list and in the input/output file

Below is a sample of what is expected from the completed code:

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 1

Adding new student: Student ID : 222222222

First name : john

Last name : Rezaei

Number of courses: 2

Course ID: 412

Course Name: CMSC Course ID: 123

Course Name: MATH

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 4

Student 1:

111111111

Lisa

Porter

3

ENEE 114

CMSC 412

ENME 515

Student 2:

222222222

John

Rezaei

2

CMSC 412

MATH 123

Student 3:

333333333

Alex

Simpson

1

CMSC 412

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 3

What is the last name of student? porter

111111111

Lisa

Porter

3

ENEE 114

CMSC 412

ENME 515

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 2

Student ID: 111111111

Student information deleted.

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 4

Student 1:

222222222

John

Rezaei

2

CMSC 412

MATH 123

Student 2:

333333333

Alex

Simpson

1

CMSC 412

1. Add new student

2. Delete a student

3. Search for a student

4. Display current students

5. Save student information to file

6. Exit

> 6

Save student information to file before leaving?

Student List saved successfully. Bye!

Explanation / Answer

Given below is the C code for the question. The question has not specified the programming language and based on the description, the answer is given for C Programming language. Please do rate the answer if it helped. Thank you.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct courseInfo
{
int courseID;
char courseName[30];
};
struct studentInfo
{
char studentId[10];
char firstName[20];
char lastName[25];
int numCourses;
struct courseInfo courses[10];
struct studentInfo* next;
};

//capitalize the 1st character
char* changeCase(char *str)
{
char *s2 = str;
  
*str = toupper(*str);
str++;
while(*str != '')
{
*str = tolower(*str);
str++;
}
return s2;
  
}
struct studentInfo* createStudent(char *id, char *fname, char *lname, int nc, struct courseInfo cinfo[])
{
int i;
struct studentInfo *s = (struct studentInfo*) malloc(sizeof (struct studentInfo));

strcpy(s->studentId, id);
strcpy(s->firstName, changeCase(fname));
strcpy(s->lastName, changeCase(lname));
s->numCourses = nc;
for(i = 0; i < nc; i++)
s->courses[i] = cinfo[i];
s->next = NULL;
return s;
}

//return 1 on success and 0 if student not added
int addStudent(struct studentInfo **head,char *id, char *fname, char *lname, int nc, struct courseInfo cinfo[])
{

struct studentInfo *curr, *prev;
if(*head == NULL)
{
*head =createStudent(id, fname, lname, nc, cinfo);
return 1;
}
else
{
prev = NULL;
curr = *head;
while(curr != NULL)
{
if(strcmp(id , curr->studentId) < 0)
break;
else if(strcmp(id , curr->studentId) == 0)
return 0;
else
{
prev = curr;
curr = curr->next;
}
}
  
struct studentInfo *s = createStudent(id, fname, lname, nc, cinfo);
s->next = curr;
if(prev == NULL) //adding before
*head = s;
else
prev->next = s;
return 1;
}
}

//return 1 if student deleted , 0 otherwise
int deleteStudent(struct studentInfo **head,char *id)
{
struct studentInfo *prev = NULL, *curr;
if(*head == NULL) //empty list
return 0;
else
{
curr = *head;
while(curr != NULL)
{
if(strcmp(curr->studentId, id) == 0)
break;
prev = curr;
curr = curr->next;
}
  
if(curr == NULL) //not found
return 0;
else
{
if(prev == NULL)//delete head node
*head = curr->next;
else
prev->next = curr->next;
free(curr);
return 1;
}
}
}


struct studentInfo* searchStudentID(struct studentInfo *head, char *id)
{
struct studentInfo *prev = NULL, *curr = head;
while(curr != NULL)
{
if(strcmp(curr->studentId, id) == 0)
return curr;
else if(strcmp(id, curr->studentId) < 0) //already reach higher ids and still not found
return 0;
prev = curr;
curr = curr->next;
}
return 0; //reached end of list
}


struct studentInfo* searchStudentLName(struct studentInfo *head, char *lname)
{
struct studentInfo *prev = NULL, *curr = head;
changeCase(lname);

while(curr != NULL)
{
if(strcmp(curr->lastName, lname) == 0)
return curr;
  
prev = curr;
curr = curr->next;
}
return 0; //reached end of list
}

void displayStudent(struct studentInfo* s)
{
int j;
printf("%s ", s->studentId);
printf("%s ", s->firstName);
printf("%s ", s->lastName);
printf("%d ", s->numCourses);
for(j = 0; j < s->numCourses; j++)
printf("%s %d ", s->courses[j].courseName, s->courses[j].courseID);
printf(" ");

}
void displayStudentInfo(struct studentInfo *head)
{
struct studentInfo *s = head;
int i = 1;
while(s != NULL)
{
printf("Student %d ", i++);
displayStudent(s);
s = s->next;
}
printf(" ");
}


void saveStudentInfo(struct studentInfo *head)
{
struct studentInfo *s = head;
int i;
FILE *fp = fopen("inputStudents.txt", "w");
while(s != NULL)
{
fprintf(fp, "%s ", s->studentId);
fprintf(fp, "%s ", s->firstName);
fprintf(fp, "%s ", s->lastName);
fprintf(fp, "%d ", s->numCourses);
for(i = 0; i < s->numCourses; i++)
fprintf(fp, "%s %d ", s->courses[i].courseName, s->courses[i].courseID);
s = s->next;
}
fclose(fp);
}


struct studentInfo* loadStudentInfo()
{
struct studentInfo *head = NULL;
FILE *fp = fopen("studentList.txt", "r");
char fname[20], lname[25], id[10];
int nc, i;
struct courseInfo cinfo[10];
  
if(fp == NULL)
{
printf("Input file studentList.txt not found");
return head;
}
  
while(fscanf(fp, "%s", id) == 1)
{
if(strcmp(id, "***") == 0) //end of file
break;
fscanf(fp, "%s", fname);
fscanf(fp, "%s", lname);
fscanf(fp, "%d", &nc);
for(i = 0; i < nc; i++)
fscanf(fp,"%s %d", cinfo[i].courseName, &cinfo[i].courseID);
addStudent(&head, id, fname, lname, nc, cinfo);
}
fclose(fp);
return head;
}

void terminate(struct studentInfo *head)
{
struct studentInfo *temp;
char ans[2];
printf("Save student information to file before leaving (y/n)? ");
scanf("%s", ans);
if(ans[0] == 'y' || ans[0] == 'Y')
{
saveStudentInfo(head);
printf("Student List saved successfully. Bye! ");
}
  
//deallcate memory
while(head != NULL)
{
temp = head->next;
free(head);
head = temp;
}
exit(0);
}
void getStudentInputAndAdd(struct studentInfo **head)
{
char id[10], fname[20], lname[25];
int nc, i;
struct courseInfo cinfo[10];
  
printf("Adding new student: ");
printf("Student ID: ");
scanf("%s", id);
printf("First name: ");
scanf("%s", fname);
printf("Last name: ");
scanf("%s", lname);
printf("Number of courses: ");
scanf("%d", &nc);
for(i = 0; i < nc; i++)
{
printf("Course ID: ");
scanf("%d", &cinfo[i].courseID);
printf("Course Name: ");
scanf("%s", cinfo[i].courseName);
}
  
if(addStudent(head, id, fname, lname, nc, cinfo))
printf("Student added successfully. ");
else
printf("Duplicate student ID. ");
  
  
}
int main()
{
struct studentInfo *head = NULL, *temp;
int choice;
char id[10], lname[30];
head = loadStudentInfo();
while(1)
{
printf("1. Add new student ");
printf("2. Delete a student ");
printf("3. Search for a student ");
printf("4. Display current students ");
printf("5. Save student information to file ");
printf("6. Exit ");
printf("> ");
scanf("%d", &choice);
switch(choice)
{
case 1:
getStudentInputAndAdd(&head);
break;
case 2:
printf("Student Id: ");
scanf("%s", id);
if(deleteStudent(&head, id))
printf("Student information deleted ");
else
printf("Student NOT found.");
break;
case 3:
printf("What is the last name of student? ");
scanf("%s", lname);
temp = searchStudentLName(head, lname);
if(temp != NULL)
displayStudent(temp);
else
printf("Student NOT found.");
break;
case 4:
displayStudentInfo(head);
break;
case 5:
saveStudentInfo(head);
break;
case 6:
terminate(head);
break;
default:
printf("Invalid menu choice ");
  
}
}
}

input file: studentList.txt

111111111
Lisa
Porter
3
ENEE 114
CMSC 412
NME 515
333333333
Alex
Simpson
1
CMSC 412
***

output

$ ./a.out
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 4
Student 1
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
NME 515


Student 2
333333333
Alex
Simpson
1
CMSC 412

1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 1
Adding new student:
Student ID: 222222222
First name: john
Last name: Rezaei
Number of courses: 2
Course ID: 412
Course Name: CMSC
Course ID: 123
Course Name: MATH
Student added successfully.

1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 4
Student 1
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
NME 515


Student 2
222222222
John
Rezaei
2
CMSC 412
MATH 123


Student 3
333333333
Alex
Simpson
1
CMSC 412

1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 3
What is the last name of student? porter
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
NME 515


1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 2
Student Id: 111111111
Student information deleted
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 4
Student 1
222222222
John
Rezaei
2
CMSC 412
MATH 123


Student 2
333333333
Alex
Simpson
1
CMSC 412

1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 6
Save student information to file before leaving (y/n)? y
Student List saved successfully. Bye!

====================
$ cat inputStudents.txt
222222222
John
Rezaei
2
CMSC 412
MATH 123
333333333
Alex
Simpson
1
CMSC 412

output file: inputStudent.txt

222222222
John
Rezaei
2
CMSC 412
MATH 123
333333333
Alex
Simpson
1
CMSC 412

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote