0% found this document useful (0 votes)
11 views17 pages

8.2 Arrays

The document provides an overview of arrays, focusing on one-dimensional and two-dimensional arrays, their declaration, and how to populate and access their elements using programming concepts. It includes examples in Python and emphasizes the differences between arrays and Python lists. Additionally, it contains activities for practical application and homework assignments for further learning.

Uploaded by

frinekine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

8.2 Arrays

The document provides an overview of arrays, focusing on one-dimensional and two-dimensional arrays, their declaration, and how to populate and access their elements using programming concepts. It includes examples in Python and emphasizes the differences between arrays and Python lists. Additionally, it contains activities for practical application and homework assignments for further learning.

Uploaded by

frinekine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ARRAYS

BY: Mohamed Salb


Aims
To learn about arrays: including
• one- and two-dimensional arrays
• use of indexes
• use of iteration for reading from and writing to arrays
What is a list?
Introduction
• array– a data structure containing several elements of the same data type; these
elements can be accessed using the same identifier name.
• The position of each element in an array is identified using the array’s index.
• In this way, lists of items can be stored, searched and put into an order.
• The first element of an array can have an index of zero or one.
• Forexample, a list of names can be ordered alphabetically, or a list of temperatures
can be searched to find a particular value.
• Arrays can be one-dimensional or multi-dimensional
One-dimensional arrays
• A one-dimensional array can be referred to as a list.
Here is an example of a list with 10 elements in it
where the first element has an index of zero.
• When a one-dimensional array is declared in
pseudocode:
• the name of the array, the first, last index value and
the data type are included.
• For example, to declare a new array called MyList:
• Each position in the array can be populated in an
array by defining the value at each index position.
For instance, we can add the number 27 to the
fourth position in the array MyList as follows:
One-dimensional arrays
• To populate the entire array instead we can use a loop:
• Notice that in this code we have used the variable Counter as the array index.

• We can display the data that lies in a particular location in an array as follows:

• This would display the value 19.


• Arrays can also be populated as Array Language
they are declared. myList = [27, 19, 36, 42, 16, 89, 21, Python
16, 55, 72]
Activity 8.22
In your chosen programming language write a short
program to declare and populate the array MyList,
as shown in the Figure below , using a FOR loop.
Activity 8.22
Python
#array with nothing
MyList = []
#loop to enter the numbers to the array
for Counter in range(10):
MyList.append(int(input("Enter your number:")))

#outputing the list


print(MyList)
print(MyList[5])
Two-dimensional arrays
• A two-dimensional array can be referred to as
a table, with rows and columns. Here is an
example of a table with 10 rows and 3
columns, which contains 30 elements. The
first element is located at position 0,0.
When a two-dimensional array is declared in
pseudocode:
• the first and the last index value for rows
• the first and the last index value for columns
• and the data type
are included.
Two-dimensional arrays
• Declaring 2D array in pseudocode For example:

• The declared array can then be populated using a loop, just like for one-dimensional
arrays – however this time there need to be two nested loops, one for each index:

• We can display the data that lies in a particular location in a 2D array as follows:
The output will be 98
Note!!!
Instead of arrays, Python uses another object called
a list. The differences you need to know are:
• a list can contain different data types whereas
arrays must all hold the same type of data
• to achieve the same structure as a two-dimensional
array, Python embeds lists within another.
Two-dimensional arrays
• Two-dimensional arrays are populated in python as follows:
Activity 8.23
In python, write a program to declare and populate
the array MyTable, as shown in the Figure below,
using a nested FOR loop.
Activity 8.23
Python
#array with nothing print(MyList)
MyList = [] print(MyList[5,5])
#loop to enter the numbers to the array
for Counter1 in range(3):
Column = []
for Counter2 in range(10):
Column.append(int(input("Enter your number:")))
MyList.append(Column)

#outputing the list


What is an array?
Homework
Workbook 2 Ch 8
Questions: 12,14,15
Pages: 48-49 and 50-54
Any Questions?

You might also like