1 / 13

Lab Session-12 CSIT121 Fall 2004

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

radley
Download Presentation

Lab Session-12 CSIT121 Fall 2004

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab Session-12 CSIT121 Fall 2004 • Structures and Their Usage • Passing Struct Variables to Functions

  2. 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

  3. 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

  4. 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:

  5. Structures and Their Usage • Struct flight • { • int SNo; • char name[40]; • char aisle; • int seat; • char status; • }

  6. 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

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

  8. 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; • }

  9. 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

  10. 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’;

  11. 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

  12. 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

  13. 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

More Related