![Page 1: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/1.jpg)
0Welcome!COMP1511 18s1
Programming Fundamentals
![Page 3: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/3.jpg)
2
Before we begin…
introduce yourself to the person sitting next to you
how are they going with assignment 2?
![Page 4: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/4.jpg)
3
Overviewafter this lecture, you should be able to…
make progress on assignment 2
have a better understanding of pointers: what pointers are
how to use pointers why we use pointers
have a better understanding of structs
have a better understanding of memory in C: dynamic memory allocation using malloc
the difference between
(note: you shouldn’t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember: programming is like learning any other
language, it takes consistent and regular practice.)
![Page 5: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/5.jpg)
4
AdminDon’t panic!
assignment 2 (if you haven’t started yet, start ASAP
deadline extended to Sunday 13th May
assignment 1 tutor marking/feedback in progress
week 8 weekly test due tomorrow don’t be scared!
don’t forget about help sessions! see course website for details
![Page 6: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/6.jpg)
5
let’s talk about pointers
![Page 7: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/7.jpg)
6
Pointers?before we talk about pointers, let’s take a step back…
![Page 8: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/8.jpg)
7
Variablesthink all the way back to week 1….
int age = 16;
what does this actually mean?
![Page 9: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/9.jpg)
8
Variables and Functions
int main(void) {
int age = 16;
int height = 185;
}
![Page 10: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/10.jpg)
9
Variables and Functions and Arrays
#define SIZE 5
int main(void) {
int age = 16;
int array[SIZE];
foo(array);
}
void foo(int array[SIZE]) {
int num = 10;
array[0] = 100;
}
![Page 11: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/11.jpg)
10
Variables and Functions and Arrays and Pointers
#define SIZE 5
int main(void) {
int age = 16;
int array[SIZE];
foo(array, &age);
}
void foo(int array[SIZE], int *age) {
int num = 10;
array[0] = 100;
*age = 21;
}
![Page 12: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/12.jpg)
11
re-visiting: structs
![Page 13: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/13.jpg)
12
Arraysarrays are a collection of many of the same type of variable
int array[10];
// ten boxes that can each hold 1 int
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
// ten boxes that can each hold 1 int
[0][1][2][3][4][5][6][7][8][9]
![Page 14: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/14.jpg)
13
Structsstructs are a collection of many of different types of variables
struct student {
int zid;
char name[MAX_NAME_LEN];
int ass1_mark;
};
// one box that can hold an int
[5112345]
// MAX_NAME_LEN boxes that can hold a char
[A][n][d][r][e][w][\0][ ][ ]
// one box that can hold an int
[94.5]
![Page 15: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/15.jpg)
14
Structsstructs are a collection of many of different types of variables
struct student {
int zid;
char name[MAX_NAME_LEN];
int ass1_mark;
};
struct student andrew;
andrew.zid = 5112345;
andrew.ass1_mark = 94.5;
strcpy(andrew.name, "Andrew");
// one box that can hold an int
[5112345]
// MAX_NAME_LEN boxes that can hold a char
[A][n][d][r][e][w][\0][ ][ ]
// one box that can hold an int
[94.5]
![Page 16: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/16.jpg)
15
Arrays of Structs?
struct student {
int zid;
char name[MAX_NAME_LEN];
int ass1_mark;
};
struct student students[NUM_STUDENTS];
// fill out one student struct in the array of structs
students[0].zid = 5112345;
students[0].ass1_mark = 94.5;
strcpy(students[0].name, "Andrew");
// fill out another student struct in the array of structs
students[1].zid = 9100123;
students[2].ass1_mark = 64.2;
strcpy(students[3].name, "Andrew");
![Page 17: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/17.jpg)
16
let’s play: Intensity
![Page 18: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/18.jpg)
17
Intensityyour task: write a program to play the game Intensity
the Intensity referee manages the game shuffles cards
deals cards
asks players for moves
etc
all input is given over standard input (i.e. scanf)
all output is given over standard output (i.e. printf)
![Page 19: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/19.jpg)
18
Stateless AIan important concept to understand: your AI is stateless
it comes to life for one single move reads the input
thinks about what to do prints out its decision
![Page 20: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/20.jpg)
19
Intensity Refereethe Intensity Referee runs the game
1511 intensity_referee
you can run your AI against it:
1511 intensity_referee your_ai_code.c
you can play interactively:
1511 intensity_referee -i
![Page 21: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/21.jpg)
20
Valid Cards To Play
![Page 22: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/22.jpg)
21
revisiting: memory
![Page 23: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/23.jpg)
22
Scope and Lifetimesthe variables inside a function only exist as long as the function does
once your function returns, the variables inside are “gone”
(this is why you can’t return an array from a function!)
![Page 24: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/24.jpg)
23
Lifetimeswhat if we need something to “stick around” for longer?
two options: make it in a “parent” function dynamically allocate memory
![Page 25: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/25.jpg)
24
Lifetimesmake it in a “parent” function
void foo(void) {
int array[SIZE];
bar(array);
printf("%d", array[0]);
}
void bar(int array[SIZE]) {
array[0] = 123;
}
![Page 26: Welcome! 0 [webcms3.cse.unsw.edu.au]€¦ · structs are a collection of many of different types of variables struct student { int zid; char name[MAX_NAME_LEN]; int ass1_mark; };](https://reader034.vdocuments.site/reader034/viewer/2022042209/5eada145fc81dc4e392b3e07/html5/thumbnails/26.jpg)
25
Lifetimesdynamically allocate memory
void foo(void) {
int *array = bar();
printf("%d", array[0]);
}
int *bar(void) {
int *array = malloc(SIZE * sizeof(int));
array[0] = 123;
return array;
}