0% found this document useful (0 votes)
7 views7 pages

Rrays

The document provides an overview of arrays in C++, explaining their structure, declaration, initialization, and access methods. It covers single-dimensional and multi-dimensional arrays, including examples of how to declare and initialize them. Additionally, it details how to access specific elements within these arrays using indices.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Rrays

The document provides an overview of arrays in C++, explaining their structure, declaration, initialization, and access methods. It covers single-dimensional and multi-dimensional arrays, including examples of how to declare and initialize them. Additionally, it details how to access specific elements within these arrays using indices.

Uploaded by

randsand97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Arrays

The array, which stores a fixed-size, sequential collection of elements of the


same type.
An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific
element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The compiler will reserve a
section of the memory to hold all of the data that is defined for the array. The
lowest address corresponds to the first element and the highest address to the
last element.

Declaring Arrays
To declare an array in C++, the programmer specifies the type of the elements
and the number of elements required by an array as follows:
type arrayName [ arraySize ];

This is called a single-dimension array. The arraySize must be an integer


constant greater than zero and type can be any valid C++ data type. For
example, to declare a 10-element array called balance of type float, use this
statement:
float balance[10];

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 1 ……………..………. Computer Programming
Initializing Arrays
You can initialize C++ array elements either one by one or using a single
statement as follows:

float balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } can't be larger than the number of
elements that we declare for the array between square brackets [ ]. If any values
are not defined, they will be left empty. For example, the 4th and 5th elements
of the array will be empty if only 3 values were provided.

If you omit the size of the array, an array just big enough to hold the values
defined is created. Therefore, if you write:

float balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance[4] = 50.0;

The above statement assigns the 5th element in the array a value of 50.0. There
is a 4 inside of the brackets because the base index used to define the individual
elements starts at 0. This means the highest index is always going to be one less
than the maximum size of the array. Shown below is the pictorial representation
of the same array we discussed above.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 2 ……………..………. Computer Programming
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example:

float salary = balance[9];

The above statement will take 10th element from the array and assign the value
to the salary variable. Below is an example that declares, assigns values, and
accesses elements to an array.
#include<iostream>
using namespace std;
int main()
{ int n [10]; //n is an array of 10 integers
for(int i =0 ; i < 10 ; i++)
{
n [ i ] = i+100; //set element at location i to i+100
}
cout<<"Element Value"<<endl;

//output each array element's value


for(int j = 0 ; j < 10 ; j++)
{
cout<<" "<<j<<" "<<n [ j ]<<endl;
}
return0;
}
When the above code is compiled and executed, it produces the following

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 3 ……………..………. Computer Programming
result:

C++ allows multidimensional arrays. Here is the general form of a


multidimensional array declaration:

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three dimensional 5, 10, 4


integer array:

int threedim [5][10][4];

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 4 ……………..………. Computer Programming
Two-Dimensional Arrays:
The simplest form of the multi-dimensional array is the two-dimensional array.
A two-dimensional array is a list of one-dimensional arrays. To declare a two-
dimensional integer array of size x, y, you would write something as follows:

type arrayName [x][y];

Where type can be any valid C++ data type and arrayName will be avalid C++
identifier.
A two-dimensional array can be think as a table, which will have x number of
rows and y number of columns. A 2-dimensional array a, which contains three
rows and four columns can be shown as below:

Thus, every element in array a is identified by an element name of the form


a[i][j], where a is the name of the array, and i and j are the subscripts that
uniquely identify each element in a.

Initializing Two-Dimensional Arrays:


Multi-dimensioned arrays may be initialized by specifying bracketed values for
each row. Following is an array with 3 rows and each row have 4 columns.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 5 ……………..………. Computer Programming
int a [3][4]={
{0,1,2,3}, /* initializers for row indexed by 0 */
{4,5,6,7}, /* initializers for row indexed by 1*/
{8,9,10,11} /* initializers for row indexed by 2*/
};

The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to previous example:

int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array


Elements:
An element in 2-dimensional array is accessed by using the subscripts, i.e., row
index and column index of the array. For example:

int val=a[2][3];

The above statement will take 4th element from the 3rd row of the array. You can
verify it in the previous diagram.

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 6 ……………..………. Computer Programming
When the above code is compiled and executed, it produces the following
result:

---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 7 ……………..………. Computer Programming

You might also like