welcome! 0 [webcms3.cse.unsw.edu.au] · welcome! 0 comp1511 18s1 programming fundamentals. comp1511...

30
0 Welcome! COMP1511 18s1 Programming Fundamentals

Upload: others

Post on 12-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

0Welcome!COMP1511 18s1

Programming Fundamentals

Page 2: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

1COMP1511 18s1 — Lecture 6 —

Loops + ArraysAndrew Bennett

<[email protected]>

loops inside loops stopping loops

arrays

Page 3: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

Before we begin… 

introduce yourself to the person sitting next to you

 

why did they decide to study computing?

Page 4: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

3

Feedbackupload lecture code

upload/incorporate diagrams in lecture recordings

more diagrams

go through programs as a whole before running them

lecture subtitles

Page 5: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

4

Overviewafter this lecture, you should be able to…

write programs using nested while loops to solve simple problems

understand how to stop loops using loop counters and sentinel variables

understand the basics of arrays

understand the basics of designing a solution to a problem (time permitting)

(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 6: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

5

AdminDon’t panic!

lecture recordings are on WebCMS3 I’ll try to add drawings/diagrams + upload to YouTube

course style guide published

weekly test due wednesday don’t be scared!

assignment 1 coming soon more on that tomorrow!

don’t forget about help sessions! see course website for details

Page 7: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

6

Loopswhat if we want to do something multiple times?

Use a loop!

keep doing this while this condition is true

Page 8: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

7

Anatomy of a Loopinitialisation

set up our variables

condition while “something”…

statements things we do inside our loop

update move along to the next iteration

Page 9: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

8

Anatomy of a Loop

// initialisation

int i = 0;

// condition

while (i < n) {

// statements -- do something in the loop

printf("Hello!\n");

// update

i++;

}

Page 10: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

9

Stopping: Loop Counters

// Print out "hello, world!" n times,

// where n is chosen by the user.

int num;

printf ("Enter a number: ");

scanf ("%d", &num);

int i = 0;

while (i < num) {

printf ("hello, world!\n");

i = i + 1;

}

Page 11: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

10

Stopping: Sentinel Value (Flag)

// Print out the number that the user entered

// Stop when they type 0

int n = 1;

while (n != 0) {

printf ("You entered: %d\n", n);

scanf("%d", &n);

}

Page 12: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

11

Nested Loops

while (something) {

while (somethingElse) {

}

}

Page 13: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

12

Nested Loops

int i = 0;

while (i < n) {

int j = 0;

while (j < n) {

// do something

printf("!!");

j++;

}

i++;

}

Page 14: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

13

revisiting: variables

Page 15: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

14

Variablesvariables are like a box that can hold a value

of a certain type

int i = 5;

______

| |

| 5 |

|______|

Page 16: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

15

Variableswe can have as many variables as we like

int i = 5;

______

| |

| 5 |

|______|

int age = 18;

______

| |

| 18 |

|______|

double pi = 3.14;

______

| |

| 3.14 |

|______|

Page 17: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

16

Lots of Variablessometimes we want to store a lot of related variables

int mark_student0 = 85;

______

| |

| 85 |

|______|

int mark_student1 = 90;

______

| |

| 90 |

|______|

int mark_student2 = 45;

______

| |

| 45 |

|______|

Page 18: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

17

Lots of Variablessometimes we want to store a lot of related variables

int mark_student0 = 85;

int mark_student1 = 90;

int mark_student2 = 45;

double averageMark = (mark_student1 + mark_student2 + mark_student3)/3;

printf("The average mark was: %lf\n", averageMark);

This doesn’t scale!

Page 19: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

18

introducing: arrays

Page 20: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

19

ArraysA series of boxes with a common type,

all next to each other

╔════╤════╤════╤════╦════╤════╤════╤════╦════╤════╤════╤════╦┄

║ │ │ │ ║ │ │ │ ║ │ │ │ ║

╚════╧════╧════╧════╩════╧════╧════╧════╩════╧════╧════╧════╩┄

Page 21: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

20

ArraysA series of boxes with a common type,

all next to each other

0 1 2 3 4 5 6 7 8 9 10 11

╔════╤════╤════╤════╦════╤════╤════╤════╦════╤════╤════╤════╦┄

║ │ │ │ ║ │ │ │ ║ │ │ │ ║

╚════╧════╧════╧════╩════╧════╧════╧════╩════╧════╧════╧════╩┄

Page 22: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

21

Why?Suppose we need to compute statistics on class marks…

int mark_student0, mark_student1, mark_student2, ...;

mark_student0 = 85;

mark_student1 = 90;

mark_student2 = 45;

...

becomes unfeasible if dealing with a lot of values … we’d need hundreds of individual variables!

Page 23: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

22

Why?Solution: Use an array!

int mark[1160];

mark[0] = 85;

mark[1] = 90;

mark[2] = 45;

...

Page 24: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

23

Arrays in Ca collection of array elements

each element must be the same type

we refer to arrays by their index valid indices for

elements are

no real limit on number of elements

we cannot assign, scan, or print whole arrays… but we can assign, scan, and print elements

Page 25: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

24

Arrays in C

// Declare an array with 10 elements

// and initialises all elements to 0.

int myArray[10] = {0};

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 26: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

25

Arrays in C

int myArray[10] = {0};

// Put some values into the array.

myArray[0] = 3;

myArray[5] = 17;

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 3 │ 0 │ 0 │ 0 │ 0 │ 17 │ 0 │ 0 │ 0 │ 0 │

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 27: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

26

Arrays in C

int myArray[10] = {0};

// Put some values into the array.

myArray[0] = 3;

myArray[5] = 17;

myArray[10] = 42; // <-- Error

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 3 │ 0 │ 0 │ 0 │ 0 │ 17 │ 0 │ 0 │ 0 │ 0 │ ???

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 28: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

27

Reading an Arrayscanf() can’t read an entire array.

this will only read 1 number:

#define ARRAY_SIZE 42

...

int array[ARRAY_SIZE];

scanf ("%d", &array);

instead, you must read the elements one by one:

int i = 0;

while (i < ARRAY_SIZE) {

scanf ("%d", &array[i]);

i++;

}

Page 29: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

28

Printing an Arrayprintf() also can’t print an entire array.

this won’t compile…

#define ARRAY_SIZE 42

...

int array[ARRAY_SIZE];

printf ("%d", array);

instead, you must print the elements one by one:

int i = 0;

while (i < ARRAY_SIZE) {

printf ("%d", array[i]);

i++;

}

Page 30: Welcome! 0 [webcms3.cse.unsw.edu.au] · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 6 —

29

Copying an Arraygiven:

#define ARRAY_SIZE 5

int array1[ARRAY_SIZE] = {1, 4, 9, 16, 25};

int array2[ARRAY_SIZE];

this won’t compile…

array2 = array1;

instead, you must copy the elements one by one:

int i = 0;

while (i < ARRAY_SIZE) {

array2[i] = array1[i];

i++;

}