fundamentals of programming€¦ · fundamentals of programming session 17 c arrays. remember:...

39
Fundamentals of Programming session 17 C Arrays

Upload: others

Post on 03-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Fundamentals of Programmingsession 17

C Arrays

Page 2: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Remember: count the ratings

1: Bad

2: Average

3: Good

4: Excellent

Page 3: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

What if there are 20 numbers?

1:

2:

3:

:

20:

Page 4: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

What if there are 100 numbers?

1:

2:

3:

:

100:

Page 5: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

What if there are a million numbers?

1:

2:

3:

:

1000000:

Page 6: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays

2100

2096

2092

2088

int a[7];

Memory

Page 7: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays

2100

2096

2092

2088

int a[7];

a[1] = 123;

Memory

Page 8: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays123

2100

2096

2092

2088

int a[7];

a[1] = 123;

Memory

Page 9: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays

a[3]

a[2]

a[1]

a[0]

2100

2096

2092

2088

int a[7];

a[1] = 123;

a[6]

a[5]

a[4]

Memory

index, subscript

Page 10: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays

100

?

?

-1

2100

2096

2092

2088

int a[7];

a[0] = -1; a[3] = 100;a[6] = 200;

200

?

?

Memory

Page 11: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

C Arrays

100

?

?

-1

2100

2096

2092

2088

int a[7];

a[0] = -1; a[3] = 100;a[6] = 200;

a[7] = 700;

200

?

?

Memory

Page 12: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Initialize Arrays

40

30

20

10

2100

2096

2092

2088

int a[7] = {10,20,30,40,50,60,70};

70

60

50

Memory

Page 13: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Initialize Arrays

0

30

20

10

2100

2096

2092

2088

int a[7] = {10,20,30};

0

0

0

Memory

Page 14: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Initialize Arrays

0

0

0

0

2100

2096

2092

2088

int a[7] = {0};

0

0

0

Memory

Page 15: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

So what?

100

?

?

-1

2100

2096

2092

2088

int a[7];

a[0] = -1; a[3] = 100;a[6] = 200;

200

?

?

Memory

Page 16: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

difference with ordinary variables

200

?

?

-1

2100

2096

2092

2088

int a[7];

a[0] = -1; a[3] = 100;a[6] = 200;

int i = 3;a[i] = 200;

i += 2;a[i] = i*i*i;

a[i+1] = 300;

300

125

?

Memory

Page 17: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

difference with ordinary variables

200

?

?

-1

2100

2096

2092

2088

int a[7];

a[0] = -1; a[3] = 100;a[6] = 200;

int i = 3;a[i] = 200;

i += 2;a[i] = i*i*i; 200

125

?

Memory

Page 18: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

difference with ordinary variables

?

?

?

?

2100

2096

2092

2088

int a[7];

for (int i = 0; i < 7; i++) { a[i] = i*i;}

?

?

?

Memory

Page 19: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

difference with ordinary variables

9

4

1

0

2100

2096

2092

2088

int a[7];

for (int i = 0; i < 7; i++) { a[i] = i*i;}

for (int i = 0; i < 7; i++) { printf("%d\n", a[i]);}

36

25

16

Memory

array1.c

Page 20: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

Page 21: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

Page 22: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

Page 23: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

Page 24: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

Page 25: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Back to our problemget students scores (all integers {0,1,2,...,19,20}, no fractions). For each number 0,1,2,....,20 print the number of students with this score.

countarray3.c

Page 26: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Change the code so that grades are between 0 and 100.

Page 27: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Change the code so that grades are between 0 and 100.

Page 28: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

countarray4.c

Page 29: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Is N a variable?

countarray4.c

Page 30: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Is N a variable?

Let's look at the preprocessor output:

$ gcc -E countarray4.c

countarray4.c

Page 31: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?
Page 32: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?
Page 33: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?
Page 34: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?
Page 35: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Only works for initializing to zero!

Page 36: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Draw a Histogram instead.

countarray4.c

Page 37: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

countarray4.c countarrayhist.c

Page 38: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

countarrayhist.c countarrayhist2.c

Page 39: Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember: count the ratings 1: Bad 2: Average 3: Good 4: Excellent. What if there are 20 numbers?

Roll a dice N times!