Array CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
easyAn array `scores` contains [85, 92, 78, 95, 88]. What is scores[0]? What is scores[3]? How many elements are there?
Solution
- 1 Step 1: Arrays use zero-based indexing: scores[0] = 85.
- 2 Step 2: scores[3] = 95 (the 4th element).
- 3 Step 3: The array has 5 elements (length = 5).
Answer
scores[0] = 85, scores[3] = 95, length = 5.
Arrays store multiple values in a single variable, accessed by index. Most programming languages use zero-based indexing, so the first element is at index 0.
About Array
An ordered collection of values stored together under a single name and accessed by their numeric index position. Arrays allow you to store, retrieve, and manipulate multiple related values efficiently using loops and index-based access.
Learn more about Array โMore Array Examples
Example 2 medium
Write pseudocode to find the sum of all elements in an array `nums = [4, 7, 2, 9]`.
Example 3 mediumGiven array `names = ['Alice', 'Bob', 'Charlie']`, what is names[1]? What happens if you access name
Example 4 mediumGiven `scores = [12, 15, 9, 20]`, what are `scores[0]` and `scores[3]`, and what happens if code tri