fundamentals of programming€¦ · fundamentals of programming session 17 c arrays. remember:...
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/1.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/2.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/3.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/4.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/5.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/6.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/7.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/8.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/9.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/10.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/11.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/12.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/13.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/14.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/15.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/16.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/17.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/18.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/19.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/20.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/21.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/22.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/23.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/24.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/25.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/26.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/27.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/28.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/29.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/30.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/31.jpg)
![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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/32.jpg)
![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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/33.jpg)
![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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/34.jpg)
![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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/35.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/36.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/37.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/38.jpg)
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?](https://reader036.vdocuments.net/reader036/viewer/2022071212/6025d674d02853162c61b4bf/html5/thumbnails/39.jpg)
Roll a dice N times!