class: middle, center # Lecture 11 ### 2 November 2020 .smaller[ Admin Matters
Unit 28: struct
Unit 29: stdio
] --- class: middle - PE2 this Saturday - Arrangement for final --- class: middle ### Tutorial/Lab 10 - Assignment 8 - PE2 from 18/19 --- class: middle ### Compound Data Type - Array: multiple values of the same data type - Structure: multiple values of different data types --- class: middle ### Some things belong together - 1D array and its length - 2D array, its width, and its height - x and y position in a maze --- class: middle,center ## Defining a `struct` --- class: middle ```C struct matrix { double** array; long num_of_rows; long num_of_columns; }; ``` --- class: middle ```C struct list { char *array; long length; }; ``` --- class: middle ```C struct module { char *code; char *title; long mc; }; ``` --- class: middle ### Declaring a struct variable ```C struct module cs1010; ``` --- class: middle ### Initializing a struct variable .tiny[ ```C struct module cs1010; cs1010.code = "CS1010"; cs1010.title = "Programming Methodology"; cs1010.mc = 4; ``` ] --- class: middle ### Declaring AND Initializing a struct variable .tiny[ ```C struct module cs1010 = { .code = "CS1010", .title = "Programming Methodology", .mc = 4 }; ``` ] --- class: middle ### Reading and writing members of a `struct` .tiny[ ```C cs1010.mc = hours_spent_per_week/2.5; cs1010_println_long(cs1010.mc); ``` ] --- class: middle,wide ### `struct` as pass-by-value parameters to a function .tiny[ ```C void update_mc(struct module cs1010, long weekly_hours_spent) { cs1010.mc = weekly_hours_spent/2.5; } ``` ] --- class: middle,wide ### `struct` as pass-by-reference parameters to a function .tiny[ ```C void update_mc(struct module *cs1010, long weekly_hours_spent) { (*cs1010).mc = weekly_hours_spent/2.5; } ``` ] --- class: middle,wide ### `struct` as pass-by-reference parameters to a function .tiny[ ```C void update_mc(struct module *cs1010, long weekly_hours_spent) { cs1010->mc = weekly_hours_spent/2.5; } ``` ] --- class: middle,wide ### How to pass multiple values back to caller (Method 1) .tiny[ ```C void find_max_steps(long n, long *max_n, long *max_num_steps) { *max_num_steps = 0; *max_n = 1; for (long i = 1; i <= n; i += 1) { long num_of_steps = count_num_of_steps(i); if (num_of_steps >= *max_num_steps) { *max_n = i; *max_num_steps = num_of_steps; } } } ``` ] --- class: middle ### How to pass multiple values back to caller (Method 2) ```C struct answer { long max_n; long max_num_steps; }; ``` --- class: middle .tiny[ ```C struct answer find_max_steps(long n) { struct answer ans = { .max_n = 1, .max_num_steps = 0 }; for (long i = 1; i <= n; i += 1) { long num_of_steps = count_num_of_steps(i); if (num_of_steps >= ans.max_num_steps) { ans.max_n = i; ans.max_num_steps = num_of_steps; } } return ans; } ``` ] --- class: middle,center ## Defining Your Own Type --- class: middle .tiny[ ```C typedef unsigned char color_t; : void paint(color_t r, color_t g, color_t b) { : } ``` ] --- class: middle ```C typedef struct module { char *code; char *title; long mc; } module; ``` --- class: middle ```C typedef struct { char *code; char *title; long mc; } module; ``` --- class: middle,wide .tiny[ ```C void update_mc(module cs1010, long weekly_hours_spent) { cs1010.mc = weekly_hours_spent/2.5; } ``` ] --- class: middle,center ## Reading and Printing without `libcs1010` --- class: middle,center ## `printf` and `scanf` --- class: middle ```C printf("%s is a %ld-MC module\n", module.code, module.mc); ``` --- class: middle .tiny[ ``` %[flags][field_width][.precision][length_modifier]specifier ``` ] --- class: middle - `printf` behaves differently from functions that you have been writing. - It takes in different number of arguments of different type. --- class: middle ```C long width; long height; scanf("%ld %ld", &width, &height); ``` --- class: middle .tiny[ ``` %[*][field_width][length_modifier]specifier ``` ] --- class: middle - space in between the format specifier matches zero or more white spaces (space, tab, newline) - Scanning stops when an input character does not match such a format character or when an input conversion fails --- class: bottom .tiny[ Version: 1.0 Last Updated: Sun Nov 1 11:59:04 +08 2020 ]