Lecture 9 Save 29042025 112455am
Lecture 9 Save 29042025 112455am
ARRAYS
Arrays are data structures consisting of related data items of the same type.
An array is a consecutive group of memory locations that all have the same type. To
refer to a particular location or element in the array, we specify the name of the array
and the position number of the particular element in the array.
subscript must be an integer or integer expression (using any integral type). If a
program uses an expression as a subscript, then the program evaluates the expression
to determine the subscript.
c[ a + b ] += 2;
Create Array
In C++, we can create/declare an array by simply specifying the data type first and then
the name of the array with its size inside [] square brackets (better known as array
subscript operator).
Access Array Elements
Elements of an array can be accessed by their position (called index) in the sequence.
In C, indexes of an array starts from 0 instead of 1. We just have to pass this index
inside the [] square brackets with the array name as shown:
Update Array Elements
EXAMPLE 2: TAKE INPUTS FROM USER AND
STORE THEM IN AN ARRAY
COMPUTING THE SUM OF THE ELEMENTS
OF AN ARRAY
TASK:
Create an array of 5 elements. Take input from the
user for each element. Then, calculate and display
the sum of the 1st, 3rd, and 5th elements of the
array.
TASK:
Take 5 inputs in an array. Find and display the
maximum and minimum element from the
array.
TASK
Take 5 inputs from the user and store them in
an array. Then display the array in reverse
order (from last to first).
TASK
Write a C++ Program that will store n even number in an array where
n is entered by the user.
MULTIDIMENSIONAL ARRAY
Arrays with two dimensions (i.e., subscripts) often represent tables of values
consisting of information arranged in rows and columns.
To identify a particular table element, we must specify two subscripts.
By convention, the first identifies the element’s row and the second identifies
the element’s column.
Arrays that require two subscripts to identify a particular element are called
two-dimensional arrays or 2-D arrays.
Arrays with two or more dimensions are known as multidimensional arrays
and can have more than two dimensions.
Figure 7.20 illustrates a two-dimensional array, a. The array contains three
rows and four columns, so it’s said to be a 3-by-4 array. In general, an array
with m rows and n columns is called an m-by-n array.
TASK: INITIALIZE AND
DISPLAY ONE 2X2 MATRIX
TASK 1: INITIALIZE AND
DISPLAY A 3X3 MATRIX
TASK: INPUT AND DISPLAY A
2X3 MATRIX
EXPECTED OUTPUT: