Need help with Fortran code, please help 1. Write a Fortran program that declare
ID: 3885399 • Letter: N
Question
Need help with Fortran code, please help
1. Write a Fortran program that declares two integer arrays of length 4.
a. Set the values in the first array to [-2, 0, 2, 4] by initializing on the line where the array is declared. Print the array using fields 3 characters wide for each element.
b. Set the values in the first array to [-2, 0, 2, 4] by using a counting loop whose loop index variable goes from 1 to 4 and finding a formula that converts the loop variable into the needed values. (Spend some time thinking about this, as it may be useful later.) Print the array using fields 5 characters wide for each element.
c. Prompt the user for any 4 integer values (they needn't be the ones from parts a and b) and read them into the first array directly. Do not declare extra variables to do this. Print the array.
d. Use a loop to fill the second array with the values from the first array in reverse order. Print the second array.
Explanation / Answer
Let us solve the problem part by part.
Note:
1. Using # for comment
2. Array in Fortran is Zero level initiated; meaning Array(postion 0, position 1, ...)
program Fortran_Array
implicit none
##Part A:
## Declaring Fortran arrays as per given reuirement.
int, dimension(4) :: array_a = (/-2,0,2,4/)
int, dimension(4) :: array_b
## Printing each value to be 3 characters wide.
do i=1,4
write(*,3) i
print *, array_a(i)
end do
#Part B
#Fill first array with values. Trick is to count do loop from -1 to 2.
do i= -1,2
array_a(i) = i * 2
end do
# Display values 5 characters wide
do i=1,5
write(*,5) i
print *, array_a(i)
end do
#Part C
# Take array input from user
do i = 1,4
array_a(i)=read(*)
end do
#Print array
do i=1,4
print array_a(i)
end do
#Part D
# Load array_b
integer :: m=4
do j=1,4
array_b(j) =array_a(m)
m=m-1
end do
#Print array_b
do i=1,4
print array_b(i)
end do