Chapter 12 Enumerated, Structure, and Union Types Objectives

Slides:



Advertisements
Similar presentations
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Week 8 Arrays Part 2 String & Pointer
Computer Science: A Structured Programming Approach Using C Converting File Type A rather common but somewhat trivial problem is to convert a text.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Topics discussed in this section:
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
+ 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.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers and Classes.
Introduction to Computers Computer Generations
Pointers Introduction
Chapter 7 Text Input/Output Objectives
Topics discussed in this section:
Chapter 7 Text Input/Output Objectives
Introduction to the C Language
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
CS1010 Programming Methodology
Chapter 10-1: Structure.
Chapter 7 Text Input/Output Objectives
FIGURE 4-10 Function Return Statements
C++, OBJECT ORIENTED PROGRAMMING
FIGURE 9-5 Integer Constants and Variables
Chapter 8 Arrays Objectives
Topics discussed in this section:
Chapter 9 Pointers Objectives
Topics discussed in this section:
Introduction to the C Language
Binary Files.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Introduction to the C Language
3-7 Sample Programs This section contains several programs that you should study for programming technique and style. Computer Science: A Structured.
Chapter 15 Lists Objectives
C Structures, Unions, Bit Manipulations and Enumerations
Topics discussed in this section:
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Functions Objectives
Topics discussed in this section:
Derived types.
Chapter 14 Bitwise Operators Objectives
Chapter 8 Arrays Objectives
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 8 Arrays Objectives
CS250 Introduction to Computer Science II
Chapter 9: Pointers and String
Pointers and dynamic objects
Topics discussed in this section:
Structures, Unions, and Enumerations
Presentation transcript:

Chapter 12 Enumerated, Structure, and Union Types Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition statement in programs ❏ To use enumerated types, including anonymous types. ❏ To create and use structures in programs ❏ To be able to use unions in programs ❏ To understand the software engineering concept of coupling and to be able to evaluate coupling between functions. Computer Science: A Structured Programming Approach Using C

FIGURE 12-1 Derived Types Computer Science: A Structured Programming Approach Using C

12-1 The Type Definition (typedef) Before discussing the derived types, let’s discuss a C declaration that applies to all of them—the type definition. A type definition, typedef, gives a name to a data type by creating a new type that can then be used anywhere a type is permitted. Computer Science: A Structured Programming Approach Using C

FIGURE 12-2 Type-definition Format Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 12-2 Enumerated Types The enumerated type is a user-defined type based on the standard integer type. In an enumerated type, each integer value is given an identifier called an enumeration constant. Topics discussed in this section: Declaring an Enumerated Type Operations on Enumerated Types Enumeration Type Conversion Initializing Enumerated Constants Anonymous Enumeration: Constants Input/Output Operations Computer Science: A Structured Programming Approach Using C

Print Cable TV Stations PROGRAM 12-1 Print Cable TV Stations Computer Science: A Structured Programming Approach Using C

Print Cable TV Stations PROGRAM 12-1 Print Cable TV Stations Computer Science: A Structured Programming Approach Using C

Note Don’t be confused about strings and enumerated types. “Jan” is a string made of three characters; JAN as defined in the previous code example, is an enumerated type (identifier) which has the integer value 1. Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 12-3 Structure A structure is a collection of related elements, possibly of different types, having a single name. Topics discussed in this section: Structure Type Declaration Initialization Accessing Structures Operations on Structures Complex Structures Structures and Functions Computer Science: A Structured Programming Approach Using C

FIGURE 12-3 Structure Examples Computer Science: A Structured Programming Approach Using C

should be logically related. Note Elements in a structure can be of the same or different types. However, all elements in the structure should be logically related. Computer Science: A Structured Programming Approach Using C

FIGURE 12-4 Tagged Structure Format Computer Science: A Structured Programming Approach Using C

FIGURE 12-5 Structure Declaration with typedef Computer Science: A Structured Programming Approach Using C

FIGURE 12-6 Structure Declaration Format and Example Computer Science: A Structured Programming Approach Using C

FIGURE 12-7 Initializing Structures Computer Science: A Structured Programming Approach Using C

FIGURE 12-8 Structure Direct Selection Operator Computer Science: A Structured Programming Approach Using C

PROGRAM 12-2 Multiply Fractions Computer Science: A Structured Programming Approach Using C

PROGRAM 12-2 Multiply Fractions Computer Science: A Structured Programming Approach Using C

FIGURE 12-9 Copying a Structure Computer Science: A Structured Programming Approach Using C

FIGURE 12-10 Pointers to Structures Computer Science: A Structured Programming Approach Using C

(*pointerName).fieldName « pointerName->fieldName. Note (*pointerName).fieldName « pointerName->fieldName. Computer Science: A Structured Programming Approach Using C

FIGURE 12-11 Interpretation of Invalid Pointer Use Computer Science: A Structured Programming Approach Using C

FIGURE 12-12 Indirect Selection Operator Computer Science: A Structured Programming Approach Using C

Clock Simulation with Pointers PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

Clock Simulation with Pointers PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

Clock Simulation with Pointers PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

Clock Simulation with Pointers PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

FIGURE 12-13 Nested Structure Computer Science: A Structured Programming Approach Using C

FIGURE 12-14 Arrays in Structures Computer Science: A Structured Programming Approach Using C

FIGURE 12-15 Pointers in Structures Computer Science: A Structured Programming Approach Using C

FIGURE 12-16 Array of Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

Sort Array of Student Structures PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

FIGURE 12-17 Passing Structure Members to Functions Computer Science: A Structured Programming Approach Using C

FIGURE 12-18 Passing and returning structures Computer Science: A Structured Programming Approach Using C

Passing and Returning Structures PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

Passing and Returning Structures PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

Passing and Returning Structures PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

Passing and Returning Structures PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

Passing and Returning Structures PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

FIGURE 12-19 Passing Structures Through Pointers Computer Science: A Structured Programming Approach Using C

Passing Structures through Pointers PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

Passing Structures through Pointers PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

Passing Structures through Pointers PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

Passing Structures through Pointers PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C