Revision: 67772
Updated Code
at October 29, 2014 04:24 by chema_ar
Updated Code
program arrays
IMPLICIT NONE
!Constant to be used in the declaration of the array
INTEGER, PARAMETER :: SIZE = 3;
INTEGER array (SIZE);
INTEGER i;
LOGICAL found;
INTEGER valueToSearch;
INTEGER sum;
INTEGER minValue, maxValue;
!Other way of declaring an array of dimension 1 and size SIZE
REAL, DIMENSION(SIZE) :: A, B
!Load and init the values of the array, describing which elements are inside.
A = (/ 8, 9, 10 /);
!Load and init the values of the array through a for each sentence
B = (/ (i, i = 1, SIZE) /)
print *,A
print *,B
!Only if both have the same dimension
A = B
print *,A
!Load and init(manually). Two examples.
DO i = 1, SIZE
array(i)=i;
END DO
DO i = 1, SIZE
read *, array(i)
END DO
!Show
DO i = 1, SIZE
print *,array(i)
END DO
!Search for a value (first apparition)
i = 1;
found = .FALSE.;
valueToSearch = 3;
do while (i<=SIZE .AND. .not. found)
if (array(i)== valueToSearch) then
print *,"(First) Found value at position: ", i
found = .TRUE.
end if
i = i +1;
end do
!Search for a value (ALL apparitions)
i = 1;
do while (i<=SIZE)
if (array(i)== valueToSearch) then
print *," (All) Found value at position: ", i
end if
i = i +1;
end do
!Search for a value (last apparition)
i = SIZE;
found = .FALSE.;
valueToSearch = 3;
do while (i>=1 .AND. .not. found)
if (array(i)== valueToSearch) then
print *,"(Last) Found value at position: ", i
found = .TRUE.
end if
i = i -1;
end do
!Adding up values
sum = 0;
DO i = 1, SIZE
sum=sum + array(i);
END DO
print *,"The sum of values is ", sum
!Adding up values with a filter
sum = 0;
DO i = 1, SIZE
if (array(i)>3) then
print *,"Found value at position: ", i
sum=sum + array(i);
end if
END DO
!Find the max value and use of the intrinsic function MAXVAL
maxValue = array(1)
DO i = 2, SIZE
if (array(i)>maxValue) then
maxValue = array(i);
end if
END DO
print *,"The max value is ", maxValue, MAXVAL(array)
!Find the min value and use of the intrinsic function MINVAL
minValue = array(1)
DO i = 2, SIZE
if (array(i)<minValue) then
minValue = array(i);
end if
END DO
print *,"The min value is ", minValue, minval(array)
read *,
end program arrays
Revision: 67771
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 29, 2014 01:19 by chema_ar
Initial Code
program arrays
IMPLICIT NONE
!Constant to be used in the declaration of the array
INTEGER, PARAMETER :: SIZE = 3;
INTEGER array (SIZE);
INTEGER i;
LOGICAL found;
INTEGER valueToSearch;
INTEGER sum;
INTEGER minValue, maxValue;
!Other way of declaring an array of dimension 1 and size SIZE
REAL, DIMENSION(SIZE) :: A, B
!Init the values of the array, describing which elements are inside.
A = (/ 8, 9, 10 /);
!Inti the values of the array through a for each sentence
B = (/ (i, i = 1, SIZE) /)
print *,A
print *,B
!Only if both have the same dimension
A = B
print *,A
!Load (manually)
DO i = 1, SIZE
array(i)=i;
END DO
DO i = 1, SIZE
read *, array(i)
END DO
!Show
DO i = 1, SIZE
print *,array(i)
END DO
!Search for a value (first apparition)
i = 1;
found = .FALSE.;
valueToSearch = 3;
do while (i<=SIZE .AND. .not. found)
if (array(i)== valueToSearch) then
print *,"(First) Found value at position: ", i
found = .TRUE.
end if
i = i +1;
end do
!Search for a value (ALL apparitions)
i = 1;
do while (i<=SIZE)
if (array(i)== valueToSearch) then
print *," (All) Found value at position: ", i
end if
i = i +1;
end do
!Search for a value (last apparition)
i = SIZE;
found = .FALSE.;
valueToSearch = 3;
do while (i>=1 .AND. .not. found)
if (array(i)== valueToSearch) then
print *,"(Last) Found value at position: ", i
found = .TRUE.
end if
i = i -1;
end do
!Adding up values
sum = 0;
DO i = 1, SIZE
sum=sum + array(i);
END DO
print *,"The sum of values is ", sum
!Adding up values with a filter
sum = 0;
DO i = 1, SIZE
if (array(i)>3) then
print *,"Found value at position: ", i
sum=sum + array(i);
end if
END DO
!Find the max value and use of the intrinsic function MAXVAL
maxValue = array(1)
DO i = 2, SIZE
if (array(i)>maxValue) then
maxValue = array(i);
end if
END DO
print *,"The max value is ", maxValue, MAXVAL(array)
!Find the min value nd use of the intrinsic function MINVAL
minValue = array(1)
DO i = 2, SIZE
if (array(i)<minValue) then
minValue = array(i);
end if
END DO
print *,"The min value is ", minValue, minval(array)
read *,
end program arrays
Initial URL
Initial Description
This is an example of declaring and iterating over 1-dimension arrays in FORTRAN.
Initial Title
Basic operations with a 1-dimension array
Initial Tags
Initial Language
Fortran