SUBROUTINE convert(xv, yv, r, theta)
!Initialise and name the subroutine

	REAL, INTENT(IN) :: xv, yv
	!Declare x, y dummy real variables that can be read in
	REAL, INTENT(OUT) :: r, theta
	!Declare r, theta dummy real variables that can be assigned within the subroutine and output to the
	!main program
	REAL :: temp
	!Declare a real variable to act as storage for one of the dummy variables

	temp = xv
	!Store the value read into xv in the temp variable
	r = SQRT(xv**2 + yv**2)
	!Calculate the magnitude of the input vector i.e. the r component and store it in the dummy x variable
	theta = ATAN2(yv, temp)
	!Calculate the argument of the input vector using the intrinsic function ATAN2() which contains checks
	!for x = 0 and subsequent cases of y and store it in the dummy y variable so we have (r, theta)

END SUBROUTINE convert
!End the subroutine

PROGRAM prog2B
!Initialise the main program

	IMPLICIT NONE
	!Assume nothing about the variables used in this program
	REAL :: x, y, r, theta
	!Declare real variables for x, y, magnitude and argument

	PRINT*, 'What are your Cartesian coordinates in R^2?'
	!Prompt the user to input their 2D vector, one component at a time
	READ*, x, y
	!Read in the first component to variable x and the second to variable y
	PRINT*, 'x = ', x, '   y = ', y
	!Print the vector components for confirmation
	
	CALL convert(x, y, r, theta)
	!Call the subroutine to read in and process the vector (x, y)
	
	PRINT '(a14, e11.5, a2, f8.5, a1)', '(r, theta) = (', r, ', ', theta, ')'
	!Print the output vector in (r, theta) form

END PROGRAM prog2B
!End the program
