The following program contains several examples about arrays. It explains how to create run-time arrays, how to read them, and how to print them. It also explains how to find the number of negative and the number of positive elements in an array and how to find the maximum and the minimum elements. Also, it shows how to reverse the element of an array whose indices start at 1 and how to transfer the elements of any array to an array whose indices start at 1.
PROGRAM PROG_ARRAY2
IMPLICIT NONE
INTEGER :: Lower_Ind_A, Upper_Ind_A, Upper_Ind_B
INTEGER :: Upper_Ind_U, Upper_Ind_V
INTEGER :: No_Neg_A, No_Pos_A, I
REAL :: Min_A, Max_A, Temp, U_dot_V
! Now we declare 4 real arrays: A, B, U, V:
REAL, DIMENSION(:), ALLOCATABLE :: A, B, U, V
! Now we read the lower index and the upper index of the array A:
PRINT *, "Enter lower and upper indices of A:"
READ *, Lower_Ind_A, Upper_Ind_A
! Now we allocate a space for the array A:
ALLOCATE(A(Lower_Ind_A: Upper_Ind_A))
! Now we read the elements of A:
PRINT *, "Enter elements of A:"
READ *, (A(I), I=Lower_Ind_A, Upper_Ind_A)
!==========================
! The purpose of the following segment is to find the number
! of negative elements and the number of positive elements of A.
! The code begins here:
No_Neg_A=0; No_Pos_A=0
DO I=Lower_Ind_A, Upper_Ind_A
IF (A(I)<0) No_Neg_A=No_Neg_A+1
IF (A(I)>0) No_Pos_A=No_Pos_A+1
END DO
PRINT *, "There are ", No_Neg_A, " negative elements in A. "
PRINT *, "There are ", No_Pos_A, " positive elements in A. "
! The code ends here
!========================
! The purpose of the following segment is to find the maximum
! and the minimum elements of A:
! The code begins here:
Min_A=A(Lower_Ind_A); Max_A=A(Lower_Ind_A)
DO I=Lower_Ind_A+1, Upper_Ind_A
IF (A(I)<Min_A) Min_A=A(I)
IF (A(I)>Max_A) Max_A=A(I)
END DO
PRINT *, "Minimum element of A is: ", Min_A
PRINT *, "Maximum element of A is: ", Max_A
! The code ends here
!================================
! The purpose of the following segment is to create an array called
! B whose lower index is 1 and then to assign the elements of A to B
! as they are ordered in A:
! The code begins here:
Upper_Ind_B=Upper_Ind_A-Lower_Ind_A+1
ALLOCATE(B(Upper_Ind_B))
DO I=1, Upper_Ind_B
B(I)=A(Lower_Ind_A+I-1)
END DO
! The code ends here
!================================
! The purpose of the following code is reverse the elements of B:
! The code begins here:
DO I=1, Upper_Ind_B/2
Temp=B(I)
B(I)=B(Upper_Ind_B-I+1)
B(Upper_Ind_B-I+1)=Temp
END DO
PRINT *, "The elements of B after being reversed are:"
PRINT *, B
! The code ends here
!===============================
! The purpose of the following code is to display the second up to the
! fifth elements of B (if it has 5 elements or more.) The elements will
! be displayed on the same line.
! The code begins here:
PRINT *, "The elements 2..5 of B are:"
IF (Upper_Ind_B>=5) THEN
PRINT *, (B(I), I=2, 5)
END IF
! The code ends here.
!================================
! The purpose of the following code is to allocate a space for
! two new arrays U and V. The lower index of both of them is 1
! and the upper indeces for both of them are the same.
PRINT *, "Enter upper index of U:"
READ *, Upper_Ind_U
Upper_Ind_V=Upper_Ind_U
ALLOCATE(U(Upper_Ind_U), V(Upper_Ind_V))
! The code ends here.
!================================
! The purpose of the following code is to read elements of
! U and then elements of V:
! The code begins here:
PRINT *, "Enter elements of U. Notice that there are ", Upper_Ind_U, " &
&of them:"
READ *, (U(I), I=1, Upper_Ind_U)
PRINT *, "Enter elements of V. Notice that there are ", Upper_Ind_V, " &
&of them:"
READ *, (V(I), I=1, Upper_Ind_V)
!================================
! The purpose of the following code is to find the dot product of
! U and V.
! The code begins here:
U_dot_V=0
DO I=1, Upper_Ind_U
U_dot_V=U_dot_V+U(I)*V(I)
END DO
PRINT *, "The dot product of U and V, is: ", U_dot_V
! The code ends here.
!================================
! The purpose of the following code is to free the memory allocated
! for arrays: A, B, U, V.
! The code begins here:
DEALLOCATE(A, B, U, V)
! The code ends here.
END PROGRAM PROG_ARRAY2