Question

In: Computer Science

Please answer QC,D,E,F using the code provided at the end please. A. Write C code to...

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

Solutions

Expert Solution

#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:


Related Solutions

Solve d) and e). I have provided answer for c) too required in e) Winkler Furniture...
Solve d) and e). I have provided answer for c) too required in e) Winkler Furniture manufactures two different types of china cabinets: a French provincial model and a Danish Modern model. Each cabinet produced must go through three departments: carpentry, painting, and finishing. The table below contains all relevant information concerning production times per cabinet produced and production capacities for each operation per day, along with net revenue per unit produced. The firm has a contract with an Indiana...
please answer part 2 . the code for part 1 is provided at the end. Part...
please answer part 2 . the code for part 1 is provided at the end. Part 1 There is a pair of functions in stdlib.h (make sure to include it) that are used for generating pseudo-random numbers. These are "srand" and "rand". Examples are on pages 686 and 687. E.g., function "srand(3)" provides the seed value of 3 to the srand function. (3 is just a number, and probably isn't even a good number for this purpose, but that's a...
3) Perfectly competitive markets PLEASE DO NOT ANSWER A-D PLEASE ONLY ANSWER E,F,G # of Contraptions...
3) Perfectly competitive markets PLEASE DO NOT ANSWER A-D PLEASE ONLY ANSWER E,F,G # of Contraptions Total Cost 0 500 1 580 2 640 3 690 4 730 5 760 6 800 7 850 8 950 9 1200 10 2000 c) If market price equals $100, how many units should be produced? What is revenue? What is profit? Add these columns to your Table too. d) What is the fixed cost? Would the number of units produced change if the...
Given the operation times provided: JOB TIMES (minutes) A B C D E F Center 1...
Given the operation times provided: JOB TIMES (minutes) A B C D E F Center 1 20 16 43 60 35 42 Center 2 27 30 51 12 28 24 a. Develop a job sequence that minimizes idle time at the two work centers. The sequence is             (Click to select)  A-B-C-D-E-F  B-A-C-E-F-D  C-A-B-D-E-F  F-E-D-A-B-C  E-F-D-B-C-A  D-C-B-A-E-F  . b. Determine idle time of center 2, assuming no other activities are involved. Idle time             minutes
Please fill in the blanks (values of A, B, C, D, E, F, G, H, I...
Please fill in the blanks (values of A, B, C, D, E, F, G, H, I , J) for the following financial statements. The firm’s tax rate is 35.3%. Income Statement for Fiscal Year 2015 Sales 2,000 Cost of goods sold 1,500 Gross margin 500 Selling and general expenses 300 Operating income 200 Interest income 5 205 Interest expense 21 Restructuring charges 14 Income before tax 170 Income tax 60 Net income J Balance Sheet, Year 2014 and Year 2015...
PLEASE ANSWER PARTS A, B, C, D, AND E. A rocket is launched straight up with...
PLEASE ANSWER PARTS A, B, C, D, AND E. A rocket is launched straight up with a constant acceleration of 25 m/s2. Six seconds after liftoff, a bolt falls off the side of the rocket. (a) Determine the velocity of the bolt just before it falls off the side of the rocket. (b) Determine the altitude of the bolt (measured from the ground) just before it falls off the side of the rocket. (c) How long is the bolt in...
Can someone please answer these 3 by Friday? Problem 2 Let D, E, F, G, and...
Can someone please answer these 3 by Friday? Problem 2 Let D, E, F, G, and H be events such that P(D) = 0.7, P(E) = 0.6, P(F) = 0.8, P(G) = 0.9, and P(H) = 0.5. Suppose that Dc, E, Fc, Gc, and H are independent. (a) Find the probability that all of the events D, E, F, G, and H occur. (b) Find the probability that at least one of the events D, E, F, G, and H...
4 -letter words'' are formed using the letters A, B, C, D, E, F, G. How...
4 -letter words'' are formed using the letters A, B, C, D, E, F, G. How many such words are possible for each of the following conditions? (a) No condition is imposed. Your answer is : (b) No letter can be repeated in a word. Your answer is : (c) Each word must begin with the letter A. Your answer is : (d) The letter C must be at the end. Your answer is : (e) The second letter must...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what...
Consider the cross: A/a; b/b; C/c; D/d; E/e x A/a; B/b; c/c; D/d; e/e a) what proportion of the progeny will phenotypically resemble the first parent? b) what proportion of the progeny will genotypically resemble neither parent?
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT