0% found this document useful (0 votes)
3 views32 pages

Lecture 9 Save 29042025 112455am

Arrays are data structures that store related data items of the same type in consecutive memory locations, accessed using an index starting from 0. In C++, arrays can be created by specifying the data type and size, and various tasks can be performed such as calculating sums, finding maximum and minimum values, and reversing the order of elements. Multidimensional arrays, such as 2-D arrays, require two subscripts to identify elements and can represent tables of values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

Lecture 9 Save 29042025 112455am

Arrays are data structures that store related data items of the same type in consecutive memory locations, accessed using an index starting from 0. In C++, arrays can be created by specifying the data type and size, and various tasks can be performed such as calculating sums, finding maximum and minimum values, and reversing the order of elements. Multidimensional arrays, such as 2-D arrays, require two subscripts to identify elements and can represent tables of values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

ARRAYS

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy