cint university / 101 / ced, the cint editor
101-12: lists
This is lesson 101-12 of CU-101.2, checked against cint 0.2.0.
You will keep a week of numbers under one name, reach each one by its position and see what cint does when a position does not exist.
the program
void main() {
I64[7] temps = [12, 15, 11, 18, 21, 19, 16];
"there are {len(temps)} days\n";
"Monday was {temps[0]}, Sunday was {temps[6]}\n";
temps[2] = 13;
"Wednesday, corrected: {temps[2]}\n";
"the day after Sunday: {temps[7]}\n";
}
Run it:
there are 7 days
Monday was 12, Sunday was 16
Wednesday, corrected: 13
fault[E_BOUNDS]: index out of bounds
--> temps.ci:7:34
|
7 | "the day after Sunday: {temps[7]}\n";
| ^ index.checked.i64
|
= operation: index.checked.i64
= operand-count: 1
= operand: I64 7
= exact: none
= limit: I64 7
= revision: 9eb95af35c842e3f063ac00a7cb32e13cd838a461e5eb04b6d46c5eaf40c8c4b
= source-map: none
= address: none
= stack-depth: 0
= fuel-consumed: 1 (not part of the fault record)
= state: the program stopped; stdout holds what it wrote before the fault
what happened
A list holds several numbers under one name. I64[7] temps makes a list of seven I64 numbers called temps. In cint a list of a fixed length is called an array. The [12, 15, 11, 18, 21, 19, 16] after the = gives the seven numbers to start with, in order: a week of noon temperatures in degrees Celsius, from Monday to Sunday.
Positions count from 0. temps[0] is the first number, 12, and temps[6] is the seventh and last, 16. The number in the square brackets is called the index. Other programming languages, such as C and Python, count positions from 0 too. An array of 7 has the indexes 0 to 6.
len gives the length. len(temps) is 7. A loop over 0..len(temps) visits every index, 0 to 6, as lesson 101-07 explained for ...
One number can change. temps[2] = 13; replaces the third number, Wednesday's 11, with 13. The rest stay as they were.
There is no eighth day. temps[7] asks for an index the array does not have. cint stopped the program with an index out of bounds fault at line 7, at temps[7]. The report says the index was 7 and the limit is 7, meaning the index must be less than 7. The three lines before it printed, and nothing after it did.
change it
- Print the average temperature of the week with a loop that adds up every day.
- Find the warmest day and print its index. You need a variable that remembers the best so far, as
highestdid in lesson 101-08. - Delete the last print line, so the program finishes, then print all seven days, one to a line, with a loop.
one way to print the average
Put these lines before the last print line, so they run before the fault: I64 total = 0;
for day in 0..len(temps) {
total += temps[day];
}
"average: {total / len(temps)}\n";
With Wednesday corrected to 13, the week adds up to 114, and this prints `average: 16` before the fault report. Division of whole numbers drops the remainder, as in lesson 101-02.
when it goes wrong
The index can come from a variable, and cint checks it the same way when the program runs:
void main() {
I64[7] temps = [12, 15, 11, 18, 21, 19, 16];
I64 day = 7;
"the day after Sunday: {temps[day]}\n";
}
fault[E_BOUNDS]: index out of bounds
--> index.ci:4:34
|
4 | "the day after Sunday: {temps[day]}\n";
| ^ index.checked.i64
|
= operation: index.checked.i64
= operand-count: 1
= operand: I64 7
...
In C, reading past the end of an array is undefined behavior: the program can quietly give back whatever number is stored next to it, or crash. In cint, every index is checked.
The starting numbers must match the length. I64[7] temps = [12, 15]; gives two numbers for seven places, and cint refuses it with error C2011. In cint 0.2.0 an array can only be made inside a function, so this lesson's program starts from main, as lesson 101-10 explained.
new words
Back to 101: first programs.