You are on page 1of 20

ARRAYS

CS115C - COMPUTER PROGRAMMING


TOPIC OUTLINE
Introduction to Arrays

Declaring Arrays

Accessing an Array Element

Array Length

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 2


OBJECTIVES

Declare and create arrays

Access array elements

Determine the number of elements in an array

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 3


Introduction to arrays
In the previous lesson, we have discussed on how to declare different variables using
the primitive data types. In declaring variables, we often use a unique identifier or name
and a datatype. In order to use the variable, we call it by its identifier name.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 4


Introduction to arrays
As you can see, it seems like a tedious task in order to just initialize and use the variables especially
if they are used for the same purpose.
In C# and other programming languages, there is one capability wherein we can use one variable to
store a list of data and manipulate them more efficiently.
This type of variable is called an array.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 5


Introduction to arrays
An array stores multiple data items of the same datatype, in a contiguous block of
memory, divided into a number of slots.
Think of an array as a stretched variable – a location that still has one identifier name but can hold
more than one value.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 6


Introduction to arrays

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 7


Declaring Arrays

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 8


Declaring Arrays
• For example,

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 9


Declaring
Arrays
• After declaring, we must
create the array and specify
its length with a constructor
statement called
INSTANTIATION.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 10


Initializing an Array

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 11


Assigning Values to an Array
• Declaring an array does not initialize the array in the memory. When
the array variable is initialized, you can assign values to the array.
• Array is a reference type, so you need to use the new keyword to
create an instance of the array.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 12


Assigning Values to an Array
• You can assign values to individual array elements, by using the index
number.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 13


Assigning Values to an Array
• You can assign values to the array at the time of declaration.

• You can also create and initialize an array.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 14


Assigning Values to an Array
• You may also omit the size of the array.

• You can copy an array variable into another target array variable. In
such case, both the target and source point to the same memory
location.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 15


Accessing Array Elements
To access an array element, or a part of the array, you use
a number called an index or a subscript.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 16


LET US TRY
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 17
LET US TRY!
• Create an array of Strings which are initialized to the 7
days of the week. For example,
{“Monday”, “Tuesday”, “Wednesday”, }
Expected Output:

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 18


08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 19
ARRAYS
1. Create an empty array.
2. Ask the user to input the SIZE of the
array.
3. Enter any number based on the
specified length.
4. Output all the entries.
5. Output and calculate the sum and
average of even numbers.
6. Output and calculate the sum and
average of odd numbers.
7. Count the number of odd and even
numbers

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 20

You might also like