Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Types

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

3 240-222 CPT: Types/63 1. Enumeration Types Sec. 10.11 1.1.Declaring Enumeration Types and Variables 1.2.Using Enumeration Type Variables 1.3.Initialisation

4 240-222 CPT: Types/64 1.1. 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;

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

6 240-222 CPT: Types/66 1.3. 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;

7 240-222 CPT: Types/67 2. typedef Sec. 10.6 2.1.Using typedef Types 2.2.What is Tomorrow? 2.3.Advanced typedef Use

8 240-222 CPT: Types/68 2.1. 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;

9 240-222 CPT: Types/69 2.2. 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

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

11 240-222 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; }

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

13 240-222 CPT: Types/613 2.3. 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 */

14 240-222 CPT: Types/614 3. Structures (struct) Sec. 10.1 3.1.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

15 240-222 CPT: Types/615 3.1. 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 */

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

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

18 240-222 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; }

19 240-222 CPT: Types/619 3.3. 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

20 240-222 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 */

21 240-222 CPT: Types/621 3.4. 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

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

23 240-222 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; }

24 240-222 CPT: Types/624 3.5. 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}} };

25 240-222 CPT: Types/625 The m[][] Array m[0][] m[1][] m[2][] 1.0, -0.5 2.5, 1.00.7, 0.7 7.0, -6.5-0.5, 1.045.7, 8.0 0.0, 0.0

26 240-222 CPT: Types/626 3.6. 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

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

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

29 240-222 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

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

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

32 240-222 CPT: Types/632 3.7. 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

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

34 240-222 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; }


Download ppt "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."

Similar presentations


Ads by Google