Question

In: Computer Science

Write a C program that contains a structure that uses predefined types and union. • Create...

Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adult), and kindOfPerson (Either kid, student, or adult) • kids have a school field. Students have college and gpa. Adults have company and salary. • Create one non-dynamic struct with the content for a college student: "Bob", 20, K-State, 3.5 • Create one struct dynamically for a kid with: "Alison", 10, "Amanda Arnold Elementary" • Implement a function that can be used to display the contents of a single structure • Print out both structures • Free up the memory allocated before exiting

Solutions

Expert Solution

Simple structure

#include<stdio.h>

#include<string.h>

struct str1

{

char name[20];

int age;

char kind[20];

char kindOfPerson[20]

};

Non dynamic struct :

struct student

{

char name[20];

int age;

char college[30];

double salary;

};

int main()

{

struct student record = {0}; //initializing null

strcpy(record.name,"Bob");

record.age=20;

strcpy(record.college,"K-State");

record.salary=3.5;

printf(" Name is: %d \n", record.name);

printf(" Age is: %d \n", record.age);

printf(" College is: %d \n", record.college);

printf(" Salary is: %d \n", record.salary);

}

Dynamic structure:

#include<stdio.h>

#include<stdlib.h>

struct kid
{
   char name[20];
   int age;
   char school[30];
};

int main()
{
   struct kid *ptr;
   int i, noOfRecords;
   printf("Enter number of records: ");
   scanf("%d", &noOfRecords);

   // Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.
   ptr = (struct kid*) malloc (noOfRecords * sizeof(struct course));

   for(i = 0; i < noOfRecords; ++i)
   {
       printf("Enter name,age and school of the kid respectively:\n");
       scanf("%s %d %s", &(ptr+i)->name, &(ptr+i)->age, &(ptr+i)->school);
   }

   printf("Displaying Information:\n");

   for(i = 0; i < noOfRecords ; ++i)
       printf("%s\t%d\n%d\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->school);

   return 0;
}

Implementing function

For ex if we want to display the contents of structure student then we can write a function as shown below :

void display(struct student stu) // in arguments the structure name is passed

{

//print details

}


Related Solutions

write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Create a structure In C program named Student with the following components and appropriate data types:...
Create a structure In C program named Student with the following components and appropriate data types: Name, ID, CGPA i. Create an Array of Students of size three and take user input to fill the array. ii. Now find the student with the least CGPA and display his or hers Name, ID and CGPA.
Write a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on page 108.  Use the format specified earlier (initial block of comments with TCC logo, name, etc) Display instructions so that the user understands the purpose of the program and what to enter. Display the results in increasing (non-decreasing) order. Run the program for the following 6 test cases.Turn in a printout of the program and printouts of the 6 test cases.(The result should...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts...
Create a nested structure in C (one structure that contains another) and print out all the...
Create a nested structure in C (one structure that contains another) and print out all the fields in both structures. The main structure should have fields for: movie name and the year it was released. The extended structure should include the original structure as well as fields for: Lead actor, genre and runtime.
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
C++ C++ Write a definition for a structure type for records for CDs. The record contains...
C++ C++ Write a definition for a structure type for records for CDs. The record contains price and total number of songs, . Part of the problem is appropriate choices of type and member names.Then declare two variables of this structure. Write a function called GetData use the structure as function argument to ask the user to input the value from the keyboard for structure variables.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT