Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Discrete subscripts

h_amini
Beginner
1,083 Views

Hello there

Can the subscripts of an array be discontinuous?

For example, how can I define a rank-one array (I) containing the elements I(5), I(6), I(21) and I(22).

Regards

Hamid

0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,083 Views
You can't define an array like that, but you can reference those elements with a vector subscript, for example:

I([5,6,21,22])

0 Kudos
h_amini
Beginner
1,083 Views
You can't define an array like that, but you can reference those elements with a vector subscript, for example:

I([5,6,21,22])


Thank you for this. Do you mean using something like the following program?

INTEGER I(4)

I = (/5,6,21,22/)

I(5)=50; I(6)=60; I(21)=210; I(22)=220

PRINT*, I(5), I(6), I(21), I(22)

END

0 Kudos
h_amini
Beginner
1,083 Views
Quoting - h.amini
You can't define an array like that, but you can reference those elements with a vector subscript, for example:

I([5,6,21,22])


Thank you for this. Do you mean using something like the following program?

INTEGER I(4)

I = (/5,6,21,22/)

I(5)=50; I(6)=60; I(21)=210; I(22)=220

PRINT*, I(5), I(6), I(21), I(22)

END


Something should be wrong. He program works with more subscripts, e.g. I(7), I(8), I(23) and I(24).

INTEGER I(4)

I = (/5,6,21,22/)

I(5)=50; I(6)=60; I(21)=210; I(22)=220

I(7)=70; I(8)=80; I(23)=230; I(24)=240

PRINT*, I(5), I(6), I(21), I(22)

PRINT*, I(7), I(8), I(23), I(24)

END

0 Kudos
Steven_L_Intel1
Employee
1,083 Views
No, that's not what I meant. The program you have here is invalid as array I is dimensioned (4) and you are referencing far outside its bounds.

What I meant was something more like this:

[plain]INTEGER I(25)

I(5)=50; I(6)=60; I(21)=210; I(22)=220

I(7)=70; I(8)=80; I(23)=230; I(24)=240

PRINT*, I([5,6,21,22])

PRINT*, I([7,8,23,24])

END[/plain]

0 Kudos
h_amini
Beginner
1,083 Views
No, that's not what I meant. The program you have here is invalid as array I is dimensioned (4) and you are referencing far outside its bounds.

What I meant was something more like this:

[plain]INTEGER I(25)

I(5)=50; I(6)=60; I(21)=210; I(22)=220

I(7)=70; I(8)=80; I(23)=230; I(24)=240

PRINT*, I([5,6,21,22])

PRINT*, I([7,8,23,24])

END[/plain]


Many thanks. But you defined a 25-element array. I wanted to have elements as less as possible, which would be an 8-element array for the above example (or 4-element for the first example).

0 Kudos
Steven_L_Intel1
Employee
1,083 Views
As I indicated earlier, you can't do that in Fortran. I suppose what you could do is define a second array with indexes into the first, but this would not save you any storage.
0 Kudos
h_amini
Beginner
1,083 Views
As I indicated earlier, you can't do that in Fortran. I suppose what you could do is define a second array with indexes into the first, but this would not save you any storage.

So the best solution seems to be defining an array with elements from the minimum to the maximum values. As I need dynamic variables, the program in post 4 will be:

INTEGER, ALLOCATABLE :: I(:)

M = 5; N = 24

ALLOCATE (I(M:N))

I(5)=50; I(6)=60 ; I(21)=210; I(22)=220

I(7)=70; I(8)=80 ; I(23)=230; I(24)=240

PRINT*, I(5), I(6), I(21), I(22)

PRINT*, I(7), I(8), I(23), I(24)

END

Thanks very much

Hamid

0 Kudos
Reply