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; }