240-222 CPT: Types/61 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
C Programming Lecture 23 Enumeration Types Structures.
Programming in C Chapter 10 Structures and Unions
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
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,
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
Structures and Lists (and maybe stacks) Chapters 9-10.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
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.
CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
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.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
CS 1430: Programming in C++.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
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.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
LESSON 06.
Structure, Unions, typedef and enumeration
Enumeration Types and typedef
Enumerations.
Enumerations.
MON TUE WED THU
Chapter 1: Introduction to Data Structures(8M)
2008 Calendar.
Sun Mon Tue Wed Thu Fri Sat
C Structures, Unions, Bit Manipulations and Enumerations
Programming Language C Language.
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
Structures.
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
2008 Calendar.
Presentation transcript:

CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined Types

CPT: Types/62 Overview 1.Enumeration Types 2. typedef 3. Structures ( struct )

CPT: Types/63 1. Enumeration Types Sec Declaring Enumeration Types and Variables 1.2.Using Enumeration Type Variables 1.3.Initialisation

CPT: Types/ Declaring Enumeration Types and Variables l An enumeration type is a finite set. enum day {SUN, MON, TUE, WED, THU, FRI, SAT}; Declare variables of the enum day type: enum day d1, d2;

CPT: Types/ Using Enumeration Type Variables l Assignment can only use enumerators: d1 = FRI; d2 = MON; l Testing: if (d1 == d2)... /* do something */

CPT: Types/ Initialisation enum suit {CLUBS = 1, DIAMONDS, HEARTS, SPADES}; enum suit a, b,c; l Or: enum suit {CLUBS = 1, DIAMONDS, HEARTS, SPADES} a, b, c;

CPT: Types/67 2. typedef Sec Using typedef Types 2.2.What is Tomorrow? 2.3.Advanced typedef Use

CPT: Types/ Using Typedef Types typedef is for defining new type names: typedef int age; /* typedef ; */ l Declare 3 variables: age s, t, u; l Use: s = 18;

CPT: Types/ What is Tomorrow? /* Manipulating a day type */ #include enum day {SUN, MON, TUE, WED, THU, FRI, SAT}; typedef enum day Day; Day get_next_day(Day d); /* enum day get_next_day(enum day d); */ : continued

CPT: Types/610 int main() { Day d1 = MON; : d1 = get_next_day(d1); : return 0; } continued

CPT: Types/611 Day get_next_day(Day d) { Day next_day; switch (d) { case SUN: next_day = MON; break; case MON: next_day = TUE; break; : /* a case for each day */ : case SAT: next_day = SUN; break; } return next_day; }

CPT: Types/612 Or: Day get_next_day(Day d) /* Compute the next day with a cast */ { return ( (Day) (( (int) d + 1)% 7 )); }

CPT: Types/ Advanced Typedef Use typedef float vector[10]; vector x; /* float x[10] */ #define N 3 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; vector w, s; /* 3 elem array of doubles */ matrix b; /* 3x3 array of doubles */

CPT: Types/ Structures (struct) Sec Using structs 3.2.Assignment and Testing 3.3.Combining Type and Variable Declarations 3.4.Student Database 3.5.Initialisation of Structures 3.6.structs are Passed Call by Value 3.7.Arrays (of structs) are Passed Call by Reference

CPT: Types/ Using structs A struct is a way of collecting together a group of data items in a single type. struct card { int value; char suit; /* 'c','d','h' or 's' */ }; struct card c1, c2; /* two cards */

CPT: Types/616 Assignment to the parts: c1.value = 5; c1.suit = 'd'; c2.value = 12;/* a queen */ c2.suit = 's';

CPT: Types/ Assignment and Testing struct card a, b: : a = b; /* ok */ if (a == b) /* incorrect */... ; l Instead write: if (card_equality(a,b))... ;

CPT: Types/618 int card_equality(struct card a, struct card b) { if ((a.value == b.value) && (a.suit == b.suit)) return 1; else return 0; }

CPT: Types/ Combining Type & Variables struct card { int value; char suit; } c, deck[52]; deck[0]deck[1]deck[2] deck[51] value membersuit member..... The deck[] array

CPT: Types/620 The deck[] array: deck[0].value = 3; deck[0].suit = 'd';/* 3 of diamonds */ deck[2].value = 5; deck[2].suit = 'h';/* 5 of hearts */

CPT: Types/ Student Database /* initialise the class array and calculate the number of fails */ #include #define CLASS_SIZE 100 struct stude { int student_id; char grade; } int num_failed(struct stude cls[]); : continued

CPT: Types/622 int main() { struct stude class[CLASS_SIZE]; /* fill in class entries */ class[0].student_id = ; class[0].grade = 'A'; class[1].student_id = ; class[1].grade = 'B'; : : printf("The number of fails is %d\n", num_failed(class) ); return 0; } continued

CPT: Types/623 int num_failed(struct stude cls[]) /* How many students got 'F'? */ { int i, cnt = 0; for (i = 0; i < CLASS_SIZE; i++) cnt += (cls[i].grade == 'F'); return cnt; }

CPT: Types/ Initialisation of Structures struct card c = {12, 's'}; /* queen of spades */ struct complex { double real; double imaginary; }; struct complex m[3][3] = { {{1.0, -0.5}, {2.5, 1.0}, {0.7, 0.7}}, {{7.0, -6.5}, {-0.5, 1.0}, {45.7, 8.0}} };

CPT: Types/625 The m[][] Array m[0][] m[1][] m[2][] 1.0, , , , , , , 0.0

CPT: Types/ Structs are Passed Call by Value /* Try to change a grade */ #include #define CLASS_SIZE 100 struct stude { int student_id; char grade; } void chg_grade(struct stude s, char grade); : continued

CPT: Types/627 int main() { struct stude class[CLASS_SIZE]; class[0].student_id = ; class[0].grade = 'A'; : chg_grade(class[0], 'B'); /* class[0] grade still 'A' */ return 0; } continued

CPT: Types/628 void chg_grade(struct stude s, char grade) /* change isn't remembered because of call by value passing */ { s.grade = grade; }

CPT: Types/629 Successfully Change a Grade #include #define CLASS_SIZE 100 struct stude { int student_id; char grade; } struct stude chg_grade(struct stude s, char grade); : continued

CPT: Types/630 int main() { struct stude class[CLASS_SIZE]; class[0].student_id = ; class[0].grade = 'A'; : class[0] = chg_grade(class[0], 'B'); /* class[0] grade now 'B' */ return 0; } continued

CPT: Types/631 struct stude chg_grade(struct stude s, char grade) /* changed student returned */ { s.grade = grade; return s; }

CPT: Types/ Arrays (of Structs) are Passed Call by Reference /* Update a grade by searching the whole array */ #include #define CLASS_SIZE 100 struct stude { int student_id; char grade; } void update_grade(struct stude cls[], int id, char grade); continued

CPT: Types/633 int main() { struct stude class[CLASS_SIZE]; class[0].student_id = ; class[0].grade = 'A'; : update_grade(class, , 'B'); /* class[0] grade now 'B' */ return 0; } continued

CPT: Types/634 void update_grade(struct stude cls[], int id, char grade) /* Change is remembered since arrays are passed using Call by Reference. */ { int count = 0; while (cls[count].student_id != id) count++; cls[count].grade = grade; }