Question
1. Declare a student structure similar to Book1 in the previous activity. Populate the structure variables with name, NUID and DOB of a student. Complete
1. Declare a student structure similar to Book1 in the previous activity. Populate the structure variables with name, NUID and DOB of a student. Complete the printStudent() function and call that function to print the student info.
2. Declare a pointer to a student structure (similar to Book2). Populate the variables and call print function.
3. Complete the createStudent() function by implementing deep copy. Then uncomment the two lines. Compile and run your program again.
So i have completed most of the code but I'm not sure what should be done for the "TODO 4" section found in the code. Could you also check to see if I complete the rest correctly please.
#include
typedef struct { char firstName[20]; char lastName[20]; int nuid; char *birthDate; } Student;
Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str); void printStudent(Student * student);
int main(void) {
Student s1; //TODO 1: Enter student specifications and print it (similar to Book#1 in the sample program) Student Student1; strcpy( Student1.Fname, "John"); strcpy( Student1.Lname, "Smith"); Student1.ID = 87654321; Student1.DOB = (char *)malloc(sizeof(char) * 100); strcpy( Student1.DOB, "12/30/1994"); printf("Print the specifications of first student.. "); printStudent(&Student1); printf(" "); //TODO 2: Declare a student structure dynamically and enter specification and then print (similar to Book#2) Student *Student2 = (Student *)malloc(sizeof(Student) * 1); strcpy( Student2->Fname, "Jane"); strcpy( Student2->Lname, "Doe"); Student2->ID = 12345678; Student2->DOB = (char*)malloc(sizeof(char) * 100); strcpy(Student2->DOB, "01/01/1994"); printf("Print the specifications of second student.. "); printStudent(Student2); // TODO 3: Uncomment following two lines, put your name, NUID, DOB in the first line and run the program. Examine the createStudent() function
Student *me = createStudent("Lindsey", "Collins", 15031297, "08/21/1994"); printStudent(me);
//TODO 4: Declare an array of 5 student structures and use createStudent() function 5 times for 5 different students. // print all student informations using an FOR loop }
/* print student informations */ void printStudent(Student * student) { printf( "First name : %s ", Student->Fname); printf( "Last name : %s ", Student->Lname); printf( "NUID : %d ", Student->ID); printf( "Date of Birth : %s ", Student->DOB); }
/** * A "factory" function to create a new student structure initialized with the given values * Strings are deep copied. */ Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str) { Student *s = (Student *)malloc(sizeof(Student) * 1); strcpy(s->Fname, firstName); strcpy(s->Lname, lastName); s->ID = nuid; s->DOB = (char *)malloc(sizeof(char) * strlen(birthDate_str) + 1); strcpy(s->DOB, birthDate_str); return s; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started