More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.

Slides:



Advertisements
Similar presentations
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Advertisements

Structure.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 C++ Syntax and Semantics The Development Process.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Class 7.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
True or false A variable of type char can hold the value 301. ( F )
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Data types and variables
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Science II Exam I Review Monday, February 6, 2006.
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
Structures EE2372 Software Design I Dr. Gerardo Rosiles.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
Review 1 List Data Structure List operations List Implementation Array Linked List.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 12 Classes and Abstraction
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
CO1401 Program Design and Implementation
Structured Data (Lecture 07)
Basic Elements of C++.
New Structure Recall “average.cpp” program
C Basics.
DATA HANDLING.
Basic Elements of C++ Chapter 2.
Structures Lesson xx In this module, we’ll introduce you to structures.
Heterogeneous aggregate datatypes
Returning Structures Lesson xx
Chapter 10: Records (structs)
Wednesday 09/23/13.
CS150 Introduction to Computer Science 1
Java Programming Language
Variables and Constants
Presentation transcript:

More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that it can hold o Ex. char variables can hold only characters defined by the ASCII or extended ASCII code sets (i.e. 0 to 255) o Ex. short variables can hold only values from -32,768 and 32,767 o Ex. bool variables can hold only true or false in counterpart, has specific values or range of values that it cannot hold o int cannot hold fractional numbers o bool cannot hold characters or floating point are subject to being involved in specific types of operations o Ex. int, float double may be manipulated with the arithmetic and/or logical operators o int can be manipulated with % (modulus) but float/double cannot

Primitive Data Types Primitive data types are those data types which are defined as part of the core C++ language: short it, int, long int, double, float, bool, char, etc.

Abstract Data Types Abstract data types are data types that are created by the programmer and which are composed of one or more primitive data types or other previously defined abstract data types Programmer defines: Acceptable values Operations that can be performed We will focus on an abstract data type that is just used to group related data.

Structures (struct) A programmer can define a new data type using a ‘struct’ declaration. A struct creates a relationship between variables and has the following characteristics: a name which is the name of the abstract data type members which are data types within the ‘struct’ Syntax: struct struct_name { data_type member_name1; data_type member_name2; … };//Note the ; at the end of the definition

Example Let’s suppose we have a marathon race and are recording participant information in a computer. We might want the following information about each participant. struct participant { int id_number; char l_name[12]; char f_name[12]; int age; char sex; bool finish; float time; }; We could declare multiple variables to be of the defined type ‘participant’ participant master_list[200];//Array of all registered participants participant over_50_male_winner, over_50_female_winner, overall_winner, under_12_winner;

Accessing Structure Members To access the individual members of a structure we use what is called the ‘.’ (dot) operator. This operator allows us to use the members of a struct variable just like any other variable. Syntax: structure_variable_name.member_name1 Examples: over_50_female_winner.age = 54; master_list[i].sex = ‘F’; master_list[7].f_name = “Sharon”; if (master_list[j].finish) PrintCertificate(master_list[j]); // This is a pass by value

Initialization Like arrays, structs can be initialized with a list of values. Not all members of a structure have to be initialized in the list but you cannot ‘skip’ a member. Syntax: struct_name var_name = {val_mem1, val_mem2, …); Example: participant youngest = {12300, “Smith”,”Annie”, 10}; Member fields sex, finish, Time are left uninitialized. The following statement would not be legal. participant youngest = {12300,,,10};

Things to Note About Structures When checking for equality, like arrays that have to be compared element by element, structures must be compared member by member. Arrays of structures are declared/defined just like any other array (Ex. participant master_list[200];) It is possible to have nested structures, that is, a structure as a member of another structure. The definition of any member must precede that of the structure of which it is a member. Structures may be passed as arguments to functions

Structures as Function Arguments Unlike arrays, structures are normally passed by value just like most other variables. This means a copy of the values of the structure members is being passed not the address of the structure variable. HOWEVER – if you have defined a large structure (perhaps it has a member array or just many members) then the performance of your program will suffer if the function is called many times. In such cases, it is desirable to pass the address of the structure. If the function should not be able to modify the contents of the structure include a const specification in the definition and prototype.

Returning Structures from Functions Structures types may be used as the return type for functions. In the source code file, the structure type definition must be placed above any function utilizing it as a return type. struct meeting { int room_number; int month; int day; int hour; int minutes; float length; }; meeting GetMeeting( ) { meeting new_meeting; cout << “Enter desired month: “; cin >> new_meeting.month; cout << “\nEnter desired day: “; cin >> new_meeting.day; cout << “\nEnter the desired hour (0 – 23): “; cin << “\nEnter the desired minutes (0-59): “; cout << “\nEnter the length in hours (ex. 1.5): “; cin >> new_meeting.length; return new_meeting; }

Calling a Function Returning a Structure int main() { meeting new_session; int room_no; new_session = GetMeeting(); room_no = FindAvailableRoom(new_session); new_session.room_number = room_no; cout << “Your meeting is scheduled to be held in room “ << room_no; … } Note: Using a structure return type for a function allows the function to return more than one value by packaging those values into the members of the returned structure.