COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
The evolution of Pokemon society Structures. COMP102 Prog. Fundamentals, Structures / Slide 2 “Good Old Days” At first it was Pokemon and his variables.
Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
COMP103 - C++ Review1 Variables and Special Chars (Ch. 2, 3)
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
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,
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Enumeration.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Enumerated Types Mr. Dave Clausen La Cañada High School.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
User-Defined Data Types
LESSON 06.
Chapter 10-1: Structure.
Pointers, Enum, and Structures
Programming Structures.
Enumeration Types and typedef
Variables and Special Chars (Ch. 2, 3)
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Structures.
Chapter 1: Introduction to Data Structures(8M)
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Sun Mon Tue Wed Thu Fri Sat
C Structures, Unions, Bit Manipulations and Enumerations
Structures In C Programming By Rajanikanth B.
Sun Mon Tue Wed Thu Fri Sat
Programming Structures.
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Programming Structures.
Structures Structured Data types Data abstraction structs ---
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
Programming Fundamental
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

Structure Structure – User defined data types  Collection of related data items Possibly of different types  In C++ Called struct Can be composed of data of different types Array  Can contain only data of the same type

Structure E.g.  Student record Student id, name, major, gender, start year, …  Bank account Account number, name, currency, balance, …  Address book Name, address, telephone number, …

Structure Individual components of a struct type  Called members (or fields) Members can be of different types  int, float, double, char, array, or even another struct A struct is named as a whole  While individual members are named using field identifiers Complex data structures  Can be formed by defining arrays of structs

struct Basics Definition of a structure E.g. struct { ; … }; struct date { int day; int month; int year; }; struct studentRecord { char gender; int id; char dept[5]; char name[20]; };

struct Basics Declaration of a variable of struct type  Syntax ; E.g.  studentRecord student1, student2; student1 and student2 are variables of studentRecord type

struct Basics The members of a struct type variable are accessed with the dot (. ) operator  Syntax. ; E.g.  student1.gender = 'M';  student1.id = 12345;  strcpy(student1.dept, "COMP");  strcpy(student1.name, "Chan Tai Man");

struct Basics Overview

struct-to-struct Assignment The values contained in one struct type variable  Can be assigned to another variable Of the same struct type E.g. student1 = student2; E.g.

Nested Structures Nest structures inside structures E.g. struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; triangle T; (P.x, P.y) (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) (T.p2.x, T.p2.y) (T.p1.x, T.p1.y) (T.p3.x, T.p3.y) line p1 p2 x y

Arrays of Structures An array of structs  Multiple types of data in each array element E.g. studentRecord class[100]; class[98].gender = 'M'; class[98].id = 12345; strcpy(class[98].dept, "COMP"); strcpy(class[98].name, "Chan Tai Man"); class[0] = class[98];

Arrays inside structures We can use arrays inside structures E.g. Assign values to sq using the given square struct square { point vertex[4]; }; … square sq;

Initialize Data of struct Type Using assignment during declaration E.g. // student record struct StudentRecord { double totalgrade; char name[name_size]; char id[id_size]; int grade[no_grades]; }; StudentRecord student1 = { 0.0, "CHAN Tai Man", "123456", {80, 67, 34, 67} };

Enumerated Type The enumerated type ( enum )  Derived from the integer type  Each integer value is given an identifier Called an enumeration constant Syntax  enum type_name {enumeration_constants};  type_name variable_name;

Enumerated Type E.g. enum days {mon, tue, wed, thur, fri, sat, sun}; days day_of_week; // normal expression day_of_week = wed; // ERROR: miss match type day_of_week = 0; // CORRECT: 0 is converted to days day_of_week = (days) 0; // Use enumeration constant as an integer int x = tue; // same as x = 3; int day[7]; day[tue] = 0; // same as day[1] = 0;

enum and struct E.g. enum suit {club, diamond, heart, spade}; struct card { suit cardSuit; int value; };

SUMMARY By the end of this lab, you should be able to:  Declare and use of struct type Nested structure Array of struct  Declare and use of enum type

Lab12 Download the template from website Save/Load function is optional, no need to demo this function in lab