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

1. Declare a student structure similar to Book1 in the previous activity. Popula

ID: 3811434 • Letter: 1

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 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<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
  
}

Explanation / Answer

// There were lot of mistakes which I have corrected. Also implemented TODO 4. Tested the code compeltely, it's working perfectly fine.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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) {

//TODO 1: Enter student specifications and print it (similar to Book#1 in the sample program)

Student Student1;

strcpy( Student1.firstName, "John");
strcpy( Student1.lastName, "Smith");

Student1.nuid = 87654321;

Student1.birthDate = (char *) malloc(sizeof(char) * 100);
strcpy(Student1.birthDate, "12/30/1994");

printf("Print the specifications of first student.. ");
printStudent(&Student1);

//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->firstName, "Jane");
strcpy( Student2->lastName, "Doe");

Student2->nuid = 12345678;

Student2->birthDate = (char*)malloc(sizeof(char) * 100);
strcpy(Student2->birthDate, "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
Student *studArr[5];
studArr[0] = createStudent("Zubaeyr", "Odin", 100431, "31/01/1996");
studArr[1] = createStudent("Prameela", "Meghavath", 100432, "12/08/1994");
studArr[2] = createStudent("Ayesha", "Nelofer", 471433, "11/08/1993");
studArr[3] = createStudent("John", "Carter", 290443, "22/01/1992");
studArr[4] = createStudent("Azhar", "Odin", 123511, "13/03/1991");

int i = 0;
printf("Print the specifications of five randomly created student.. ");
for (; i<5; i++) {
     printStudent(studArr[i]);
}

}

/* print student informations */
void printStudent(Student * student) {
    printf( "First name : %s ", student->firstName);
    printf( "Last name : %s ", student->lastName);
    printf( "NUID : %d ", student->nuid);
    printf( "Date of Birth : %s ", student->birthDate);
}

/**
* A "factory" function to create a new student structure initialized with the given values
* Strings are deep copied.
*/


Student * createStudent(const char * Fname, const char *Lname, int ID, const char * birthDate_str) {
    Student *s = (Student *)malloc(sizeof(Student) * 1);
    strcpy(s->firstName, Fname);
    strcpy(s->lastName, Lname);
    s->nuid = ID;
  
    s->birthDate = (char *)malloc(sizeof(char) * strlen(birthDate_str) + 1);
    strcpy(s->birthDate, birthDate_str);
  
    return s;

}