240-222 CPT: Types/61 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these...

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

Transcript of 240-222 CPT: Types/61 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these...

240-222 CPT: Types/6 1

240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998

Objective of these slides:– to look at how new types can be created

6. User-defined Types

240-222 CPT: Types/6 2

OverviewOverview

1. Enumeration Types

2. typedef

3. Structures (struct)

240-222 CPT: Types/6 3

1. Enumeration Types Sec. 10.111. Enumeration Types Sec. 10.11

1.1. Declaring Enumeration Types and Variables

1.2. Using Enumeration Type Variables

1.3. Initialisation

240-222 CPT: Types/6 4

1.1. Declaring Enumeration Types 1.1. Declaring Enumeration Types and Variablesand Variables

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;

240-222 CPT: Types/6 5

1.2. Using Enumeration Type 1.2. Using Enumeration Type VariablesVariables

Assignment can only use enumerators:d1 = FRI;d2 = MON;

Testing:if (d1 == d2) . . . /* do something */

240-222 CPT: Types/6 6

1.3. Initialisation1.3. Initialisation

enum suit {CLUBS = 1, DIAMONDS, HEARTS, SPADES};

enum suit a, b,c;

Or:

enum suit {CLUBS = 1, DIAMONDS, HEARTS, SPADES} a, b, c;

240-222 CPT: Types/6 7

2. typedef Sec. 10.62. typedef Sec. 10.6

2.1. Using typedef Types

2.2. What is Tomorrow?

2.3. Advanced typedef Use

240-222 CPT: Types/6 8

2.1. Using Typedef Types2.1. Using Typedef Types

typedef is for defining new type names:

typedef int age;/* typedef <current type> <new type>; */

Declare 3 variables:age s, t, u;

Use:s = 18;

240-222 CPT: Types/6 9

2.2. What is Tomorrow?2.2. What is Tomorrow?

/* Manipulating a day type */#include <stdio.h>

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

240-222 CPT: Types/6 10

int main(){ Day d1 = MON; : d1 = get_next_day(d1); : return 0;}

continued

240-222 CPT: Types/6 11

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

240-222 CPT: Types/6 12

Or:Or:

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

240-222 CPT: Types/6 13

2.3. Advanced Typedef Use2.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 */

240-222 CPT: Types/6 14

3. Structures (struct) Sec. 10.13. 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

240-222 CPT: Types/6 15

3.1. Using structs3.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 */

240-222 CPT: Types/6 16

Assignment to the parts:Assignment to the parts:

c1.value = 5;c1.suit = 'd';

c2.value = 12; /* a queen */c2.suit = 's';

240-222 CPT: Types/6 17

3.2. Assignment and Testing3.2. Assignment and Testing

struct card a, b::

a = b; /* ok */

if (a == b) /* incorrect */. . . ;

Instead write:if (card_equality(a,b))

. . . ;

240-222 CPT: Types/6 18

int card_equality(struct card a, struct card b)

{ if ((a.value == b.value) && (a.suit == b.suit)) return 1; else return 0;}

240-222 CPT: Types/6 19

3.3. Combining Type & Variables3.3. Combining Type & Variables

struct card {int value;char suit;

} c, deck[52];

deck[0] deck[1] deck[2] deck[51]

value member suit member

. . . . .

. . . . .

The deck[] array

240-222 CPT: Types/6 20

The deck[] array: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 */

240-222 CPT: Types/6 21

3.4. Student Database3.4. Student Database

/* initialise the class array and calculate the number of fails */#include <stdio.h>

#define CLASS_SIZE 100

struct stude { int student_id; char grade;}

int num_failed(struct stude cls[]);:

continued

240-222 CPT: Types/6 22

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

240-222 CPT: Types/6 23

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

240-222 CPT: Types/6 24

3.5. Initialisation of Structures3.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}}};

240-222 CPT: Types/6 25

The The m[][]m[][] Array Array

m[0][]

m[1][]

m[2][]

1.0, -0.5 2.5, 1.0 0.7, 0.7

7.0, -6.5 -0.5, 1.0 45.7, 8.0

0.0, 0.0 0.0, 0.0 0.0, 0.0

240-222 CPT: Types/6 26

3.6. Structs are Passed Call by Value3.6. Structs are Passed Call by Value

/* Try to change a grade */#include <stdio.h>#define CLASS_SIZE 100

struct stude { int student_id; char grade;}

void chg_grade(struct stude s, char grade);

:

continued

240-222 CPT: Types/6 27

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

240-222 CPT: Types/6 28

void chg_grade(struct stude s, char grade)

/* change isn't remembered because of call by value passing */{ s.grade = grade;}

240-222 CPT: Types/6 29

Successfully Change a GradeSuccessfully Change a Grade

#include <stdio.h>#define CLASS_SIZE 100

struct stude { int student_id; char grade;}

struct stude chg_grade(struct stude s, char grade);

:

continued

240-222 CPT: Types/6 30

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

240-222 CPT: Types/6 31

struct stude chg_grade(struct stude s, char grade)

/* changed student returned */{ s.grade = grade; return s;}

240-222 CPT: Types/6 32

3.7. Arrays (of Structs) are Passed 3.7. Arrays (of Structs) are Passed Call by ReferenceCall by Reference/* Update a grade by searching the whole array */#include <stdio.h>#define CLASS_SIZE 100

struct stude { int student_id; char grade;}

void update_grade(struct stude cls[], int id, char grade);

continued

240-222 CPT: Types/6 33

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

240-222 CPT: Types/6 34

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