0% found this document useful (0 votes)
26 views27 pages

1D Arrays - 1

Uploaded by

19465dxbp
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)
26 views27 pages

1D Arrays - 1

Uploaded by

19465dxbp
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/ 27

Data Structures-

1
1D Arrays
LO: Declare and use one-dimensional (1D) .
Keywords: Array index, Upper bound, Lower bound
CCL: Maths

LO: To declare and use 1D arrays


Starter 2

Identify the images.

LO: To declare and use 1D arrays


Arrays 3
• An array is 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.

Array index: row or column number of an individual array


element
Upper bound: the highest index number of an array
Lower bound : the lowest index number of an array
LO: To declare and use 1D arrays
Advantages of arrays 4

01 02 03
Need of a Continuous Less
single memory processing
variable for locations time.
multiple are
data items available.

LO: To declare and use 1D arrays


Types of arrays
1D array 5

• One dimensional
(1D) array
• Two dimensional
(2D) array

LO: To declare and use 1D arrays


1D Arrays 6

• A One-Dimensional Array is the simplest form of an Array in


which the elements are stored linearly and can be
accessed individually by specifying the index value of
each element stored in the array.
• A One-Dimensional Array is a group of elements having the
same data type which are stored in a linear arrangement
under a single variable name.

LO: To declare and use 1D arrays


1-dimensional arrays 7

• A 1-dimensional array has just one row of data.


• The best way to visualise an array is with a table:
Index 0 1 2 3 4
Data 10 5 90 26 87

• This array has 5 spaces. Each space has an index. In this


array the first data item is in position 0, the data value is 10.
• Arrays can be 0-indexed or I-indexed.
• MyData[3] is accessing the fourth element in the array
named MyData.
LO: To declare and use 1D arrays
1D Array Declaration 8

LO: To declare and use 1D arrays


1D Array-Task 9

• Implement input, output, process for a 1D array named


ARR.

• Add 1 to all the elements of the array

LO: To declare and use 1D arrays


1D Array-Task Sol 10

• Implement input, output, process for a 1D array named ARR.

Input Output Process


FOR index 1 TO 5 FOR index 1 TO 5
FOR index 1 TO 5 OUTPUT PROCESS
INPUT ARR[index] ARR[index] ARR[index]
NEXT index NEXT index NEXT index

• Add 1 to all the elements of the array

Sol:
FOR index 1 TO 5
ARR[index] ARR[index] +1
NEXT index
LO: To declare and use 1D arrays
Putting data into an array(Population of
array) 11
Example 1
• Store the colour "red" in the first position of the array named Colour:
• Colour [O]  “red”

• Example 2
• Store the colour "yellow" in the second position of the array named Colour:
• Colour[1]  “yellow”

• Example 3
• Store the number 20 in the sixth position of the array named Numbers:
• Numbers[5]  20
LO: To declare and use 1D arrays
Getting data out of an array 12
Example 1
• Output the first value in the array Colours:
• OUTPUT(Colours[0])

Example 2
Store the second value in the array Colours in a variable:
TheColour  Colours[1]

Example 3
Add 10 to the third value in the array Numbers:
Value
LO:  10and
To declare + Numbers[2]
use 1D arrays
Using iteration to read and write 13

Example 1
Output all 10 elements in the array Colours:
FOR Count  0 TO 9
OUTPUT(Colours[Count])
NEXT Count
Example 2
Ask the user to input 20 numbers and store each in the array Numbers:
FOR Counter  O TO 19
OUTPUT ("Enter a number")
INPUT Numbers[Counter]
NEXT Counter
LO: To declare and use 1D arrays
Data types 14

LO: To declare and use 1D arrays


Example 3 15

• Searching the values in the array “Values” that has 50


values, for the data input by the user: If found then output
“Found it”.
• You need to declare the array.
• Let the variable name be ValueToFind.

LO: To declare and use 1D arrays


Example 3-SOl 16

Searching the values in the array values that has 50 values, for the data
input by the user:
• DECLARE Values: ARRAY[0:49] OF INTEGER
• DECLARE ValueToFind, Count :INTEGER
• OUTPUT "Enter the value to find"
• INPUT ValueToFind
• FOR Count  0 TO 49
• IF Values[Counter] = ValueToFind
• THEN
• OUTPUT("Found it")
• ENDIF
• NEXT
LO: Count
To declare and use 1D arrays
TASK 17
1. Explain the difference between a variable and an array.

2. Explain why the following code will result in an error.


• MyData[O]  1
• MyData[l]  4
• MyData[2]  "7"
• MyData[3]  "9“

3. Write a program to read 10 numbers from the user into a 1-dimensional


array named MyNumbers.

4. To
LO: Write a program
declare toarrays
and use 1D add together all 100 elements in a 1-dimensional array
SOL 18

1. A variable can store 1 value whereas an array can store


multiple values.

2. Variables stores different data types whereas arrays


can only store one data type.

LO: To declare and use 1D arrays


Sol 19
3. DECLARE MyNumbers: ARRAY[0:9] OF INTEGER
DECLARE Count: INTEGER
FOR Count  0 TO 9
OUTPUT” Enter number”)
INPUT(MyNumbers[Count])
NEXT Count

4. DECLARE MyNumbers: ARRAY[0:99] OF INTEGER


DECLARE Count: INTEGER
Total 0
FOR Count  0 TO 99
Total  Total + MyNumbers[Count]
NEXT Count
LO: To declare and use 1D arrays
TASK-EXAM STYLE QS 20

1. Write a program to ask the user to enter 20 numbers.


Store each number in an array named ArrayData.

LO: To declare and use 1D arrays


Sol 21

1. One mark per bullet.


• Looping 20 times
• Inputting a number
• Storing in appropriate array index.
DECLARE ArrayData: ARRAY[0:19] OF INTEGER
DECLARE Count: INTEGER
FOR Count 0 TO 19
INPUT ArrayData[Count]
NEXT Count
LO: To declare and use 1D arrays
Task 22

• Declare an array in pseudocode with name StudentNames


and fill (assign) 5 student names to it.

LO: To declare and use 1D arrays


Task- Answer 23

DECLARE StudentNames : ARRAY [1:5] OF STRING


DECLARE StudentNames : ARRAY ["Mary", "Paul", "Ivy",
"Will", "Dan"]

LO: To declare and use 1D arrays


Task-HW 24

• In your chosen programming language write a short


program to declare and populate the array MyList, as
shown in Figure 8.10, using a FOR loop.

LO: To declare and use 1D arrays


Array in Python 25

LO: To declare and use 1D arrays


Plenary 26

• Declare an array using name and elements of your choice.

LO: To declare and use 1D arrays


Reference 27

• https://lalacomputersci.wordpress.com/arrays/
• http://www.bitsofbytes.co/arrays.html

LO: To declare and use 1D arrays

You might also like