Programming Fundamentals
in
C++
(PRACTICAL#8)
Objective: To become familiar with Arrays
An array is a collection of multiple variable of the same data
type
An array is a group of variables (called elements) that are all of the same data
type.
The variables in an array are called the elements or items or
data items of array.
An array can be int or float or they can be user-defined types
like structures or objects.
Arrays are like structures in that they both group a number
of data items(variables, members, elements ) into a larger
unit.
Butwhile a structure groups data items of different data
types, an array groups data items of the same data type.
More importantly, the data items in a structure are accessed
by name, while in an array elements are accessed by an
index number.
Arrays
Definingarrays
Accessing array elements
Initializing arrays
Defining arrays in C++
An array definition specifies its data type, followed by the variable name
and a square bracket.
size of array
Example
int marks [ 4 ]; // array ‘marks’ of 4 ints
data type name Brackets delimit array size
(Square bracket signals, that variable marks is an array of type int)
Representation of an array in memory
int marks [ 4 ]; Memory
Each element in an array is assigned an marks [ 0 ]
index number.
marks [ 1 ]
Element 1 is assigned index 0.
Element 2 is assigned index 1. marks [ 2 ]
…..
….. marks [ 3 ]
Element 4 is assigned index 3.
The last element is at index 3.
Representation of an array in memory
int marks [ 4 ]; Memory
Accessing array elements marks [ 0 ]
Each element in an array is assigned
an index number.
marks [ 1 ]
Element 1 is assigned index 0.
Element 2 is assigned index 1.
marks [ 2 ]
..
Element 4 is assigned index 3.
marks [ 3 ]
The number of elements in an array
is called its length
Array Initialization
int marks [ 4 ];
Each element in array an array is assigned an index number.
marks [ 0 ] = 60;
marks [ 1 ] = 70;
.
.
marks [ 3 ] = 80;
Array Initialization
Each element in array an array is assigned an index number.
int marks [ 4 ] = { 50, 60, 70 ,80 };
Operations:
Traversing: accessing or processing each element of an array.
Insertion: Adding a new element to the array.
Deletion: Removing an element from the array.
Searching: Finding an element in an array or its location.
Sorting: Arranging the elements in some type of order.
Two-dimensional Arrays areoften used to represent tables of
values consisting of information in rows and columns.
Defining two- dimensional array
int arr [2][3]; // 2 by 3 array
First specify the rows and second specify the columns.
To access or identify a particular element in two-
dimensional we specify two indices.
Array creation and initialization
int arr [ 2 ][ 3 ] = { {1,2,3},{4,5,6} };
Array creation and initialization
int arr [ 3 ][ 2 ] = { {1,2},{3,4}, {5,6} };
Tasks for Lab # 8
Task # 1: Write a C++ program that creates and initializes an array of type integer as :
int array[]={5,10,15,20,25}; and computes the SUM of the elements in an array.
Task #2 Consider the array in Taskno.2 . Write a C++ program that print the all elements of the array in
reverse order, As 25,20, …5.
Task #3 : Write a C++ program that prints the second last element in the array created in Task no. 2.
And also displays the sum and product of last two elements of the array.
Task #4 : Write a C++ program that finds the smallest element in the array and also its index.
Task #5 : Write a C++ program that finds the largest element in the array and also its index.
Task #6 : Write a C++ program that passes an array (of your choice) as an argument to a function.