arrays
arrays
An array is a collection of elements, all of which must be of the same data type (e.g.,
integers, strings, etc.). The elements in the array are stored under a single identifier, and
each element can be accessed using an index (or position) within the array.
An array allows you to store multiple values, but unlike variables, which hold a single
value, an array can hold many values. You use an array when you need to store and
manage a collection of data.
For example:
• If you want to store multiple integers, you can use an array. If you want to store
multiple colors as strings, you can use an array for strings.
2. One-Dimensional Arrays
A 1-dimensional array is the simplest type of array. It has only one "row" of data, which
can be thought of as a single list of items.
For example:
Index: 0 1 2 3 4
Data: [10, 5, 2, 90, 3]
This array has 5 spaces (or slots), each identified by an index. The index starts from 0.
The first item, 10, is stored at index 0, the second item, 5, is stored at index 1, and so
on.
To access an element from an array, you use its index inside square brackets.
• Array[0] gives you the first element.
• Array[3] gives you the fourth element (since indexes start from 0).
For example:
You store data in an array by specifying the array name and the index (position) where
you want to store the data.
To access and display a value from an array, you use its index.
Instead of directly using a number to access an array, you can use a variable. The value
of the variable will determine which element of the array is accessed.
For example:
If you want to store multiple numbers in an array, you can use a loop to input those
numbers from the user.
FOR counter = 0 TO 19
OUTPUT("Enter a number:")
INPUT Numbers[counter] // Store each number in the Numbers
array at position 'counter'
NEXT counter
• Indexing: The index of an array is the position of each element. The index
typically starts at 0 but can start at 1 in some languages.
• Array Elements: All elements in the array must be of the same data type (e.g., all
integers, all strings, etc.).
• Array Length: The size of the array is determined by the number of elements it
can hold. This is called the length of the array.
• Accessing: To access an element, use the array name and the index inside
square brackets.
• Updating Elements: You can update an element in the array by assigning a new
value to an index.
1. Exercise 1: Create an array named Scores and store 5 integer values in it (e.g.,
85, 90, 75, 88, 92). Then, output the second and fourth elements in the array.
2. Exercise 2: Write a program that stores 10 numbers input by the user into an
array and then outputs the sum of all the numbers.
3. Exercise 3: Create an array of 5 strings (e.g., ["apple", "banana", "cherry", "date",
"elderberry"]). Ask the user to input a number between 0 and 4, and output the
corresponding fruit from the array based on the number.
4. Exercise 4: Write a program where the user enters 5 numbers into an array.
Then, output the largest number from the array.
Exercise 1:
Task: Create an array named Scores and store 5 integer values in it (e.g., 85, 90, 75, 88,
92). Then, output the second and fourth elements in the array.
Solution:
// Output the second element (index 1) and the fourth element (index
3)
OUTPUT(Scores[1]) // Outputs: 90
OUTPUT(Scores[3]) // Outputs: 88
Exercise 2:
Task: Write a program that stores 10 numbers input by the user into an array and then
outputs the sum of all the numbers.
Solution:
// Ask the user to input 10 numbers and store them in the array
FOR counter = 0 TO 9
OUTPUT("Enter a number:")
INPUT Numbers[counter]
NEXT counter
Explanation:
• We use a FOR loop to input 10 numbers from the user and store them in the array
Numbers.
• Then, we use another FOR loop to add all the numbers together and store the
result in the Sum variable.
• Finally, the sum is displayed.
Exercise 3:
Solution:
Explanation:
• The array Fruits contains 5 strings.
• We ask the user to input a number between 0 and 4.
• If the number is within range, we output the corresponding fruit from the array.
Otherwise, we show an error message.
Exercise 4:
Task: Write a program where the user enters 5 numbers into an array. Then, output the
largest number from the array.
Solution:
// Ask the user to input 5 numbers and store them in the array
FOR counter = 0 TO 4
OUTPUT("Enter a number:")
INPUT Numbers[counter]
NEXT counter
Explanation: