CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

CISC Data Structures Ben Perry University of Delaware Summer 2011.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 Chapter Nine Characters and Strings. 2 Text Data These days, computers work less with numeric data than with text data To unlock the full power of text.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
1) Enumerated types 2) Records and structures 3) Accessing fields in records/structures 4) Operations on records/structures 5) Variant records 6) Dynamic.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
Data Types.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.
BİL527 – Bilgisayar Programlama I Strings 1. Contents More on Variables – Type Conversions – Enumerations – Structs – Arrays – String Operations 2.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Data collections Android Club Agenda Array ArrayList HashMap.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
C++ Workshop Sujana Jyothi Dept. of Computer Science.
CSci 125 Lecture 21 Martin van Bommel. Memory Allocation Variable declarations cause compiler to reserve memory to hold values - allocation Global variables.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Session 1 C# Basics.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
2/17/2016IT 2791 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Computer Programming Lab 6. Exercise 1 Sample Runs.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
BASIC C PROGRAMMING LANGUAGE TUTORIAL infobizzs.com.
Chapter Structured Data 11. Combining Data into Structures 11.2.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
C++ Lesson 1.
Object-Oriented Programming (OOP) Lecture No. 32
Lecture 8.
Presentation transcript:

CSci 162 Lecture 2 Martin van Bommel

Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration Representation of enumerated type –give each element a number e.g. 0, 1, 2, 3,...

Enumeration via constants One method to use enumerated type #defineSunday0 #defineMonday1 #defineTuesday2 #defineWednesday3 #defineThursday4 #defineFriday5 #defineSaturday6 Then can use if (day == Sunday) cout << ”Rest today\n”;

Defining Enumerated Types Better to use explicit type definition Why? –Compiler chooses integer codes –Programs easier to read with separate, meaningful type name –Easier to debug the program

Enumerated Type Definition enum typename { list of elements }; Example enum weekdayT { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

Enumerated types Values corresponding to elements of types are by default assigned sequentially from zero Can change this behavior: enum yearT { Freshman = 1, Sophmore = 2, Junior = 3, Senior = 4 }; Values assigned sequentially from previous enum yearT{ Freshman=1,Sophmore,Junior,Senior };

Operations of Enumerated Types Values of enumerated type is implicitly converted to integer Integer not implicitly converted to enum weekdayT day = Monday; int d; d = (day + 1) % 7; day = (weekdayT) d; day = (weekdayT) ( (day + 1) % 7 );

Displaying value of type Use function to display value of type void PrintDay(weekdayT day) { switch(day) { case Sunday: cout << ”Sunday”;break; case Monday: cout << ”Monday”;break; etc. }

Another Way to Display Value void PrintDay(weekdayT day) { string name[] = {”Sunday”,”Monday”,…}; cout << name[day]; }

Characters Characters are internally defined almost like an enumerated type enum char { …,’Z’,’[’,…,’`’,’a’,…,’z’,… };

Boolean Values The bool type is an enumerated type enum bool {false, true}; Functions with return type “bool” can return “false” or “true” and still be used in “if”

Array Indexed by enum Since values of enumerated type are implicitly converted to integer, can use as indices to array int sales[7], i, total = 0; sales[Monday] = 10000; for (i=Sunday; i<=Saturday; i++) { total += sales[i]; }