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

Problem 2 The program for this exercise uses functions fi and f2 to generate (ij

ID: 3812701 • Letter: P

Question

Problem 2 The program for this exercise uses functions fi and f2 to generate (ij) elements of two dimensional arrays A and B respectively in the main program. These arrays have n rows and n columns where n is read from the keyboard. A contained subroutine called prMat neatly prints two dimensional arrays with up to 7 columns. The module subroutine called s1 uses the functions fi and f2 ta fill the arrays A and B. The module subroutine rowSums sums the values in the rows of BOTH A and B to produce an n element one dimensional array containing these sums. These totals are printed

Explanation / Answer

Using functions has a number of advantages:

1. The code to implement the calculation can be written just once, but used many times.

2. The functions can be tested separately from the rest of the program to make sure they give the correct result; we can then be confident when using them in the larger program.

Subroutines: Subroutines are very similar to functions except that they do not return a value. Instead they have an effect on the program in which they are invoked by modifying the arguments to the subroutine. Here is a simple example which defines a subroutine called swap which swaps the values of its two arguments:

.program swapmain

implicit none

real :: a, b !

!Read in two values

read(*,*) a, b

call swap(a,b)

write(*,*) a,b

.contains

subroutine swap(x, y)

real :: x, y, temp

temp = x

x = y

y = temp

end subroutine swap

end program swapmain

Note: how the subroutine is called and that its action is to modify its arguments.

it is a C++ program to show the desire result:-

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