What Is An Array
What Is An Array
An array is a collective name given to a group of similar quantities. These similar quantities could
be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or
ages of 25 students. Thus an array is a collection of similar elements. These similar elements
could be all integers or all floats or all characters etc
Declaration of an Array:-
Arrays must be declared before they can be used in the program.
Like any other variable, the syntax of declaring an array is:-
DataType ArrayName[dimension\order]
The array is first identified by its kind, which could be a char, an int, a float, etc; followed by its
name that follows the C++ naming rules.
The name is then followed by square brackets that specify the dimension of the array or its size.
Here are examples of declaring arrays:
int age[12]; float grade[100]; double angle[360]; int Age[12]; declares a group or array of 12 values,
each one being an integer.
float Grade[100]; declares an array of 100 floating-point values.
double Angle[360]; declares an array of double-precision numbers. There are 360 of these items in
the group.
Array types :-
Three types
Single dimention Array
Two-Dimensional Arrays
Just like any variable can be initialized, an array also can be initialized. To accomplish this, for a one-dimensional
array, the syntax used is:
DataType ArrayName[dimension] = { element1, element2, …, elementn}; Therefore, you can start with the data type
to specify the kind of array you are declaring. This is followed by the array name, and the square brackets. After
specifying the dimension or not, and after the closing square bracket, type the assignment operator. The elements,
also called items, that compose the array are included between an opening curly bracket '{' and a closing curly
bracket '}'. Each item is separate from the next by a comma operator. As a normal C/C++ initialization, you end it
with a semi-colon.
Here are examples of declaring an initializing arrays:
int number[12] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12}; double distance[5] = {44.14, 720.52, 96.08, 468.78,
6.28}; If you have decided to initialize the array while you are declaring it, you can omit the dimension. Therefore,
these arrays can be declared as follows:
int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12}; double distance[] = {44.14, 720.52, 96.08, 468.78,
6.28};
Introduction to Arrays
Introduction
An array of airplanes An array of bugs An
array of cards An array of characters
Applications of array:-
Arrays are used to implement mathematical vectors and matrices, as well as other
kinds of rectangular tables. Many databases, small and large, consist of (or include)
one-dimensional arrays whose elements are records.
Arrays are used to implement other data structures, such as heaps, hash tables, deques,
queues, stacks, strings, and VLists.
One or more large arrays are sometimes used to emulate in-program
dynamic memory allocation, particularly memory pool allocation. Historically, this
has sometimes been the only way to allocate "dynamic memory" portably.
Arrays can be used to determine partial or complete control flow in programs, as a
compact alternative to (otherwise repetitive), multiple IF statements. They are known
in this context as control tables and are used in conjunction with a purpose built
interpreter whose control flow is altered according to values contained in the array.
The array may contain subroutine pointers (or relative subroutine numbers that can be
acted upon by SWITCH statements) - that direct the path of the execution.
values[0]
10
The array values stored in the
.
values[1]
20
memory
values[2]
30
values[3]
40
values[4]
50
values[5]
60
values[6]
70
values[7]
80
values[8]
90
values[9]
100
Arrays Fundamentals :-