Activity 1: A Book Structure This activity will familiarize you with a completed
ID: 3853788 • Letter: A
Question
Activity 1: A Book Structure
This activity will familiarize you with a completed program in which a C structure has been declared to represent a Book. Several functions have been implemented to assist in the construction and printing of this structure. The createBook() function takes the appropriate parameters, allocates memory to hold a new Book and sets each field of the Book appropriately. In the case of (char *) strings, malloc is used to create enough space and the string is then copied. Ultimately, a pointer to the new Book structure is returned. There is also a print function that takes a Book (by reference) and prints it out to the standard output.
1.Download the bookDemo.c source file
2.Examine the syntax and program structure and observe how each function works as well as how
they are used.
3.Examine how three book structures are declared and created in main() program
4.Compile and run the program. Refer back to this program in Activity 2 as needed.
bookDemo.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char title[50];
char author[50];
char *subject;
int book_id;
}Books;
/* function declaration */
void printBook(Books *book);
Books *createBook(char *book_title, char *book_author, char *book_subject, int ID);
int main(void){
/*---------------------------------------------*/
/* First Book */
/*---------------------------------------------*/
Books Book1; /* Declare Book1 of type Book */
/*Book#1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Dennis Ritchie");
Book1.subject = (char *) malloc(sizeof(char) * 100);
strcpy( Book1.subject, "C Programming Tutu
rial");
Book1.book_id = 6495407;
/* print Book1 info by passing address of Book1 */
printf("Print the specifications of first Book.. ");
printBook( &Book1 );
printf(" ");
/*---------------------------------------------*/
/* Second Book */
/*---------------------------------------------*/
Books *Book2 = (Books *) malloc(sizeof(Books) * 1); /* Declare Book2 of type Book (dynamically) */
/* Book#2 specification */
strcpy( Book2->title, "C++ Programming");
strcpy( Book2->author, "Bjarne Stroustrupi");
Book2->subject = (char*)malloc(sizeof(char) * 100);
strcpy( Book2->subject, "C++ Programming Tuturial");
Book2->book_id = 6495700;
/* print Book2 info by passing address of Book2 */
printf("Print the specifications of Second Book.. ");
printBook(Book2 );
/*---------------------------------------------*/
/* Third Book */
/*---------------------------------------------*/
Books *Book3 = createBook("Java Programming", "James Gosling", "Java Tuturial", 6478543);
printf("Print the specifications of Third Book.. ");
printBook(Book3);
return 0;
}
/* Print book specifications function */
void printBook(Books *book) {
printf( "Book title : %s ", book->title);
printf( "Book author : %s ", book->author);
printf( "Book subject : %s ", book->subject);
printf( "Book book_id : %d ", book->book_id);
}
/* A function to create a new Book structure initialized with the given values. */
Books *createBook(char *book_title, char *book_author, char *book_subject, int ID){
/* Declare a structure */
Books *b = (Books *)malloc(sizeof(Books)*1);
strcpy(b->title, book_title);
strcpy(b->author, book_author);
b->subject = (char *) malloc(sizeof(char) * strlen(book_subject)+1);
strcpy(b->subject, book_subject);
b->book_id = ID;
return b;
}
Activity 2: Completing the Student Program
In this activity, you will complete the studentDemo.c program. This is exactly similar to the bookDemo.c program. You need to read the instructions and add your logic below "TODO" section of your program. You need to compile and run your program after adding logic for each "TODO". Show your output to an instructor.
Instructions
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.2
3.Complete the createStudent() function by implementing deep copy. Then uncomment the two lines. Compile and run your program again.
studentDemo.c
#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)
//TODO 2: Declare a student structure dynamically and enter specification and then print (similar to Book#2)
// 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("Joe", "Smith", 140602, "07/30/1980");
//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) {
}
/**
* A "factory" function to create a new student structure initialized with the given values
* Strings are deep copied.
*/
Student * createStudent(const char * first_Name, const char *last_Name, int NU_ID, const char * birthDate_str) {
}
Explanation / Answer
studentDemo.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct {
char *firstName;
char *lastName;
int nuid;
struct tm birthDate;
} Student;
Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str);
void printStudent(Student * student);
int main(void) {
Student *me = createStudent("Joe", "Smith", 140602, "07/30/1980");
printStudent(me);
/*Student *students[5];
students[0]= createStudent("smith", "Feghn", 140602, "07/30/1981");
students[1]= createStudent("joe", "Ffhman", 140602, "07/30/1970");
students[2]= createStudent("uty", "Feghan", 140602, "07/38/1980");
students[3]= createStudent("ijjh", "Fefgan", 140602, "07/30/1980");
students[4]= createStudent("ghjh", "ghan", 140602, "07/30/1980");
for(int i=0;i<5;i++)
printStudent(students[i]);*/
return 0;
}
/**
* A "factory" function to create a new student structure initialized with the given values
* Strings are deep copied.
* You must use the "strptime" function defined in the <time.h> file to copy the "birthDate_str" from the argument to the "birthDate" struct component of the Student struct
*/
Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str) {
const char delim[2] = "/";
char *token;
Student * student = (Student *) malloc(sizeof(Student) * 1);
student->firstName =(char*)malloc(sizeof(char)*strlen(firstName));
strcpy(student->firstName,firstName);
student->lastName = (char*)malloc(sizeof(char)*strlen(lastName));
strcpy(student->lastName,lastName);
student->nuid =nuid;
token = strtok(birthDate_str,delim);
student->birthDate.tm_mday=atoi(token);
token = strtok(NULL, delim);
student->birthDate.tm_mon=atoi(token)-1;
token = strtok(NULL, delim);
student->birthDate.tm_year=atoi(token)-1900; return student;
}
/*
* A factory function to display a student first name, last name, nuid and birth date
*/
void printStudent(Student * student) {
char *tmp = (char *) malloc(sizeof(char) * 2000);
//format the student into the temporary string
sprintf(tmp, "%s, %s (%08d, %d-%d-%d) ", student->lastName, student->firstName, student->nuid, (student->birthDate.tm_year+1900), (student->birthDate.tm_mon+1), student->birthDate.tm_mday);
//create the final string that is the exact size needed
char *result = (char *) malloc(sizeof(char) * (strlen(tmp)+1));
strcpy(result, tmp);
//free up the temporary string
free(tmp);
//return the result
printf("%s ",result);
free(result);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.