In: Computer Science
Please answer QC,D,E,F using the code provided at the end please.
A. Write C code to define a structure called date that has three fields day, month, year, all of which are ints.
B. Write C code to define a structure called person that has fields for name (50 char array), dateOfBirth (date struct from Q1), address (200 char array), phoneNum (20 char array), and ID (int).
C. Write a C program that uses the person structure from Q2, and declares a variable of this type. Your program should then read information from the keyboard to fill one of these structures with information. Use fgets to read entire lines of text for the name and address rather than scanf.
D. Create another variable that is a pointer to the person structure defined in Q2. Set this variable to point at the variable declared in Q3, and then use the pointer to print the structure to the screen (with appropriate text).
E. Show how a typedef statement can be used to declare a new data type – to demonstrate this create a new type called Person which is the structure declared previously.
F. . Write C code to declare an array of 50 pointers to Persons. Using a loop, use malloc to dynamically allocate memory for each of these structures. There is no need to put any values into the structures. Use a second loop to then deallocate each structure.
__________________________________________________________________________________________________________________________
#include<stdio.h>
#include<stdlib.h>
#define NAME_SIZE 50
#define address_SIZE 200
#define phone_SIZE 20
// Part A defining the structure
struct Date{
int day, month, year;
};
// Part B defining the Person structure
struct Person{
char name[NAME_SIZE];
struct Date dateOfBirth;
char address[address_SIZE];
int id;
char phoneNumber[phone_SIZE];
};
// Main function for the same
int main(){
// part C defining the variable for the use of above structure
struct Person p;
// Just to make the inputs go flowlessly
char newLine;
// Taking the user input
printf("Enter the person name: ");
fgets(p.name, NAME_SIZE, stdin);
printf("Enter dob in 'day month year' Format");
scanf("%d%d%d", &p.dateOfBirth.day, &p.dateOfBirth.month, &p.dateOfBirth.year);
scanf("%c", &newLine);
printf("Enter the address: ");
fgets(p.address, address_SIZE, stdin);
printf("Enter the id: ");
scanf("%d", &p.id);
scanf("%c", &newLine);
printf("Enter the phone number: ");
fgets(p.phoneNumber, phone_SIZE, stdin);
// part D defining the pointer for the person
struct Person* p2;
p2 = &p;
// Printing the Scanned values
printf("Printing the populated Person Structure\n");
printf("Name: %s\n", p2->name);
printf("DOB: %d %d %d\n", p2->dateOfBirth.day, p2->dateOfBirth.month, p2->dateOfBirth.year);
printf("Address: %s\n", p2->address);
printf("Id: %d\n", p2->id);
printf("Phone: %s\n", p2->phoneNumber);
#include<stdio.h>
#include<stdlib.h>
#define NAME_SIZE 50
#define address_SIZE 200
#define phone_SIZE 20
// Part A defining the structure
struct Date{
int day, month, year;
};
// Part B defining the Person structure
struct Person{
char name[NAME_SIZE];
struct Date dateOfBirth;
char address[address_SIZE];
int id;
char phoneNumber[phone_SIZE];
};
// E typedef to create a new type called Person which is the structure Person
typedef struct Person Person;
// Main function for the same
int main(){
// part C defining the variable for the use of above structure
struct Person p;
// Just to make the inputs go flawlessly
char newLine;
// Taking the user input
printf("Enter the person name: ");
fgets(p.name, NAME_SIZE, stdin);
printf("Enter dob in 'day month year' Format : ");
scanf("%d%d%d", &p.dateOfBirth.day, &p.dateOfBirth.month, &p.dateOfBirth.year);
scanf("%c%c", &newLine,&newLine);
printf("Enter the address: ");
fgets(p.address, address_SIZE, stdin);
printf("Enter the id: ");
scanf("%d", &p.id);
//scanf("%c", &newLine);
// phone number will not contain space , so no need for fgets
printf("Enter the phone number: ");
scanf("%s", p.phoneNumber);
//fgets(p.phoneNumber, phone_SIZE, stdin);
// part D defining the pointer for the person
struct Person* p2;
p2 = &p;
// Printing the Scanned values
printf("Printing the populated Person Structure\n");
printf("Name: %s\n", p2->name);
printf("DOB: %d %d %d\n", p2->dateOfBirth.day, p2->dateOfBirth.month, p2->dateOfBirth.year);
printf("Address: %s\n", p2->address);
printf("Id: %d\n", p2->id);
printf("Phone: %s\n", p2->phoneNumber);
// F create array of 50 Person pointers Persons
Person *Persons[50];
int i;
// loop to allocate memory for each Person in Persons
for(i=0;i<50;i++)
Persons[i] = (Person*)malloc(sizeof(Person));
// loop to deallocate memory from each Person in Persons
for(i=0;i<50;i++)
free(Persons[i]);
return 0;
}
//end of program
Output: