What is an array in programming?
An array is a data structure that stores a fixed number of values of the same data type under one name. Each element is accessed by its index, which starts at 0. Arrays are processed with loops, which makes standard algorithms such as finding the maximum easy to code. Example: marks = [62, 75, 58, 91, 70]. Find the maximum. Set max to marks[0] = 62 Compare: 75 > 62, so max = 75 58 is not larger; 91 > 75, so max = 91 70 is not larger Answer: The maximum is 91.
In SQA Higher Computing Science (Software design and development), you use 1-D arrays, records and arrays of records, and implement standard algorithms such as find minimum or maximum, count occurrences and linear search. A fixed loop over the array's indices visits every element in turn.
Worked example
For marks = [62, 75, 58, 91, 70], use the find maximum algorithm.
- Set max to the first element: max = 62.
- Index 1: 75 > 62, so max = 75.
- Index 2: 58 is not greater than 75, so no change.
- Index 3: 91 > 75, so max = 91.
- Index 4: 70 is not greater than 91, so the loop ends.
Answer: The maximum mark is 91.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre