📊 Working with Arrays
Arrays allow you to store multiple values in a single variable. Learn how to declare, manipulate, and work with arrays in iPseudo.
🎯 Array Declaration
Declare an array by specifying its size in square brackets:
pseudocode
Algorithm ArrayDeclaration # Declare array with 5 elements Var numbers[5] # Arrays use 0-based indexing # Valid indices: 0, 1, 2, 3, 4 Endalgorithm
✏️ Array Assignment
Assign values to specific array elements:
Algorithm ArrayAssignment Var items[3] # Assign values items[0] = 10 items[1] = 20 items[2] = 30 # Using Input items[1] = Input "Enter value:" Print items[0] Endalgorithm
Bounds Checking:
iPseudo automatically checks array bounds. Accessing an invalid index will throw an error!
📏 Size() Function
Get the size of an array using the Size()
function:
Algorithm ArraySize Var data[10] Var length = Size(data) Print "Array size:", length # Output: 10 # Use in loops For i = 0 To Size(data) - 1 Print "Index:", i Endfor Endalgorithm
🔄 Looping Through Arrays
Algorithm ArrayLoop Var scores[5] # Fill array with values For i = 0 To 4 scores[i] = (i + 1) * 10 Endfor # Display all elements For i = 0 To Size(scores) - 1 Print "Score", i + 1, ":", scores[i] Endfor Endalgorithm
💡 Complete Example
pseudocode
Algorithm StudentGrades # Declare array for 5 students Var grades[5] Var total = 0 # Input grades For i = 0 To Size(grades) - 1 grades[i] = Input "Enter grade for student " + (i + 1) total = total + grades[i] Endfor # Calculate average Var average = total / Size(grades) # Display results Print "All Grades:" For i = 0 To Size(grades) - 1 Print "Student", i + 1, ":", grades[i] Endfor Print "Average:", average Endalgorithm