An array is a collection of data that holds a fixed number of values of the same type.
In order to use a one dimensional array in a computer program, you need to consider:
what the array is going to be used for, so it can be given a meaningful name.
how many items are going to be stored, so the size of the array can be determined.
what sort of data is to be stored, so that the array can be the appropriate data type.
DECLARE <identifier> : ARRAY[<lower bound>:<upper bound>] OF <data
type>
Note: the <lower bound> can start in 0 or 1.
Example
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE <identifier> : ARRAY[<lowerbound1>:<upperbound1>,<lowerbound2>:<upperbound2>] OF <data
type>
Note: the <lower bounds> can start in 0 or 1.
Example
DECLARE NoughtsAndCrosses : ARRAY[1:4,1:3] OF CHAR
Example 1
StudentNames[1] ← "Ali"
The pseudocode means that the StudentNames 1D array at index 1 will get the value of "Ali"
Example 2
NoughtsAndCrosses[2,3] ← ꞌXꞌ
The pseudocode means that the NoughtsAndCrosses 2D array at index 2,3 will get the value of 'X'
Example 3
n <- 5
StudentNames[n+1] ← StudentNames[n]
Here, as you can see the index of the StudentNames 1D array is n+1, you need to calculate this index. In this case n+1=6 since the value of n is 5.
So, in the second line of the pseudocode we say that StudentNames[n+1] is the same than StudentNames[6]. Therefore, StudentNames[6] is getting the value that StudentNames[5] contains.
The following pseudocode declares a 1D array called my_music that will contain seven elements, and then asks the user for seven instruments to input into the array.
DECLARE my_music : ARRAY[0 : 6] OF STRING
FOR index <- 0 TO 6
INPUT element
my_music[index] <- element
NEXT index
This is the equivalent Python code.
my_music = []
for index in range(0,6,1):
element = input("Enter a musical instrument: ")
my_music.append(element)
Symbol
The following pseudocode declares a 2D array called Symbol (see figure above), asks the user to enter symbols, and then inputs these elements into the 2D array Symbol.
DECLARE Symbol: ARRAY [0 : 4, 0 : 3] OF CHAR
FOR row <- 0 TO 4
FOR column <- 0 TO 3
INPUT element
Symbol[row, column] <- element
NEXT column
NEXT row
In Python there isn't a structure that represents 2D arrays, that is why we will use a 1D array, and this 1D array will contain other 1D arrays inside it. This is the equivalent Python code.
Symbol = [] # a 1D array that will represent the 2D array
row = [] //a 1D array used to store each row of the 2D array.
for row in range(0,4,1):
for column in range(0,3,1):
element = input("Enter a symbol: ")
row.append(element)
Symbol. Append(row)
row = []
my_music = ["maraca", "'guitar", "piano", "flute", "clarinet", "trumpet", "cello"]
The following pseudocode outputs on screen the elements from the 1D array my_music.
FOR index <- 0 TO 6
OUTPUT my_music[index]
NEXT index
This is the equivalent Python code.
for index in range(0,6,1):
print(my_music[index])
The following pseudocode outputs on screen the elements from the 2D array Symbols.
FOR row <- 0 TO 4
FOR column <- 0 to 3
OUTPUT Symbol[row, column]
NEXT column
NEXT row
This is the equivalent Python code.
for row in range(0,4,1):
for column in range(0,3,1):
print(Symbol[row][column], end= " ")
print()
Given the array:
my_music = ["maraca", "'guitar", "piano", "flute", "clarinet", "trumpet", "cello"]
Output the length of the array (Remember: the length is the number of elements in an array)
Index. Output the third element in the my_music array.
Slicing arrays. Output the second, third, fourth and fifth elements of the my_music array. Use just one line of code.
Output from the fourth element to the end of the my_music array.
Appending elements. Add your favourite music instrument to the array my_music.
Having the following array add your favourite meat.
meat = ["steak", "brisket", "fillet", "solomillo"]
6.6 Having the following array add three more elements.
my_array = ["rose", "lili"]
Write values into an array using iteration.
Create an array with 5 animals using iteration.
Read values from an array using iteration.
Print the values in the array my_music in reverse order.
cello
trumpet
clarinet
flute
piano
guitar
maraca
Create an array that will contain even numbers. Call the array my_even_numbers.
Create an array that will contain positive numbers only. Give your array an appropriate name.
Having the array numbers:
numbers_array = [7, 25, 43, 44, 89, 10, 6]
Add all the numbers and output the total
Output the average of these numbers
Output the biggest number
Output the smallest number
Output the odd numbers only