2025/04/24
Introduction to Arrays
Lecturer:
Ms Marinda Taljaard / Mr Ighsaan Salie / Mr Ntembeko Jafta
Admin – test 2 details
▪ Test 2 is scheduled for Friday 25 April – book for a session on the module site
• Session 1: 12:00 – 13:30
• Session 2: 14:00 – 15:30
• Session 3: 16:00 – 17:30
• Booking to be available from Wednesday 16 April – Wednesday 23 April (14:00)
▪ Content
• All work covered this semester, including user defined methods
• Only coding questions
▪ Check previous slides to ensure that you are familiar with the test process
1
2025/04/24
Scenario to consider
▪ Write a program that asks the user for 10 integers, calculates the average of the
numbers, and displays the numbers and the average.
• Would you be able to use a loop, and reads each number into a variable, e.g. num1?
• Then determine and display the average?
▪ What about the part of the question that stated that the final display should re-
display the numbers with the average?
• Do we need to create 10 different variables to be able to do this (e.g. num1, num2….
num10)?
▪ A good solution is to make use of an array – basically a list (data structure) that
can contain a number of values
Arrays
▪ An array is a list of data elements that all have the same data type
▪ Arrays are often named with the word list at the end, and often plural
• E.g. sales, salesList, numList
• Array numList below has space for 7 elements – which should all be of the same type
numList
2
2025/04/24
Arrays - terminology
▪ Array element – each individual item in the array
▪ Subscript / index – distinguish each element from the others, using the subscript/index
• Indicates the position of the element in the array – the first element is always in position 0
• Use integer inside square brackets [ ] to indicate the position (e.g. numList[1] = 35)
▪ Size (the maximum number of elements - in this example size = 7)
▪ Number of elements (nrEl) – how many elements currently in the array (in this example
number of elements = 4), for a full array size = number of elements
numList
12 35 8 41
Position 0 Position 3 Position 6
Declaring an Array
▪ Two-step process
• const int SIZE = 7 //max number of elements – ideal job for constant
• int[] numList //declaring the array variable
- numList is declared as an array of integers
• numlist = new int[SIZE] //reserve memory for 7 elements
▪ Keyword new - reserves memory for separate numList elements (in this case 7)
▪ int[] numList = new int[SIZE] //two-step process in single
line
numList
3
2025/04/24
Initializing an Array
▪ Default values will be allocated to the array elements, based on the type of the array
▪ When an array is created (without immediately providing values), values are allocated,
according to the following
• Numeric fields are set to 0
• Character fields are set to null
• Boolean fields are set to false
▪ Can assign non-default values to array elements at declaration (different ways possible),
e.g.
• int[] numList = new int[7] {12, 8, 35, 41, 25, 34, 45};
• int[] numList = new int[] {12, 8, 35, 41, 25, 34, 45};
• int[] numList = {12, 8, 35, 41, 25, 34, 45};
Accessing Array elements
▪ For a regular double or int variable (e.g. num1) you could say:
• num1 = num1 + 10;
▪ When using arrays you get to the specific value by using the index (position), e.g. based on
the example below:
• numList[0] contains the value 12 and
• numList[3] contains the value 41
▪ You can use array elements as if they were individual variables, e.g.
• numList[3] = numList[3] + 10;
(after this instruction, the value of numList[3] will be 51)
12 35 8 41
4
2025/04/24
Accessing Array elements
▪ Shortcut instructions can also be used , e.g.
• numList[3] = numList[3] + 10;
• numList[3] += 10; //shortcut instruction to increase value by 10
▪ A value at a specific index can be used in a WriteLine statement
WriteLine(“The number is {0}”, numList[2]);
Accessing Array elements
▪ It is often required to work through all the elements in an array –
• Traverse the array to process the numbers, or maybe display them
12 35 8 41
▪ Use a loop to add values into an array / to process the array elements
for (int counter = 0; counter <=6; counter++)
numList[counter] = numList[counter] + 10;
(The array is traversed, and every value is increased by 10, remember that the first element in the
array is at position 0)
What would you expect the values in the array to be after this for loop was executed?
5
2025/04/24
Working with Array elements
▪ Using the Length property
▪ Finds the total number of elements present in an array
• [Link] – gives a value that can be used in other statements
Example 1
▪Consider the following code -
what is this code doing?
6
2025/04/24
Example 2
Write a program named ArrayDemo that stores
an array of 10 integers. Until the user enters a
sentinel value, allow the user four options:
(1) to view the list in order from the first to the
last position
(2) to view the list in order from the last to the
first position
(3) to choose a specific position to view, or
(4) to quit the application
In order to get to the final solution – let us plan the
steps required…
Example 2 – Menu driven program
▪ Switch
• Menu driven program
▪ While
• Allows the user to continuously make choices
from the available options
▪ Initially attempt the solution without using
Methods
7
2025/04/24
Example 2 – step by step discussion
▪ Write a program that stores an array of 10 integers
• Declare an array of 10 integers
• You can initialise the array by randomly just picking 10 integers.
Example 2 – step by step discussion
▪ How will we display numbers in order from the first position to the last position (not
sorted – position order).
• Use a loop to traverse the array (from position 0 to ?)
- Since you know how many elements, it is easy to use a for loop to do this
• Need a Write or WriteLine in the body of the loop
• Start with a index value of 0
• Increment with 1 each in each iteration
• When to stop?
8
2025/04/24
Example 2 – step by step discussion
▪ How will we display numbers in order from the last position to the first position (not
sorted).
• Use a loop to traverse the array (from position 9 to 0)
- Why not start from 10?
- Can we calculate the starting value somehow (with what we know), instead of typing the
actual 9?
• Need a Write or WriteLine in the body of the loop
• Start with a index value of 9
• Decrement with 1 each in each iteration
• When to stop?
Example 2 – step by step discussion
▪ Prompt the user to specify a specific position in the array - then display the
element in that position
• Prompt the user for a position in the array
- If required, how can we check this value for validity?
• Do we need to traverse the array?
- NO!
- Use the position value as the index, and get to the value immediately
(e.g. numberList[pos])
9
2025/04/24
Example 2 – Get started
Write a program that stores an array of 10 integers – you can initialise the array
by randomly just picking 10 integers.
▪ Initialise an array with any 10 integers
Setup menu display and selection
Provide functionality in the relevant section of the switch statement
Example 2 –
a solution
▪ Initialise array
▪ Setup menu display and
selection
▪ Process user selection
▪ Provide functionality in the
relevant section of the
switch statement
10
2025/04/24
Example 2 – a solution (continued)
▪ Available menu options and selection handled in a user defined method
Example 2 – another viewpoint
▪ Create methods for each of the functionality options
• This would make the switch statement (and main method) much shorter
11
2025/04/24
Arrays and Methods
▪ Want to send through a single array element?
▪ Consider the following method declaration:
static void displayElement(int num)
▪ Which can be called with a method call similar to:
displayElement(numbers[x])
//x must have a value to identify the specific position of
the element in the array
numbers is an array of integers, so
numbers[x] contains an integer value, as
long as x is a valid index for the array
▪
Arrays and Methods
▪ Want to send through the complete array?
▪ Consider the following method declaration:
static void displayElement(int[] nums)
▪ Which can be called with a method call similar to:
displayElement(numbers)
▪ Arrays are passed by reference (without specifying the keyword ref)
• The method receives the actual memory address of the array (and not a copy of the
array in a new memory location)
• If any changes are made to array elements the calling method will receive those
changes
12
2025/04/24
Example 2, using methods
Working with Arrays
▪ Declare
▪ Possibly initialise (not referring to the default values)
▪ Add values into (add array elements)
▪ Traverse array to display values
▪ Traverse array to update values
▪ Search for a specific element in an array
▪ Delete an element in an array (not discussed in this lecture)
▪ Important to know the size of the array, and the number of elements in the array
Elements 32 16 45 25 9 17
Position 0 1 2 3 4 5 6
13
2025/04/24
Traversing an array
▪ Use a loop to work through the array (e.g. for or
while loop)
▪ New looping structure for arrays: foreach
• Uses a temporary iteration variable that
automatically holds each array value in turn
• No index specified, automatically runs through the
whole array
• Important things to remember about foreach:
- Will access every element
- Can not make changes to elements – read-only structure
(no assignment of values)
Find an element in an array
▪ Create a method that accepts an array, the number of elements in the array, and
the value (element that we are looking for)
• Method returns an integer
- Position of the element in the array
- Otherwise (if not found) returns a value of -1
▪ E.g. See if the array contains a 45, and return the position of the element in the
array, otherwise return a -1
32 16 45 25 9 17
▪ Use loop to traverse the array; checking the elements in the array
14
2025/04/24
Find an element in an array
▪ Traverse the array, checking
the elements in the array
▪ If the value is found, we can
immediately terminate the
loop, and return the position
of the element
On the module to-do list
▪ View the available videos:
• Arrays – Introduction
• Adding elements to an Array (this one also shows methods that uses an array)
• Arrays – Finding an element
▪ Work through the examples in these slides again to help with the understanding of how to
work with arrays
▪ Test on 25 April
• Check your lab allocation before you come to the Embizweni building, it will be available on
the module site on Friday (mid-morning)
• Bring your student card (ID/driver’s licence/Passport), and pen & pencil
15
2025/04/24
Any questions
Last slide
▪ Time to pack up and go
▪ See you another time
▪ ☺
16