A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.
Here is the syntax template of a one-dimensional array declaration:
Data_Type Array_Name [Const_Int_Expression];
In the syntax template,
int number[50];
creates the number
array which has 50 components, each capable of
holding one int
value. In other words, the number
array has a total of 50 components, all of type int
.
Array_Name [Index_Expression]
The index expression must result in an integer value. It can be of type
char
, short
, int
,
long
, or bool
value because these are
all integral types. The simplest form of index expression is a constant.
For example:
number[0] specifies the 1st compnent of the number array
number[1] specifies the 2nd compnent of the number array
number[2] specifies the 3rd compnent of the number array
number[3] specifies the 4th compnent of the number array
number[4] specifies the 5th compnent of the number array
.
.
.
number[48] specifies the 2nd last component of the number array
number[49] specifies the last component of the number array
To store values in the number
array, we can do the following:
for (int i=0; i < 50; i++)
{
number[i]=i; //store a number in each array element
cout << "number[" << i << "] = " << number[i] << endl;
}
Each array element can be treated as a simple variable. Here, an integer
value is assigned to each array element, which has been declared to hold
data type int
.
number
array, we can treat each
array element as a simple variable of data type int
. For
example:
for (int i=0; i < 50; i++)
{
number[i]=2*number[i]; //double the value in each array element
//and store it in the array element
cout << "number[" << i << "] = " << number[i] << endl;
}
For the number
array, the valid index range is from 0 to 49. If
the IndexExpression results in a value less than 0 or greater than the
array_size minus 1, then the index is considered to be
out-of-bounds
. For instance, number[50]
is
trying to access a memory
location outside of the number
array.
A variable can be initialized in its declaration. For example:
int value = 25;
The value 25
is called an initializer.
Similarly, an array can be initialized in its declaration. A list of initial
values for array elements can be specified. They are separated with commas
and enclosed within braces. For example:
int age[5] = {23, 56, 87, 92, 38};
In this declaration, age[0]
is initialized to 23,
age[1]
is initialized to 56, and so on. There must be at least
one initial value between braces. If too many initial values are specified,
a syntax error will occur. If the number of initial values is less
than the array size, the remaining array elements will be initialized to zero.
In C++, the array size can be omitted when it is initialized in the declaration. For example:
int age[] = {23, 56, 87, 92, 38, 12, 15, 6, 3};
The compiler determines the size of the age
array
according to how many initial values are listed. Here, the size of
the age array is 9.
This program adds two integer arrays and displays the arrays.
// Program Arraypractice.cpp will add two arrays and store the sum
// in the third array. Print them all out to the screen.
#include <iostream>
using namespace std;
int main()
{
const int MAX_ARRAY = 5;
int a[MAX_ARRAY];
int b[MAX_ARRAY];
int c[MAX_ARRAY];
int index;
// Ask users to enter values for array a[].
for (index = 0; index < MAX_ARRAY; index++)
{
cout << "Please input a number for the array element: ";
cin >> a[index];
}
// Ask users to enters value for array b[].
for (index = 0; index < MAX_ARRAY; index++)
{
cout << "Please input a number for the array element: ";
cin >> b[index];
}
// Store the sum of array a[] and array b[] to array c[].
for (index = 0; index < MAX_ARRAY; index++)
{
c[index] = a[index] + b[index];
}
// Add code to print out each of the arrays
for (index = 0; index < MAX_ARRAY; index++)
{
cout << "array a is " << a[index] << endl;
cout << "array b is " << b[index] << endl;
cout << "array c is " << c[index] << endl;
cout << endl;
}
return 0;
}
©Department of Computer Science, University of Regina.