Arrays in C
Fixed-size arrays: initializing and iterating.
int sum_array(void) {
int scores[5] = {90, 82, 77, 95, 88};
int total = 0;
for (int i = 0; i < 5; i++)
total += scores[i];
int doubled[5];
for (int i = 0; i < 5; i++)
doubled[i] = scores[i] * 2;
return total + doubled[0];
}
How it works
- A braced initializer fills the array.
- One loop sums it; another fills a second array.
- Indexing with
[i]reads and writes elements.
Keywords and builtins used here
forintreturnsum_arrayvoid
The run, in numbers
- Lines
- 11
- Characters to type
- 224
- Tokens
- 94
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 63 seconds.
Step 1 of 5 in Arrays & strings, step 22 of 35 in Language basics.