+ - 0:00:00
Notes for current slide
Notes for next slide

Lecture 11

2 November 2020

Admin Matters
Unit 28: struct
Unit 29: stdio

AY2021 S1              1
  • PE2 this Saturday
  • Arrangement for final
AY2021 S1              2

Tutorial/Lab 10

  • Assignment 8
  • PE2 from 18/19
AY2021 S1              3

Compound Data Type

  • Array: multiple values of the same data type
  • Structure: multiple values of different data types
AY2021 S1              4

Some things belong together

  • 1D array and its length
  • 2D array, its width, and its height
  • x and y position in a maze
AY2021 S1              5

Defining a struct

AY2021 S1              6
struct matrix {
double** array;
long num_of_rows;
long num_of_columns;
};
AY2021 S1              7
struct list {
char *array;
long length;
};
AY2021 S1              8
struct module {
char *code;
char *title;
long mc;
};
AY2021 S1              9

Declaring a struct variable

struct module cs1010;
AY2021 S1              10

Initializing a struct variable

struct module cs1010;
cs1010.code = "CS1010";
cs1010.title = "Programming Methodology";
cs1010.mc = 4;
AY2021 S1              11

Declaring AND Initializing a struct variable

struct module cs1010 = {
.code = "CS1010",
.title = "Programming Methodology",
.mc = 4
};
AY2021 S1              12

Reading and writing members of a struct

cs1010.mc = hours_spent_per_week/2.5;
cs1010_println_long(cs1010.mc);
AY2021 S1              13

struct as pass-by-value parameters to a function

void update_mc(struct module cs1010, long weekly_hours_spent) {
cs1010.mc = weekly_hours_spent/2.5;
}
AY2021 S1              14

struct as pass-by-reference parameters to a function

void update_mc(struct module *cs1010, long weekly_hours_spent) {
(*cs1010).mc = weekly_hours_spent/2.5;
}
AY2021 S1              15

struct as pass-by-reference parameters to a function

void update_mc(struct module *cs1010, long weekly_hours_spent) {
cs1010->mc = weekly_hours_spent/2.5;
}
AY2021 S1              16

How to pass multiple values back to caller (Method 1)

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;
}
}
}
AY2021 S1              17

How to pass multiple values back to caller (Method 2)

struct answer {
long max_n;
long max_num_steps;
};
AY2021 S1              18
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;
}
AY2021 S1              19

Defining Your Own Type

AY2021 S1              20
typedef unsigned char color_t;
:
void paint(color_t r, color_t g, color_t b) {
:
}
AY2021 S1              21
typedef struct module {
char *code;
char *title;
long mc;
} module;
AY2021 S1              22
typedef struct {
char *code;
char *title;
long mc;
} module;
AY2021 S1              23
void update_mc(module cs1010, long weekly_hours_spent) {
cs1010.mc = weekly_hours_spent/2.5;
}
AY2021 S1              24

Reading and Printing without libcs1010

AY2021 S1              25

printf and scanf

AY2021 S1              26
printf("%s is a %ld-MC module\n",
module.code, module.mc);
AY2021 S1              27
%[flags][field_width][.precision][length_modifier]specifier
AY2021 S1              28
  • printf behaves differently from functions that you have been writing.

  • It takes in different number of arguments of different type.

AY2021 S1              29
long width;
long height;
scanf("%ld %ld", &width, &height);
AY2021 S1              30
%[*][field_width][length_modifier]specifier
AY2021 S1              31
  • 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

AY2021 S1              32

Version: 1.0

Last Updated: Sun Nov 1 11:59:04 +08 2020

AY2021 S1              33
  • PE2 this Saturday
  • Arrangement for final
AY2021 S1              2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow