danielp1213
Programmer
Hey guys I was wondering if i could get some help with my program. I am trying to add 2 separate arrays together section by section to create a large integer. I have a bunch of the code finished but I am having trouble with the sub routine and how it works. Here is the question:
Write a Fortran program to operate on two large integers (of any length, say,
up to 300 digits) and a subroutine to perform their addition.
(a) A suggested approach is as follows: Treat each number as a list (onedimensional
array) each of whose elements is a block of digits of that
number. For example, the integer 179,534,672,198 might be stored with
N(1) = 179,N(2) = 534,N(3) = 672,N(4) = 198.
(b) Then add the two integers (lists) element by element, carrying from one
element to the next when necessary.
(c) Test your subroutine by a program that reads two large integers and calls
the subroutine to find and output their sum.
and here is the code so far:
PROGRAM largeint
IMPLICIT NONE
CHARACTER (len=10) :: NUMx, NUMy
INTEGER :: i
OPEN (UNIT=1, FILE='int2.dat', STATUS='OLD')
READ(1,*) NUMx
READ(1,*) intx(i) i=1,3
READ(1,*) NUMy
READ(1,*) inty(i) i=1,3
CONTAINS
SUBROUTINE sumint(intx, inty)
INTEGER,INTENT(IN) :: intx, inty
INTEGER :: carry, i, z
z = intx + inty
carry = 0
DO i=1,3
L = 3-i+1
z(L) = z(L) + carry
carry=0
IF (z(L)>1000) THEN
z(L) = z(L) -1000
carry=1
END IF
END DO
END SUBROUTINE sumint
END PROGRAM largeint
sorry for the post being so long but not sure else how to put it. Thanks for any help you can offer!
Write a Fortran program to operate on two large integers (of any length, say,
up to 300 digits) and a subroutine to perform their addition.
(a) A suggested approach is as follows: Treat each number as a list (onedimensional
array) each of whose elements is a block of digits of that
number. For example, the integer 179,534,672,198 might be stored with
N(1) = 179,N(2) = 534,N(3) = 672,N(4) = 198.
(b) Then add the two integers (lists) element by element, carrying from one
element to the next when necessary.
(c) Test your subroutine by a program that reads two large integers and calls
the subroutine to find and output their sum.
and here is the code so far:
PROGRAM largeint
IMPLICIT NONE
CHARACTER (len=10) :: NUMx, NUMy
INTEGER :: i
OPEN (UNIT=1, FILE='int2.dat', STATUS='OLD')
READ(1,*) NUMx
READ(1,*) intx(i) i=1,3
READ(1,*) NUMy
READ(1,*) inty(i) i=1,3
CONTAINS
SUBROUTINE sumint(intx, inty)
INTEGER,INTENT(IN) :: intx, inty
INTEGER :: carry, i, z
z = intx + inty
carry = 0
DO i=1,3
L = 3-i+1
z(L) = z(L) + carry
carry=0
IF (z(L)>1000) THEN
z(L) = z(L) -1000
carry=1
END IF
END DO
END SUBROUTINE sumint
END PROGRAM largeint
sorry for the post being so long but not sure else how to put it. Thanks for any help you can offer!