0% found this document useful (0 votes)
18 views

Module 03 Array

The document discusses arrays in C++. It defines arrays as collections of variables of the same type that can be referenced with a common name. There are three types of arrays: one-dimensional, two-dimensional, and multi-dimensional arrays. Arrays allow storing and processing large numbers of variables of the same type. The document also covers initializing arrays, including unsized arrays, and how strings are treated as character arrays in C++.

Uploaded by

Charles Oxford
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Module 03 Array

The document discusses arrays in C++. It defines arrays as collections of variables of the same type that can be referenced with a common name. There are three types of arrays: one-dimensional, two-dimensional, and multi-dimensional arrays. Arrays allow storing and processing large numbers of variables of the same type. The document also covers initializing arrays, including unsized arrays, and how strings are treated as character arrays in C++.

Uploaded by

Charles Oxford
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Batangas State University The National Engineering University – Mabini Campus

MODULE 03
Arrays

CS 131: Data Structures and Algorithms Instructor : Joshua C. Abella


Batangas State University The National Engineering University – Mabini Campus

Let’s have an
Ice Breaker
Are you ready???

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Let’s Group Your Self


How well do you know your classmate?

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Group your self based


on your Attitude

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Group your self based


on your Hobbies

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Group your self based


on your Age

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Group your self based on


your Relationship Status

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

ARRAYS

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Topic Objectives
● Be introduced to arrays;
● Understand the need of arrays;
● Determine the different types of arrays;
● Initialize arrays;
● Initialize unsized arrays; and,
● Use strings as an array.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Introduction

This topic will introduce the students to


arrays, why do we need them, its types, and
how to initialize them using C++.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Introduction
An array is a collection of variables that can hold value of
the same type and reference by common name. It is a
derived data structure. Arrays are always stored in a
continuous memory location. Array data types can either
be an integer, a character, or float base type. We need
arrays to store processed large number of variables of same
data type and reference/name. With the help of arrays, we
can easily understand a program
CS 131: Data Structures and Algorithms
Batangas State University The National Engineering University – Mabini Campus

Array indexing is always beginning at zero, and the last


element is the highest address. If we have an array of nine
(9) elements, then, our indices will start from zero (0), and the
highest index is eight (8)

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

TYPES OF ARRAYS

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Types of Arrays
One – Dimension Arrays
A one- dimensional array is when one subscript/indices
specification is needed to specify a particular element of
array

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Memory representation of One-Dimensional Arrays

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Two – Dimensional Arrays


A two-dimensional array is an array which each element is
itself an array.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Memory Representation of Two-Dimensional


Arrays

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Multi – Dimensional Arrays


Multi-dimensional arrays are arrays with dimensions more
than two. Its maximum limit is dependent to the compiler.

Arrays of three or more dimensions are not often used because of


the huge memory requirement and complexity involved.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Array Initialization

C++ provides the facility of array initialization at the time of


declaration. General form of array initialization is:

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Line 6 is the initialization. As you can see, the array name is of integer data type,
has a size of 12, and has. If you will notice, I used a do-while loop to display all of
the elements of the array. The variable x is the indices. Using that code, the output
will be:

Trying it in C++, it looks like this:


CS 131: Data Structures and Algorithms
Batangas State University The National Engineering University – Mabini Campus

Two-dimensional arrays are also initialized in the same way as linear arrays. For
example,

However, this is not recommended. Instead, use this initialization:

Multi-Dimensional Arrays
CS 131: Data Structures and Algorithms
Batangas State University The National Engineering University – Mabini Campus

Multi – Dimensional Arrays


Line 4 is the initialization. The
value of x corresponds for the
number of rows, and y for the
number of columns. To access
the array elements, I used a
nested for-loop. The first for-
loop on line 5 is for accessing
the rows of the array, and the
second for-loop on line 6 is for
accessing the columns of the
array.
CS 131: Data Structures and Algorithms
Batangas State University The National Engineering University – Mabini Campus

Unsized Array Initialization


C++ allows you to skip the size of an array in its initialization statement. C++
automatically creates an array that is big enough to hold all the initializers
present. If you skip the size, you must give list of initializers so that C++ can
calculate the size of the array. For example,

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Strings as Arrays
C++ does not have a string data type, it impairments string
as a one-dimensional character array. A string as a character
array is terminated by a null character, '\0'.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Summary
● An array is a collection of variables that can hold value of the same data type and
reference by a common name.

● An array is a derived data structure.

● Indexing in array starts from zero, and the highest address corresponds to the last element.

● Arrays are needed to store processed large number of variables of the same data type and
name.

● There are three types of arrays: one-dimensional, two-dimensional, and multi-


dimensional.

● Arrays with 3 or more dimensions are not often used because of its complexity and huge memory
requirement.

CS 131: Data Structures and Algorithms


Batangas State University The National Engineering University – Mabini Campus

Summary
● In C++, the facility of array initialization at the time of declaration is provided.
● Two-dimensional arrays are initialized the same way as linear arrays.
● C++ enables you to skip the size of an array in the initialization statement. It
automatically creates an array that is big enough to hold the initializers
present.
● If the size is skipped, the initializers should be given, so that C++ can
calculate the array size.

● String data types are unavailable in C++, but it impairs a string as a


one-dimensional character array.

● A string as a character array is terminated by a null character.


CS 131: Data Structures and Algorithms
Batangas State University The National Engineering University – Mabini Campus

Thank You ☺

CS 131: Data Structures and Algorithms

You might also like