Basic operations with a 1-dimension array


/ Published in: Fortran
Save to your folder(s)

This is an example of declaring and iterating over 1-dimension arrays in FORTRAN.


Copy this code and paste it in your HTML
  1. program arrays
  2. IMPLICIT NONE
  3. !Constant to be used in the declaration of the array
  4. INTEGER, PARAMETER :: SIZE = 3;
  5. INTEGER array (SIZE);
  6. INTEGER i;
  7. LOGICAL found;
  8. INTEGER valueToSearch;
  9. INTEGER sum;
  10. INTEGER minValue, maxValue;
  11. !Other way of declaring an array of dimension 1 and size SIZE
  12. REAL, DIMENSION(SIZE) :: A, B
  13. !Load and init the values of the array, describing which elements are inside.
  14. A = (/ 8, 9, 10 /);
  15. !Load and init the values of the array through a for each sentence
  16. B = (/ (i, i = 1, SIZE) /)
  17. print *,A
  18. print *,B
  19. !Only if both have the same dimension
  20. A = B
  21. print *,A
  22. !Load and init(manually). Two examples.
  23. DO i = 1, SIZE
  24. array(i)=i;
  25. END DO
  26.  
  27. DO i = 1, SIZE
  28. read *, array(i)
  29. END DO
  30.  
  31.  
  32. !Show
  33. DO i = 1, SIZE
  34. print *,array(i)
  35. END DO
  36.  
  37. !Search for a value (first apparition)
  38. i = 1;
  39. found = .FALSE.;
  40. valueToSearch = 3;
  41. do while (i<=SIZE .AND. .not. found)
  42. if (array(i)== valueToSearch) then
  43. print *,"(First) Found value at position: ", i
  44. found = .TRUE.
  45. end if
  46. i = i +1;
  47. end do
  48.  
  49. !Search for a value (ALL apparitions)
  50. i = 1;
  51. do while (i<=SIZE)
  52. if (array(i)== valueToSearch) then
  53. print *," (All) Found value at position: ", i
  54. end if
  55. i = i +1;
  56. end do
  57.  
  58.  
  59. !Search for a value (last apparition)
  60. i = SIZE;
  61. found = .FALSE.;
  62. valueToSearch = 3;
  63. do while (i>=1 .AND. .not. found)
  64. if (array(i)== valueToSearch) then
  65. print *,"(Last) Found value at position: ", i
  66. found = .TRUE.
  67. end if
  68. i = i -1;
  69. end do
  70.  
  71.  
  72. !Adding up values
  73. sum = 0;
  74. DO i = 1, SIZE
  75. sum=sum + array(i);
  76. END DO
  77.  
  78. print *,"The sum of values is ", sum
  79.  
  80.  
  81. !Adding up values with a filter
  82. sum = 0;
  83. DO i = 1, SIZE
  84. if (array(i)>3) then
  85. print *,"Found value at position: ", i
  86. sum=sum + array(i);
  87. end if
  88. END DO
  89.  
  90.  
  91. !Find the max value and use of the intrinsic function MAXVAL
  92. maxValue = array(1)
  93. DO i = 2, SIZE
  94. if (array(i)>maxValue) then
  95. maxValue = array(i);
  96. end if
  97. END DO
  98.  
  99. print *,"The max value is ", maxValue, MAXVAL(array)
  100.  
  101. !Find the min value and use of the intrinsic function MINVAL
  102. minValue = array(1)
  103. DO i = 2, SIZE
  104. if (array(i)<minValue) then
  105. minValue = array(i);
  106. end if
  107. END DO
  108.  
  109. print *,"The min value is ", minValue, minval(array)
  110.  
  111.  
  112.  
  113. read *,
  114.  
  115. end program arrays

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.