Arrays
Create arrays, iterate over them, index elements, and pass arrays to typed functions.
Core concepts
array[int], indexing, for, let mut, function parameters
How the learner should approach this project
Do not paste the complete program immediately. Create the file, type the first step, run vpp check, and then run the program when a complete entry point exists. Read the explanation before looking at the snippet. After each step, predict what the new code should do. This turns the page into a lesson rather than a code dump.
Step 1: Sum an array
An array type describes the element type inside square brackets. The sum function accepts array[int], so the compiler knows every element is an integer.
total must be mutable because each loop iteration updates it. The for loop gives n one element at a time.
After typing the snippet, identify the new names introduced by the step. Ask what each name represents, what type the compiler should infer or check, and what value should exist after the code runs.
fn sum(nums: array[int]) -> int {
let mut total = 0
for n in nums {
total = total + n
}
return total
}Step 2: Find the largest value
Indexing uses square brackets. nums[0] reads the first element and gives best its inferred integer type.
Each element is compared with best. When a larger value is found, best is replaced. This demonstrates how arrays and mutable local state work together.
Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.
fn max_of(nums: array[int]) -> int {
let mut best = nums[0]
for n in nums {
if n > best {
best = n
}
}
return best
}Step 3: Call the array functions
The array literal gives nums the type array[int]. The same array can be passed to both functions because their signatures expect the same element type.
Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.
fn main() -> int {
let nums = [3, 7, 2, 9, 4]
print(sum(nums))
print(max_of(nums))
return 0
}Complete source
fn sum(nums: array[int]) -> int {
let mut total = 0
for n in nums {
total = total + n
}
return total
}
fn max_of(nums: array[int]) -> int {
let mut best = nums[0]
for n in nums {
if n > best {
best = n
}
}
return best
}
fn main() -> int {
let nums = [3, 7, 2, 9, 4]
print(sum(nums))
print(max_of(nums))
return 0
}Expected behavior
The complete program should produce the following output when run with the command shown below. Exact formatting should follow the current V++ runtime.
25 9
Run the project
vpp run projects/05-arrays/main.vppWhat the learner should understand after this project
Arrays provide ordered collections of values with one element type. Iteration and indexing are two different ways to access that collection.
Common mistakes to teach
- Indexing beyond the available elements
- Passing an array with the wrong element type
- Trying to update a non mutable accumulator
Practice extension
Write a function that returns the first array element and another that counts how many values are greater than 5.
A strong learner should be able to explain the program without looking at the code, rebuild the core idea from memory, and make the practice change without copying a solution.
Full program
fn sum(nums: array[int]) -> int {
let mut total = 0
for n in nums {
total = total + n
}
return total
}
fn max_of(nums: array[int]) -> int {
let mut best = nums[0]
for n in nums {
if n > best {
best = n
}
}
return best
}
fn main() -> int {
let nums = [3, 7, 2, 9, 4]
print(sum(nums))
print(max_of(nums))
return 0
}