Arrays are collections that store multiple values of the same type under a single variable name. Instead of creating separate variables for each item, you can store them all in one array and access them by index.
score1, score2, score3... score100
(100 variables!)
scores[100]
(One array, 100 values!)
# Declare array with size
var arrayName[size]
# Declare and initialize
var numbers = [10, 20, 30, 40, 50]
Algorithm ArrayBasics
# Declare an array for 5 scores
var scores[5]
# Assign values to array elements
scores[0] = 85
scores[1] = 92
scores[2] = 78
scores[3] = 95
scores[4] = 88
# Access and display values
Print "First score:", scores[0]
Print "Third score:", scores[2]
Print "Last score:", scores[4]
Endalgorithm
array[0]
array[1]
array[2]
array[4]
Arrays and loops work perfectly together:
Algorithm ArrayLoop
var numbers[5]
# Fill array with values
Print "Filling array..."
For i = 0 To 4
numbers[i] = (i + 1) * 10
Print "numbers[", i, "] =", numbers[i]
Endfor
# Calculate sum
var sum = 0
For i = 0 To 4
sum = sum + numbers[i]
Endfor
Print ""
Print "Sum of all elements:", sum
Print "Average:", sum / 5
Endalgorithm
Use Size()
to get the number of elements in an array:
Algorithm ArraySize
var scores = [85, 92, 78, 95, 88]
var arraySize = Size(scores)
Print "Array size:", arraySize
# Loop through array using Size()
Print ""
Print "All scores:"
For i = 0 To Size(scores) - 1
Print "Score", i + 1, ":", scores[i]
Endfor
Endalgorithm
Algorithm FindMaximum
var numbers = [45, 23, 89, 12, 67, 34]
var max = numbers[0] # Start with first element
var maxIndex = 0
For i = 1 To Size(numbers) - 1
If numbers[i] > max Then
max = numbers[i]
maxIndex = i
Endif
Endfor
Print "Maximum value:", max
Print "Found at index:", maxIndex
Endalgorithm
Algorithm FindMinimum
var temperatures = [22, 18, 25, 15, 30, 27]
var min = temperatures[0]
For i = 1 To Size(temperatures) - 1
If temperatures[i] < min Then
min = temperatures[i]
Endif
Endfor
Print "Minimum temperature:", min, "°C"
Endalgorithm
Algorithm SearchArray
var numbers = [10, 25, 33, 47, 52, 68]
var searchValue = Input "Enter value to search for:"
var found = false
var position = -1
For i = 0 To Size(numbers) - 1
If numbers[i] == searchValue Then
found = true
position = i
Endif
Endfor
If found == true Then
Print "✅ Found", searchValue, "at index", position
Else
Print "❌ Value not found"
Endif
Endalgorithm
Algorithm ReverseArray
var original = [1, 2, 3, 4, 5]
var size = Size(original)
Print "Original array:"
For i = 0 To size - 1
Print original[i]
Endfor
Print ""
Print "Reversed array:"
For i = size - 1 To 0 Step -1
Print original[i]
Endfor
Endalgorithm
Algorithm GradeManager
var studentCount = Input "How many students?"
var grades[studentCount]
# Input grades
Print ""
Print "Enter grades:"
For i = 0 To studentCount - 1
grades[i] = Input "Student " + (i + 1) + ":"
Endfor
# Calculate average
var total = 0
For i = 0 To studentCount - 1
total = total + grades[i]
Endfor
var average = total / studentCount
# Find highest and lowest
var highest = grades[0]
var lowest = grades[0]
For i = 1 To studentCount - 1
If grades[i] > highest Then
highest = grades[i]
Endif
If grades[i] < lowest Then
lowest = grades[i]
Endif
Endfor
# Display statistics
Print ""
Print "=== GRADE STATISTICS ==="
Print "Class Average:", average
Print "Highest Grade:", highest
Print "Lowest Grade:", lowest
Print "========================"
Endalgorithm
Algorithm TemperatureTracker
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var temps[7]
Print "=== TEMPERATURE TRACKER ==="
Print ""
# Input temperatures
For i = 0 To 6
temps[i] = Input days[i] + " temperature (°C):"
Endfor
# Calculate weekly average
var sum = 0
For i = 0 To 6
sum = sum + temps[i]
Endfor
var weeklyAvg = sum / 7
# Find warmest day
var warmestTemp = temps[0]
var warmestDay = 0
For i = 1 To 6
If temps[i] > warmestTemp Then
warmestTemp = temps[i]
warmestDay = i
Endif
Endfor
# Display report
Print ""
Print "=== WEEKLY REPORT ==="
Print "Average temperature:", weeklyAvg, "°C"
Print "Warmest day:", days[warmestDay]
Print "Warmest temperature:", warmestTemp, "°C"
Endalgorithm
❌ Wrong (array has 5 elements, 0-4):
var arr[5]
Print arr[5] # Error! Index 5 doesn't exist
✅ Correct:
var arr[5]
Print arr[4] # Last element
Size()
to get array length