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 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...
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...
write a c program of an structure based on rings(jewelry)
write a c program of an structure based on rings(jewelry)
write a python program, please. Create a program that uses the Gettysburg Address as input and...
write a python program, please. Create a program that uses the Gettysburg Address as input and outputs a list of tuples for every two words. For example: [('Four', 'score'),('and', 'seven'), ...].
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT