Starting from:
$35

$29

Assignment 2 Solution

    1. The greatest common divisor (GCD) of two positive integers a and b is the largest integer that divides both a and b. The Euclidean algorithm for finding greatest common divisor, GCD(a, b), is as follows: if b = 0, GCD(a, b) is a; otherwise let a > b (if this is not true, the values of a and b are simply swapped) and divide a by b to obtain quotient q and remainder r, so that a = qb+r. Then, GCD(a, b) = GCD(b, r), which means, that the process is continued with b and r replacing a and b. Because the values of remainders decrease in each iteration, a zero remainder will be reached eventually. The last nonzero remainder is the GCD.

For example: GCD(1260,198) = GCD(198,72) = GCD(72,54) = GCD(54,18) = 18.

Write a Fortran integer function GCD(a, b) which finds and returns the greatest common divisor of a and b. Write a simple program which reads pairs of integer numbers and uses GCD(a, b) to find the greatest common divisor for each pair until a pair of zeros is entered indicating the end of data.

    2. One of methods of finding the square root of a positive number a is as follows: for any initial positive approximation x (for example, x = a/2), the next approximation is obtained by calculating the average of x and a/x, that is:

x +1    =  xI + a/xI

I    2

and this process continues until the difference between two consecutive values of x is sufficiently small.

Write a Fortran real function mysqrt which finds an approximation of the square root. Use mysqrt in a program which reads consecutive values a and prints a and its aproximated square root until a negative value of a is entered.

Submission instructions:

    • Paper copies of your programs with representative results should be handed over at the start of class on the assignment due date.

    • Brightspace (D2L) environment is used for electronic submission of assignments. A single zip file, with all relevant Fortran and data files, should be created for each assignment, for example:

zip name.zip *.f90 *.dat

and this name.zip file is submitted.

    • To use Brightspace, go to https://online.mun.ca and log in using your my.mun.ca username and password. Click on the 3731 (or 6906) link.

    • Click on Assignments (Dropbox) link from the navigation bar of the course. Select the appropriate assignment link and click on it. Click on the Add a File button to upload your file(s). Once all files have been added, click on the Submit button.

    • To exit the Brightspace environment, click on the Log Out link from the navigation bar of the course.

    • If you mistakenly submit incorrect file(s), or you make changes to your file(s) after you have submitted them, you may submit again (but only before the due time). The most recent submission is used for evaluation.

More products