Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember:...

Post on 03-Oct-2020

2 views 0 download

Transcript of Fundamentals of Programming€¦ · Fundamentals of Programming session 17 C Arrays. Remember:...

Fundamentals of Programmingsession 17

C Arrays

Remember: count the ratings

1: Bad

2: Average

3: Good

4: Excellent

What if there are 20 numbers?

1:

2:

3:

:

20:

What if there are 100 numbers?

1:

2:

3:

:

100:

What if there are a million numbers?

1:

2:

3:

:

1000000:

C Arrays

2100

2096

2092

2088

int a[7];

Memory

C Arrays

2100

2096

2092

2088

int a[7];

a[1] = 123;

Memory

C Arrays123

2100

2096

2092

2088

int a[7];

a[1] = 123;

Memory

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

C Arrays

100

?

?

-1

2100

2096

2092

2088

int a[7];

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

200

?

?

Memory

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

Initialize Arrays

40

30

20

10

2100

2096

2092

2088

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

70

60

50

Memory

Initialize Arrays

0

30

20

10

2100

2096

2092

2088

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

0

0

0

Memory

Initialize Arrays

0

0

0

0

2100

2096

2092

2088

int a[7] = {0};

0

0

0

Memory

So what?

100

?

?

-1

2100

2096

2092

2088

int a[7];

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

200

?

?

Memory

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

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

difference with ordinary variables

?

?

?

?

2100

2096

2092

2088

int a[7];

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

?

?

?

Memory

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

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.

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.

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.

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.

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.

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

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

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

countarray4.c

Is N a variable?

countarray4.c

Is N a variable?

Let's look at the preprocessor output:

$ gcc -E countarray4.c

countarray4.c

Only works for initializing to zero!

Draw a Histogram instead.

countarray4.c

countarray4.c countarrayhist.c

countarrayhist.c countarrayhist2.c

Roll a dice N times!