// You are given a partially completed program that creates a roster of students
ID: 3670424 • Letter: #
Question
// You are given a partially completed program that creates a roster of students for a class. // Each student has the corresponding information: grade in the class and education level. // 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. // If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases. // // You are to assume that all input is valid: // Valid first name: String containing alphabetical letters beginning with a capital letter // Valid last name: String containing alphabetical letters beginning with a capital letter // Valid grade input: A+, A, A-, B+, B, B-, ... // Valid education level input : f, so, j, or s. // All input will be a valid length and no more than the allowed number of students will be added to the list #include <stdio.h> #include <string.h> #include <ctype.h> #pragma warning(disable: 4996) typedef enum { freshman = 0, sophomore, junior, senior } education; // enumeration type education level struct student { char firstName[100]; char lastName[100]; char grade[30]; education level; }; int count = 0; // the amount of students currently stored in the list (initialized at 0) struct student list[30]; // initialize list of students // forward declaration of functions void flush(); void branching(char); void read(); void add(char*, char*, char*, char*, struct student*); void display(); void save(char* fileName); void load(char* fileName); int main() { load("Student_List.txt"); // load list of students from file char ch = 'i'; printf("Student Roster: "); do { printf("Please enter your selection: "); printf(" a: add a new student "); printf(" d: display students "); printf(" q: quit and save your list "); ch = tolower(getchar()); flush(); branching(ch); } while (ch != 'q'); save("Student_List.txt"); // save list of students to file return 0; } // consume leftover ' ' characters void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case 'a': read(); break; case 'd': display(); break; case 'q': break; default: printf("Invalid input! "); } } // This function is already implemented for you. It prompts for and stores a student along with their grade and education level. // It then calls the add() function (which is to be implemented) sending over those parameters (along with list). void read() { char student_firstName[100]; char student_lastName[100]; char student_grade[30]; char student_level[100]; printf(" Enter the student's first name: "); fgets(student_firstName, sizeof(student_firstName), stdin); printf(" Enter the student's last name: "); fgets(student_lastName, sizeof(student_lastName), stdin); printf(" Enter the student's grade (A+,A,A-,...): "); fgets(student_grade, sizeof(student_grade), stdin); printf(" Enter the student's education level (f/so/j/s): "); fgets(student_level, sizeof(student_level), stdin); // discard ' ' chars attached to input; NOTE: If you are using GCC, you may need to comment out these 4 lines student_firstName[strlen(student_firstName) - 1] = ''; student_lastName[strlen(student_lastName) - 1] = ''; student_grade[strlen(student_grade) - 1] = ''; student_level[strlen(student_level) - 1] = ''; add(student_firstName, student_lastName, student_grade, student_level, list); printf(" "); // newline for formatting } // Q1: add // This function is used to insert a new student into the roster. // Your list should be sorted alphabetically by last name, so you need to search for the correct index to add into your list. // If a student already exists with the same last name, then you will need to sort them by their first names. // Do not allow for the same student to be added to the list multiple times. (same first name and same last name). // // NOTE: You must convert the char pointer "student_level" to an enum type and store it in the list. This will be tested. // (You must store all of the required information correctly to pass all of the test cases) // NOTE: You should not allow for the same student to be added twice, you will lose points if you do not account for this. // (That means that students on the list are allowed to have the same first names OR the same last names, but not both) // Again, you will lose points if you do not account for these cases. // // It may be helpful to print "Student added!" or "This student is already on the list!" at the correct places for debugging purposes. // // You are not required to use pointer operations for your list but you may do so if you'd like. // 'list' is passed to this function for testing purposes only, it is global void add(char* student_firstname, char* student_lastname, char* student_grade, char* student_level, struct student* list) { } // Traverse your list and print the students the required format; see word document for output format. // Your output must match the expected output format or you risk losing points. // If the list is empty, print: "There are no students on this roster! " // (hint: Use can use your global integer value "count" to see how many students are in your list) void display() { } // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; file = fopen(fileName, "wb"); if (file == NULL) { return; } else { fwrite(&count, sizeof(count), 1, file); for (int i = 0; i < count; i++) { fwrite(list[i].firstName, sizeof(list[i].firstName), 1, file); fwrite(list[i].lastName, sizeof(list[i].lastName), 1, file); fwrite(list[i].grade, sizeof(list[i].grade), 1, file); fwrite(&list[i].level, sizeof(list[i].level), 1, file); } fclose(file); } } // This function loads the array of structures from file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void load(char* fileName) { FILE* file; file = fopen(fileName, "rb"); if (file == NULL) { return; } else { fread(&count, sizeof(count), 1, file); for (int i = 0; i < count; i++) { fread(list[i].firstName, sizeof(list[i].firstName), 1, file); fread(list[i].lastName, sizeof(list[i].lastName), 1, file); fread(list[i].grade, sizeof(list[i].grade), 1, file); fread(&list[i].level, sizeof(list[i].level), 1, file); } fclose(file); } }
Explanation / Answer
// READ BEFORE YOU START:
// You are given a partially completed program that creates a roster of students for a class.
// Each student has the corresponding information: grade in the class and education level.
// 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.
// If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases.
//
// You are to assume that all input is valid:
// Valid first name: String containing alphabetical letters beginning with a capital letter
// Valid last name: String containing alphabetical letters beginning with a capital letter
// Valid grade input: A+, A, A-, B+, B, B-, ...
// Valid education level input : f, so, j, or s.
// All input will be a valid length and no more than the allowed number of students will be added to the list
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
typedef enum { freshman = 0, sophomore = 1, junior = 2, senior = 3 } education; // enumeration type education level
struct student {
char firstName[100];
char lastName[100];
char grade[30];
education level;
};
int count = 0; // the amount of students currently stored in the list (initialized at 0)
struct student list[30]; // initialize list of students
// forward declaration of functions
void flush();
void branching(char);
void read();
void add(char*, char*, char*, char*, struct student*);
void display();
void save(char* fileName);
void load(char* fileName);
int main()
{
load("Student_List.txt"); // load list of students from file
char ch = 'i';
do
{
printf("Please enter your selection: ");
printf(" a: add a new student ");
printf(" d: display students ");
printf(" q: quit and save your list ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
save("Student_List.txt"); // save list of students to file
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a': read(); break;
case 'd': display(); break;
case 'q': break;
default: printf("Invalid input! ");
}
}
// This function is already implemented for you. It prompts for and stores a student along with their grade and education level.
// It then calls the add() function (which is to be implemented) sending over those parameters (along with list).
void read()
{
char student_firstName[100];
char student_lastName[100];
char student_grade[30];
char student_level[100];
printf(" Enter the student's first name: ");
fgets(student_firstName, sizeof(student_firstName), stdin);
printf(" Enter the student's last name: ");
fgets(student_lastName, sizeof(student_lastName), stdin);
printf(" Enter the student's grade (A+,A,A-,...): ");
fgets(student_grade, sizeof(student_grade), stdin);
printf(" Enter the student's education level (f/so/j/s): ");
fgets(student_level, sizeof(student_level), stdin);
// discard ' ' chars attached to input; NOTE: If you are using GCC, you may need to comment out these 4 lines
student_firstName[strlen(student_firstName) - 1] = '';
student_lastName[strlen(student_lastName) - 1] = '';
student_grade[strlen(student_grade) - 1] = '';
student_level[strlen(student_level) - 1] = '';
add(student_firstName, student_lastName, student_grade, student_level, list);
printf(" "); // newline for formatting
}
// Q1: add (40)
// This function is used to insert a new student into the roster.
// Your list should be sorted alphabetically by last name, so you need to search for the correct index to add into your list.
// If a student already exists with the same last name, then you will need to sort them by their first names.
// Do not allow for the same student to be added to the list multiple times. (same first name and same last name).
//
// NOTE: You must convert the char pointer "student_level" to an enum type and store it in the list. This will be tested.
// (You must store all of the required information correctly to pass all of the test cases)
// NOTE: You should not allow for the same student to be added twice, you will lose points if you do not account for this.
// (That means that students on the list are allowed to have the same first names OR the same last names, but not both)
// Again, you will lose points if you do not account for these cases.
//
// It may be helpful to print "Student added!" or "This student is already on the list!" at the correct places for debugging purposes.
//
// You are not required to use pointer operations for your list but you may do so if you'd like.
// 'list' is passed to this function for testing purposes only, it is global
void add(char* student_firstname, char* student_lastname, char* student_grade, char* student_level, struct student* list)
{
char *currLastName = student_lastname; //create storage for student_lastname
char *currFirstName = student_firstname; //create storage for student_firstname
struct student *pointer = list; //create a pointer to list for referencing
education sLevel; // create an enum for education
int i, j; //count increments
for (i = 0; i < 30; i++) // Start of algorithm for checking for no duplicate names and adds student
{
if (((strcmp((pointer + i)->lastName, currLastName)) == 0) && ((strcmp((pointer + i)->firstName, currFirstName)) == 0)) //Checks for duplicate last name and first name
{
printf("Duplicate Student "); // Prints if a duplicate sudent was entered
break;
}
if ((*(pointer + i)->lastName) == '') // If last name is not reoccurring then add student into null slot.
{
int dup = 0; //in order to not get it to overide sophomore with senior, i created a second check
char *soph = "so"; // in order to check 'so' i needed to create a string pointer so it read more than just 's'
strcpy((pointer + i)->firstName, student_firstname); //Adds first name into the structure i
strcpy((pointer + i)->lastName, currLastName); //Adds last name into the structure i
strcpy((pointer + i)->grade, student_grade); //Adds grade into the structure i
if (*student_level == 'f') //Series of if statements to determine what to set enum as and adds to structure i
{
sLevel = freshman; // sets enum equal to value freshman
(pointer + i)->level = sLevel;
}
if (strcmp(student_level, "so") == 0) //string compare so it won't just look at the first character
{
sLevel = sophomore; // sets enum equal to value sophomore
(pointer + i)->level = sLevel;
dup++; // inc dup variable so it doesn't go into senior check
}
if (*student_level == 'j') // checks junior
{
sLevel = junior; // sets enum equal to value junior
(pointer + i)->level = sLevel;
}
if ((*student_level == 's') && (dup == 0)) //checks senior if conditions are met
{
sLevel = senior; // sets enum equal to value senior
(pointer + i)->level = sLevel;
dup++;
}
count++; //increment count because a student was added to the struct
break; //stop loop
}
}
//selection sort algorithm here
for (i = 0; i < count; i++) // First Loop
{
for (j = i + 1; j < count; j++)
{
int x = strcmp((pointer+i)->lastName, (pointer+j)->lastName); // last name comparison
int y = strcmp((pointer+i)->firstName, (pointer+j)->lastName); //first name comparison
if (x > 0)
{
struct student temp; //temporary struct for swap
temp = pointer[i]; // temp = spot 1
pointer[i] = pointer[j]; // spot 1 = spot 2
pointer[j] = temp; // spot 2 = spot 1
}
if (x == 0) // if last name is same go inside
{
if (y > 0) // if first name comes before go inside
{
struct student temp; //temporary struct for swap
temp = pointer[i]; // temp = spot 1
pointer[i] = pointer[j]; // spot 1 = spot 2
pointer[j] = temp; // spot 2 = spot 1
}
}
}
}
}
// Q2: display (10)
// Traverse your list and print the students the required format; see word document for output format.
// Your output must match the expected output format or you risk losing points.
// If the list is empty, print: "There are no students on this roster! "
// (hint: Use can use your global integer value "count" to see how many students are in your list)
void display()
{
if (count == 0)
{
printf(" There are no students in this roster! ");
}
int i;
for (i = 0; i < count; i++) // loop to display all struct[i] spots
{
printf("%s, %s ", &(list[i].lastName), &(list[i].firstName)); //Prints lastname, firstname
if ((list[i].level) == freshman) //series of if statements in order to determine grade
{
printf("Education Level: freshman ");
}
if ((list[i].level) == sophomore)
{
printf("Education Level: sophomore ");
}
if ((list[i].level) == junior)
{
printf("Education Level: junior ");
}
if ((list[i].level) == senior)
{
printf("Education Level: senior ");
}
printf("Grade: %s ", &(list[i].grade)); //prints grade
}
}
// This function saves the array of structures to file. It is already implemented for you.
// You should understand how this code works so that you know how to use it for future assignments.
void save(char* fileName)
{
FILE* file;
file = fopen(fileName, "wb");
if (file == NULL)
{
return;
}
else
{
int i;
fwrite(&count, sizeof(count), 1, file);
for (i = 0; i < count; i++)
{
fwrite(list[i].firstName, sizeof(list[i].firstName), 1, file);
fwrite(list[i].lastName, sizeof(list[i].lastName), 1, file);
fwrite(list[i].grade, sizeof(list[i].grade), 1, file);
fwrite(&list[i].level, sizeof(list[i].level), 1, file);
}
fclose(file);
}
}
// This function loads the array of structures from file. It is already implemented for you.
// You should understand how this code works so that you know how to use it for future assignments.
void load(char* fileName)
{
FILE* file;
file = fopen(fileName, "rb");
if (file == NULL)
{
return;
}
else
{
int i;
fread(&count, sizeof(count), 1, file);
for (i = 0; i < count; i++)
{
fread(list[i].firstName, sizeof(list[i].firstName), 1, file);
fread(list[i].lastName, sizeof(list[i].lastName), 1, file);
fread(list[i].grade, sizeof(list[i].grade), 1, file);
fread(&list[i].level, sizeof(list[i].level), 1, file);
}
fclose(file);
}
}
output
Please enter your selection:
a: add a new student
d: display students
q: quit and save your list
a
Enter the student's first name:
ram
Enter the student's last name:
saran
Enter the student's grade (A+,A,A-,...):
A+
Enter the student's last name:
saran
Enter the student's grade (A+,A,A-,...):
A+
Enter the student's education level (f/so/j/s):
f
Please enter your selection:
a: add a new student
d: display students
q: quit and save your list
q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.