130 likes | 203 Views
Lab Session-12 CSIT121 Fall 2004. Structures and Their Usage Passing Struct Variables to Functions. Structures and Their Usage. Arrays can only store data items of same data type under a common name
E N D
Lab Session-12 CSIT121 Fall 2004 • Structures and Their Usage • Passing Struct Variables to Functions
Structures and Their Usage • Arrays can only store data items of same data type under a common name • If we want to store student’s name and SS# together under a common name, it is not possible with arrays • We need to group together data items that are logically related to each other but these are of different data types
Structures and Their Usage • Examples of logical groups • Passenger Name, Flight Number, Seat Number and Status • Book Name, Author, Year published, Catalogue Number, Status • Student Name, SS#, Current courses
Structures and Their Usage • C++ provides struct data type • Using struct data type, we can collect many data items with different data types together under a same name • For example, if we wish to implement flight reservation system, we will need something similar to the following:
Structures and Their Usage • Struct flight • { • int SNo; • char name[40]; • char aisle; • int seat; • char status; • }
Structures and Their Usage • We have defined a data type by the name flight. • We can now declare variables of this data type • Example: flight AA474; • we can use AA474 in our program now
Structures and Their Usage • The data items grouped together under flight data type are called members of the structure. • Accessing members is done via dot notation • For Example: • AA474.seat=24; AA474.aisle=‘F’; • AA474.status=‘K’; (K means OK)
Structures and Their Usage • The members can be used just like any other variables are used • Example: • switch(AA474.status) • { • case ‘K’:cout<<“Confirmed\n”; break; • case ‘W’:cout<<“Waiting\n”;break; • case ‘C’:cout<<“Canceled”; break; • default: cout<<“Error in flight information\n”; break; • }
Passing Struct Variables to Functions • Struct variables can be assigned as below • flight AA475; AA475=AA474; • We can pass struct variables as arguments to functions. See example prototypes • By Value void print_info(flight); • By Reference void change_info(flight&); • Better pass by reference and add const if you wish to protect it
Lab Exercise (Demo Due 12/7) • Define a struct time that can hold hours, minutes and seconds as well as a char flag for am or PM. • struct time • { int hours; • int minutes; • int seconds; • char ampm; • } • time now; • now.ampm=‘a’;
Lab Exercise • Develop a program that defines a local struct variable and calls a function to read from the keyboard and store current time in its struct variable • A second function simulates running clock. This function runs an infinite while loop so it should be called at the end. Here is an algorithm for this function
Lab Exercise • Display Time in standard notation. For example, 10:02:00 AM • Wait for one second (One second delay may be achieved by a counted loop in which the counter counts from 0 to 500 million) • Increment seconds, then check if there is a need to increment minutes and hours and am/pm or vice versa • Clear the screen
Demo Data • Demo Data is as follows: • Hours: 11 • Minutes: 59 • Seconds: 0 • AM/PM: A • The program should display: • 11:59:00 AM • After one minute: • 12:00:00 PM