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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

UNIT IV.
C Language.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Introduction to Assembly language
Structure.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise.
1 Pre-Exam Class CSIT121 Fall 2000 Exam-II (Final Examination) TUESDAY DECEMBER 19th 8:30AM.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-6 CSIT221 Fall 2002 A Note About Destructors Using Pointers Implementing Stacks.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.
1 Session-19 CSIT 121 Spring 2006 Count controlled loops can be implemented with for statement This statement becomes the loop header for (lcv=initial.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
Computer Science II Exam I Review Monday, February 6, 2006.
1 Lab Session-11 CSIT 121 Fall 2003 Using arrays in functions Programming Exercise.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
1 Lab Session-XII CSIT121 Spring 2002 b Arrays and Their Usage b Finding the largest element b Lab Exercise 12-A b Lab Demo (12-B) b Practice Problems.
1 Lab Session-IX CSIT121 Fall 2000 w Arrays and Their Usage w Examples and Lab Exercises w Passing Arrays to Functions w Examples and Exercises w Sorting.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Advanced Data types and Sorting
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Learners Support Publications Classes and Objects.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.
1 CS161 Introduction to Computer Science Topic #15.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Structs versus Classes
New Structure Recall “average.cpp” program
Student Book An Introduction
CS1100 Computational Engineering
Structure.
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