Download presentation
Presentation is loading. Please wait.
1
1 Lecture 32 Handling Selected Topics Again!! Structs and Arrays of Structs Special Lectures
2
2 Motivation l We may have different types of data about a single item of interest l For example, a cat’s color is a string, its age in years is an integer and its weight is a floating point number l A student’s name is a string, SS# is a long integer and GPA is a float value l To collect different types of data about an item of interest, we use “struct” data type in C++
3
3 Struct Declaration Syntax l struct name { l member1; l member2; l. l }; l Variables of this type declared later
4
4 Struct Examples l Let us declare some example structs l struct cat l struct student l struct employee l (In employee struct, we can define name, ID number and date of hiring as members)
5
5 Using Structs in Programs l Declare two cats named Kitty and Mely. Kitty is of brown color and weighs 19.2 lbs and is 4 years old. Mely is a 6 years old white cat weighing 23.5 lbs l If the difference between the weights of these two cats is less than three times the difference between their ages, the program should display the message “Older one is under-weight”
6
6 Arrays of Structs: A Program l Arrays are convenient for bringing huge amounts of data under a common name l For example, a building manager maintains a list of residents in a computer program. There are a total of 150 apartments in the building. The manager has to maintain the name of the tenant, tenant’s home phone number, emergency phone number and the security code assigned to the tenant for opening the main entrance door of the building
7
7 Practice Program l Write a program that declares necessary data structures to hold all this information about residents of the 150 apartments. Apartments are numbered 1 through 150 l Write a function to check if a new code number being allocated to a tenant is not already allocated l Write a function to count how many apartments are empty (Empty apartments have the security code reduced to 0)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.