Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. You are given a partially completed program hw05q1.c. You should follow the instructions given in the program to complete the functions so that the

1. You are given a partially completed program hw05q1.c. You should follow the instructions given in the program to complete the functions so that the program executes as instructed. The program declares a struct studentRecord with elements for student's name, student's major, school year of student, student ID. You will be completing a program that creates a list of students (array of structures). It is a menudriven program where the user is given the following options:

a) Add a new student to the list. When adding a new student to the list, the user is prompted for student's name, student's major, school year of student and student ID. The student should be added at the end of the list. If the student already exists in the list, then you should not add to the list. The school year is enum type.

b) Display the list of students. This should display each students details one after the other. CSE240 Introduction to Programming Language 2 | Page Homework 05

c) Sort the list of students alphabetically by student name. The sorting should happen within the list. You should not create a new list (array of structs) of students having sorted students. There is save() already implemented to write the students list to a file Student_List.txt. save() is executed at the end of the program when the user quits the program. You need to implement load() which is called at the start of the program. This function will read the saved file and fill in the array of structures. You need to read the file in the same order and manner that it is saved in save().

Expected output of each function:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

/ READ BEFORE YOU START:

// You are given a partially completed program that creates a list of students,

like students' record.

// Each record has this information: student's name, major, school year of student,

ID.

// The struct 'studentRecord' holds information of one student. School year is enum

type.

// An array of structs called 'list' is made to hold the list of students.

// To begin, you should trace through the given code and understand how it works.

// Please read the instructions above each required function and follow the

directions carefully.

// You should not modify any of the given code, the return types, or the

parameters, you risk getting compile error.

// You are not allowed to modify main ().

// You can use string library functions.

// WRITE COMMENTS FOR IMPORANT STEPS IN YOUR CODE.

#include

#include

#include

#pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_STUDENTS 15

#define MAX_NAME_LENGTH 25

typedef enum { freshman = 0, sophomore, junior, senior } schoolYear; // enum type

struct studentRecord { // struct for student details

char studentName[MAX_NAME_LENGTH];

char major[MAX_NAME_LENGTH];

schoolYear schoolYear;

unsigned int studentID;

};

struct studentRecord list[MAX_STUDENTS]; // declare list of students

int count = 0;

// the number of students

currently stored in the list (initialized to 0)

// functions already implmented

void flushStdIn();

void executeAction(char);

void save(char* fileName);

// functions that need implementation:

int addSort(char* studentName_input, char* major_input, char* schoolYear_input,

unsigned int studentID_input); // 30 points

void display(); // 10 points

void load(char* fileName); // 10 points

int main()

{

char* fileName = "Student_List.txt";

load(fileName); // load list of students from file (if it exists).

Initially there will be no file.

char choice = 'i'; // initialized to a dummy value

do

{

printf(" Enter your selection: ");

printf("\t a: add a new student ");

printf("\t d: display student list ");

printf("\t q: quit ");

choice = getchar();

flushStdIn();

executeAction(choice);

} while (choice != 'q');

save(fileName); // save list of students to file (overwrites file, if it

exists)

return 0;

}

// flush out leftover ' ' characters

void flushStdIn()

{

char c;

do c = getchar();

while (c != ' ' && c != EOF);

}

// ask for details from user for the given selection and perform that action

void executeAction(char c)

{

char studentName_input[MAX_NAME_LENGTH], major_input[MAX_NAME_LENGTH];

unsigned int studentID_input, add_result= 0;

char schoolYear_input[20];

switch (c)

{

case 'a':

// input student record from user

printf(" Enter student name: ");

fgets(studentName_input, sizeof(studentName_input), stdin);

studentName_input[strlen(studentName_input) - 1] = '\0'; // discard

the trailing ' ' char

printf("Enter major: ");

fgets(major_input, sizeof(major_input), stdin);

major_input[strlen(major_input) - 1] = '\0'; // discard the trailing

' ' char

printf("Enter whether students is 'freshman' or 'sophomore' or 'junior'

or 'senior': ");

fgets(schoolYear_input, sizeof(schoolYear_input), stdin);

schoolYear_input[strlen(schoolYear_input) - 1] = '\0'; // discard

the trailing ' ' char

printf("Please student ID number: ");

scanf("%d", &studentID_input);

flushStdIn();

// add the student to the list

add_result = addSort(studentName_input, major_input, schoolYear_input,

studentID_input);

if (add_result == 0)

printf(" Student is already on the list! ");

else if (add_result == 1)

printf(" Student successfully added to the list! ");

else

printf(" Unable to add. Student list is full! ");

break;

case 'd': display(); break;

case 'q': break;

default: printf("%c is invalid input! ",c);

}

}

// Q1 : addSort (30 points)

// This function is used to add a student into the list and making sure the list is

sorted at all times.

// The list has to be sorted at all times. You can add the student at the end of

the list and then sort the list.

// Sort can be implemented as a different method and call it everytime you add a

record.

// Use a temp struct (already declared) if you need to swap two structs in your

logic.

// Do not allow the student to be added to the list if it already exists in the

list. You can do that by checking student names already in the list.

// If the student already exists then return 0 without adding it to the list. If

the student does not exist in the list then go on to add the student at the end of

the list, sort the list and return 1.

// If student list is full, then do not add new student to the list and return 2.

// NOTE: Notice how return type of addSort() is checked in case 'a' of

executeAction()

// NOTE: You must convert the string 'schoolYear_input' to an enum type and store

it in the list because the school year has enum type (not string type).

// The list should be case sensitive. For instance, 'Roger' and 'roger' should be

considered two different names.

// Hint: 'count' holds the number of students currently in the list

int addSort(char* studentName_input, char* major_input, char* schoolYear_input,

unsigned int studentID_input)

{

struct studentRecord studentTemp; // needed for swapping structs. Not

absolutely necessary to use.

return 0; // edit this line as needed

}

// Q2 : display (10 points)

// This function displays the student list with the details (struct elements) of

each student.

// Parse through the list and print the student details one after the other. See

expected output screenshots in question pdf file.

// NOTE: School year is stored in the struct as enum type. You need to display

'freshman','sophomore', 'junior' or 'senior'

void display()

{

}

// save() is called at the end of main()

// This function saves the array of structures to file. It is already implemented.

// You should read and understand how this code works. It will help you with

'load()' function.

// save() is called at end of main() to save the student list to a file.

// The file is saved at the same place as your C file. For VS, the default

directory looks like this:

// C:\Users\\Documents\Visual Studio 2017\Projects\Project1\Project1

// You can simply delete the file to 'reset the list' or to avoid loading from it.

void save(char* fileName)

{

FILE* file;

int i, schoolYearValue=0;

file = fopen(fileName, "wb"); // open file for writing

fwrite(&count, sizeof(count), 1, file); // First, store the number of

students in the list

// Parse the list and write student record to file

//

for (i = 0; i

{

fwrite(list[i].studentName, sizeof(list[i].studentName), 1, file);

fwrite(list[i].major, sizeof(list[i].major), 1, file);

// convert enum to a number for storing

if (list[i].schoolYear == freshman)

schoolYearValue = 0; // 0 for freshman

else if (list[i].schoolYear == sophomore)

schoolYearValue = 1; // 1 for sophomore

else if (list[i].schoolYear == junior)

schoolYearValue = 2; // 2 for junior

else

schoolYearValue = 3; // 3 for senior

fwrite(&schoolYearValue, sizeof(schoolYearValue), 1, file);

fwrite(&list[i].studentID, sizeof(list[i].studentID), 1, file);

}

fclose(file); // close the file after writing

}

// Q3: load (10 points)

// This function is called in the beginning of main().

// This function reads the student list from the saved file and builds the array of

structures 'list'.

// In the first run of the program, there will be no saved file because save() is

called at the end of program.

// So at the begining of this function, write code to open the file and check if it

exists. If file does not exist, then return from the function.

// (See expected output of add() in homework question file. It displays

"Student_List.txt not found" because the file did not exist initially.)

// If the file exists, then parse the student list to read the student details from

the file.

// Use the save function given above as an example of how to write this function.

Notice the order in which the struct elements are saved in save()

// You need to use the same order to read the list back.

// NOTE: The saved file is not exactly readable because all elements of the struct

are not string or char type.

// So you need to implement load() similar to how save() is implemented. Only

then the 'list' will be loaded correctly.

// You can simply delete the file to 'reset the list' or to avoid loading

from it.

void load(char* fileName)

{

}

Student_list.txt not found. Enter your selection: a: add a new student d: display student list q: quit Enter student name: Eden Hazard Enter major: Computer Science, Enter whether students is 'freshman' or 'sophomore' or 'junior' or 'senior': junior Please student ID number: 101 Student successfully added to the list! Enter your selection: a: add a new student d: display student list q: quit (after adding and sorting 3 student details) Enter your selection: a: add a new student d: display student list q: quit Enter student name: William Borges Enter major: Software Engineering, Enter whether students is 'freshman' or 'sophomore' or 'junior' or 'senior': senior Please student ID number: 202, Student successfully added to the list! Enter your selection: a: add a new student d: display student list q: quit Enter student name: David Luiz Enter major: Computer Science Enter whether students is 'freshman' or 'sophomore' or 'junior' or 'senior': freshman Please student ID number: 155 Student successfully added to the list! Enter your selection: a: add a new student d: display student list q: quit display: Enter your selection: a: add a new student d: display student list q: quit d Student name: David Luiz Major: Computer Science School year: freshman Student ID: 155 Student name: Eden Hazard Major: Computer Science School year: junior Student ID: 101 Student name: William Borges Major: Software Engineering School year: senior Student ID: 202 Enter your selection: a: add a new student d: display student list q: quit The 3 students seen in display() output above and they are sorted. Use 'd' option to verify sorted result. load: Students record loaded from Student List.txt. Enter your selection: 'a: add a new student d: display_student list q: quit d Student name: David Luiz Major: Computer Science School year: freshman, Student ID: 155 Student name: Eden Hazard Major: Computer Science school year: junior Student ID: 101 Student name: William Borges Major: Software Engineering School year: senior Student ID: 202 Enter your selection: a: add a new student d: display student list q: quit Notice the message given by load() "Students record loaded from Patient_List.txt" at the top. To verify that load() worked as expected, use 'd' display option to display loaded list

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions