CP-Arrays C++
CP-Arrays C++
CP-Arrays C++
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.
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
marks [ 3 ] = 80;
Array Initialization
Each element in array an array is assigned an index number.
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.