In this section, we will delve into the operations that can be performed on arrays and strings in Fortran. Understanding these operations is crucial for manipulating data efficiently and effectively in your programs.

Key Concepts

  1. Array Operations

    • Element-wise operations
    • Array slicing
    • Array intrinsic functions
  2. String Operations

    • Concatenation
    • Substring extraction
    • String intrinsic functions

Array Operations

Element-wise Operations

Fortran allows you to perform operations on arrays element-wise. This means you can add, subtract, multiply, or divide arrays of the same shape directly.

Example: Element-wise Addition

program element_wise_addition
    implicit none
    integer, dimension(3) :: array1 = (/1, 2, 3/)
    integer, dimension(3) :: array2 = (/4, 5, 6/)
    integer, dimension(3) :: result

    result = array1 + array2

    print *, "Result of element-wise addition: ", result
end program element_wise_addition

Explanation:

  • array1 and array2 are initialized with values.
  • The result array stores the element-wise addition of array1 and array2.

Array Slicing

Array slicing allows you to access a subset of an array. This is useful for manipulating parts of an array without affecting the entire array.

Example: Array Slicing

program array_slicing
    implicit none
    integer, dimension(5) :: array = (/1, 2, 3, 4, 5/)
    integer, dimension(3) :: slice

    slice = array(2:4)

    print *, "Array slice: ", slice
end program array_slicing

Explanation:

  • The slice array contains elements from the second to the fourth position of the array.

Array Intrinsic Functions

Fortran provides several intrinsic functions to perform operations on arrays, such as sum, product, maxval, and minval.

Example: Using Intrinsic Functions

program array_intrinsic_functions
    implicit none
    integer, dimension(5) :: array = (/1, 2, 3, 4, 5/)
    integer :: total, maximum

    total = sum(array)
    maximum = maxval(array)

    print *, "Sum of array elements: ", total
    print *, "Maximum value in array: ", maximum
end program array_intrinsic_functions

Explanation:

  • sum(array) calculates the sum of all elements in the array.
  • maxval(array) finds the maximum value in the array.

String Operations

Concatenation

String concatenation in Fortran is done using the // operator.

Example: String Concatenation

program string_concatenation
    implicit none
    character(len=5) :: str1 = "Hello"
    character(len=6) :: str2 = "World!"
    character(len=11) :: result

    result = str1 // " " // str2

    print *, "Concatenated string: ", result
end program string_concatenation

Explanation:

  • str1 and str2 are concatenated with a space in between to form the result.

Substring Extraction

You can extract substrings from a string using the substring notation string(start:end).

Example: Substring Extraction

program substring_extraction
    implicit none
    character(len=11) :: str = "Hello World"
    character(len=5) :: sub

    sub = str(1:5)

    print *, "Extracted substring: ", sub
end program substring_extraction

Explanation:

  • The substring from the first to the fifth character of str is extracted and stored in sub.

String Intrinsic Functions

Fortran provides several intrinsic functions for string manipulation, such as len, trim, and index.

Example: Using Intrinsic Functions

program string_intrinsic_functions
    implicit none
    character(len=11) :: str = "Hello World"
    integer :: length, position

    length = len(trim(str))
    position = index(str, "World")

    print *, "Length of trimmed string: ", length
    print *, "Position of 'World' in string: ", position
end program string_intrinsic_functions

Explanation:

  • len(trim(str)) calculates the length of the string after trimming trailing spaces.
  • index(str, "World") finds the starting position of the substring "World" in str.

Practical Exercises

Exercise 1: Array Multiplication

Write a program that multiplies two arrays element-wise and prints the result.

Solution

program array_multiplication
    implicit none
    integer, dimension(3) :: array1 = (/2, 4, 6/)
    integer, dimension(3) :: array2 = (/1, 3, 5/)
    integer, dimension(3) :: result

    result = array1 * array2

    print *, "Result of element-wise multiplication: ", result
end program array_multiplication

Exercise 2: String Reversal

Write a program that reverses a given string.

Solution

program string_reversal
    implicit none
    character(len=11) :: str = "Hello World"
    character(len=11) :: reversed
    integer :: i

    do i = 1, len(str)
        reversed(i:i) = str(len(str)-i+1:len(str)-i+1)
    end do

    print *, "Reversed string: ", reversed
end program string_reversal

Conclusion

In this section, we covered various operations that can be performed on arrays and strings in Fortran. We learned about element-wise operations, array slicing, and intrinsic functions for arrays. For strings, we explored concatenation, substring extraction, and intrinsic functions. These operations are fundamental for data manipulation and will be useful in more complex programming tasks. In the next module, we will delve into procedures and functions, which will help you organize and modularize your code effectively.

© Copyright 2024. All rights reserved