Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Fortran program that defines two one-dimensional arrays, each with 20 re

ID: 3799473 • Letter: W

Question

Write a Fortran program that defines two one-dimensional arrays, each with 20 real elements Use the array constructor notation we discussed in lecture to initialize the values of the first array to the following values (you may cut and paste these to save some typing): 0.1557 -0.4643 0.3491 0.4340 0.1787 0.2577 0.2431 -0.1078 0.1555 -0.3288 0.2060 -0.4682 -0.2231 -0.4538 -0.4029 0.3235 0.1948 -0.1829 0.4502 -0.4656 Make sure that no line in your program is longer than 80 characters. Remember that the ampersand character & allows you to continue a line. Initialize the second array with zeros. Then, fill the second array with the sum of all of the values in the first array up to that point. In other words, the value in the first element of the second array will be 0.1557, the value of the second element of the second array will be 0.1557 + - 0.4643 = -0.3086. Use a loop to print out the second array, 5 elements at a time.

Explanation / Answer

program hello

real,dimension(20) :: array1
real,dimension(20) :: array2
integer::i
  
array1 = (/0.1557,-0.4643,0.3491,0.4340,0.1787,&
0.2577,0.2431,0.1078,0.1555,0.3288,&
0.2060,-0.4682,-0.2231,-0.4538,-0.4029,&
0.3235,0.1948,-0.1829,0.4502,-0.4656 /)
  
  
  
array2(1) = array1(1)
do i = 2, 20
array2(i) = array2(i-1)+array1(i)
end do
Print *, array2(1),array2(2),array2(3),array2(4),array2(5)
Print *, array2(6),array2(7),array2(8),array2(9),array2(10)
Print *, array2(11),array2(12),array2(13),array2(14),array2(15)
Print *, array2(16),array2(17),array2(18),array2(19),array2(20)
end program Hello

Output:

sh-4.3$ main                                                                                                                                                     

  0.155699998     -0.308600008       4.04999852E-02  0.474499971      0.653199971                                                                                

  0.910899997       1.15400004       1.26180005       1.41729999       1.74609995                                                                                

   1.95209992       1.48389995       1.26079988      0.806999922      0.404099911                                                                                

  0.727599919      0.922399938      0.739499927       1.18969989      0.724099874

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote