One Dimensional Array |
Let us consider A is a one dimensional array which have four elements like: |
A(1) A(2), A(3), A(4) |
these are stored in the computer and their values will be as: |
A(1) |
A(2) |
A(3) |
A4) |
2 |
8 |
3 |
6 |
|
|
This is an example of an array which contains integer numbers: |
Assume that the index (or subscript) starts at 1 then: |
· the first element of the array is 2 with an index of 1 |
· the second element of the array is 8 with an index of 2 |
· the third element of the array is 3 with an index of 3 |
· the last element of the array is 6 with an index of 4. |
· one-dimensional array is the simplest array, which is just a sequence of elements stored consecutively in the memory of computer. |
Example: |
the declaration is: |
real A(20) |
declares A as a real array of length 20 which means A consists of 20 real numbers stored contiguously in memory. Here the first number in the array is denoted by A(1) and the last by A(20). |
However, you may define an arbitrary index range for your arrays using the following syntax: |
real b(0:19), weird(-162:237) |
Here, b is a real array & the index runs from 0 through 19, while weird is an array of length 237-(-162)+1 = 400. |
The type of an array element can be any of the basic data types. |
Example: |
integer i(10) |
logical aa(0:1) |
double precision x(100) |
Each element of an array can be thought of as a separate variable. |
Vector: |
An array with a rank of one is called a vector. |
Example: |
Program of one dimensional array showing addition of array and displaying the array. |
 |
Output: |
 |
|
|