Main PDF Lesson 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

MODULE 2

ARRAYS
LESSON #1
Single Dimensional
Array
Upon completion of this subtopic, you will be able to:
• Create single dimensional arrays.
• Understand how single array holds value
• An array is a series of elements of the same type placed
in contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
• An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of
variables of the same type.
• To declare an array in C++, the programmer specifies the
type of the elements and the number of elements required
by an array as follows:
type arrayName [ arraySize ];

• This is called a single-dimension array.


The arraySize must be an integer constant greater than
zero and type can be any valid C++ data type
double balance[10];
C++ array elements either one by one or using a single
statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
or
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } are the number of


elements that we declare for the array between square
brackets [ ].
Arrays have 0 as the first index not 1. In this
example, balance[0] is the first element.

If the size of an array is n, to access the last element, (n-1) index


is used. In this example, balance[4] is the last element.
An element is accessed by indexing the array name. This is done by
placing the index of the element within square brackets after the
name of the array. For example:

balance[0] = 100;

In the given statement, we access the first index of array balance and
then assign a value of 100.
Output:
Suppose you declared an array of 10 elements. Let's say,

int testArray[10];

You can use the array members from testArray[0] to testArray[9].

If you try to access array elements outside of its bound, let's


say testArray[14], the compiler may not show any error
C++ Program to display marks of 5 students by passing
one-dimensional array to a function.
In the given function, the contents of the array are
passed into it.
The output shows all the contents of the marks array
from the main function:
• https://www.w3schools.com/cpp/cpp_arrays.asp
• https://www.w3schools.com/cpp/cpp_arrays_loop.asp
• https://www.programiz.com/cpp-programming/arrays
• http://www.cplusplus.com/doc/tutorial/arrays/
• https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
LESSON #2
Two Dimensional
Arrays
Upon completion of this subtopic, you will be able to:
• Create two dimensional arrays.
• Understand how two dimensional array holds value
In C++, you can create an array of an array known as
multi-dimensional array. For example:

int x[3][4];

Here, x is a two dimensional array. It can hold a


maximum of 12 elements.
You can think this array as table with 3 rows and each row has 4
columns as shown below.
If you want to create a two dimensional array with initial
values, you can use the following syntax:

int test[2][3] = { {2, 4, 5}, {9, 10, 11}};

The two dimensional array here is composed 2 rows and


3 columns. The first row has 2, 4 and 5 while the second
row has 9, 10 and 11
C++ Program to display the elements of two dimensional array by
passing it to a function:
Two dimensional arrays can be also used in a function as a
parameter.
The output:
• https://www.programiz.com/cpp-programming/multidimensional-
arrays
• https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_ar
rays.htm

You might also like