Introduction to
Arrays
Presenter: Mrs. McCallum-Rodney
Introduction
• Variables hold ONE value at a given time
• Arrays hold MULTIPLE values of the same type under one name.
WHAT ARE ARRAYS?
• An array is a set of data of the same type grouped together and referred to by one name.
• Any collection of homogeneous data (data of the same type) is well-suited for storage in arrays.
WHY USE ARRAYS?
• Arrays provide a mechanism for declaring and accessing several data items with only one identifier, thereby simplifying the task of data management.
• As programs become larger and more complicated, it becomes increasingly difficult to manage the data. Variable names typically become longer to ensure their uniqueness. And, the number of variable names makes it difficult for the programmer to concentrate on the more important task of correct coding.
Real life example of an array
A motel with rooms. It has a collection of rooms, each with its unique number.
Let’s play CHARADEs
Rules:
1.Go into your major groups create a list of 10 real life arrays.
2.Each group will choose a representative to act out (no talking or writing) their real life array. The first group to get it correct in one minute will get a point ; the presenting group will also receive a point if they could guide a group to the right answer.
3.No group can present on array already guessed correctly.
4.If the answer is not an array, the presenting group will lose a point.
END OF
LESSON ONE
LESSON TWO
Structure of Arrays
STRUCTURE OF ONE –DIMENSIONAL ARRAYS?
• If you will need to store 10 integer numbers and still have access to all numbers entered, then entering the numbers into an array will be appropriate.
• Consider the following array:
Number
11 2 51 1 19 71 -11 87 43 16
STRUCTURE OF ONE –DIMENSIONAL ARRAYS?
Number
Values 11 2 51 1 19 71 -11 87 43 16
• How many elements are in the array? 10
Each element has a number assigned to it – this is known as the SUBSCRIPT. The subscript identifies each item in the array.
If you need to access values from the array, you will need to state the name of the array and the index (subscript) number. The same goes for putting data into the array.
Subscript 1 2 3 4 5 6 7 8 9 10
UNDERSTANDING ARRAYS?
Number
Values 11 28 51 1 19 71 -11 87 43 16
Subscript 1 2 3 4 5 6 7 8 9 10
• What is the value in the 4th element of the array?
• What is in Number[8]?
• What is in Number[2]?
• Where is the value -11 stored?
• Where is the value 16 stored?
1
87
28
Number[7]
Number[10]
UNDERSTANDING ARRAYS?
STUDENT_NAME
Values ALTEVETA
JEWEL-ANN ALECIA ROXANNE RENEE SUZAN
Subscript 1 2 3 4 5 6
• What is the name of the array?
• How many elements are in the array?
• What is the data type for STUDENT_NAME
• What is the value in the 5th element of the array?
• What is in STUDENT_NAME[2]?
• Where is the value SUZAN stored?
STUDENT_NAME
6
string
RENEE
JEWEL-ANN
STUDENT_NAME[6]
• DO THE CROSSWORD PUZZLE ACTIVITY
DECLARING ARRAYS
• In every algorithm and program you will need to state each variable that you will be using and its data type. This is known as “DECLARING” variables.
• To declare an integer array called “Number”, to hold 10 values, you will do the following:
PSEUDOCODE
DECLARE Number AS integer array of size 10
DECLARING ARRAYS
Question 1
Declare an array called ‘teachaddr’, to hold addresses of 50 teachers.
PSEUDOCODE
DECLARE teachaddr AS string array of size 50
DECLARING ARRAYS
Question 2
Declare an array called ‘costprice’, to hold the price of 1000 items in store
PSEUDOCODE
DECLARE costprice AS real array of size 1000
DECLARING ARRAYS
Question 3
Declare an array called ‘answer’, to hold the answer of ‘Y’ or ‘N’ for 25 questions in a questionnaire.
PSEUDOCODE
DECLARE answer AS character array of size 25
ASSIGNING VALUES TO ARRAY ELEMENTS
STUDENT_NAME
Values Kerri-Ann
Allyandra
Subscript 1 2 3 4 5 6
• Place the value Kerri-Ann into third element of STUDENT_NAME.PSEUDOCODE STUDENT_NAME[3] ← “Kerri-Ann”
• Place the value Allyandra into last element of STUDENT_NAME.PSEUDOCODE STUDENT_NAME[6] ← “Allyandra”
END OF
LESSON TWO
FILLING AN ARRAY using a FOR LOOPGrades
Values 76 89 100 67 90
Subscript 1 2 3 4 5
Pseudocode segment..FOR count ← 1 TO 5
PRINT “Enter grade for student”READ Grades[count]
ENDFOR..
Question: Write an algorithm pseudocode to accept the names and ages of 100 students.
FILLING AN ARRAY using a FOR LOOP
START DECLARE count AS integerDECLARE sname AS string array of size 100DECLARE sage AS integer array of size 100
FOR count ← 1 TO 100PRINT “Enter name of student”READ sname[count]PRINT “Enter the age of student”READ sage[count]
ENDFORSTOP
PRINTING VALUES from ARRAY ELEMENTS
STUDENT_NAME
Values KRYSSAL SHANICE JODY-GAYE MARSHA SARAN AMELIA
Subscript 1 2 3 4 5 6
• PRINT the value in first element of STUDENT_NAME. PSEUDOCODE PRINT STUDENT_NAME[1]
• Print the value in the fourth element of STUDENT_NAME. PSEUDOCODE PRINT STUDENT_NAME[4]
KRYSSAL
MARSHA
PRINTING ALL VALUES in an ARRAY
STUDENT_NAME
Values KRYSSAL SHANICE JODY-GAYE MARSHA SARAN AMELIA
Subscript 1 2 3 4 5 6
KRYSSALSHANICE
Pseudocode segment..FOR count ← 1 TO 6
PRINT STUDENT_NAME[count]ENDFOR..
JODY-GAYEMARSHASARANAMELIA
The End of
Lessonon
ArraysHope you have learnt a lot.