Fortran Solving Fortran Error on Pascal's Triangle Program

  • Thread starter Thread starter Tubs
  • Start date Start date
  • Tags Tags
    Error Fortran
AI Thread Summary
The discussion revolves around a Fortran program designed to print the first n rows of Pascal's triangle. The user encounters a compilation error at the line where the factorial function, FACT, is called. The error message indicates an invalid declaration or reference to the symbol FACT. Suggestions for resolving the issue include using a local variable to store intermediate factorial values and ensuring that the function definition specifies the return type. Specifically, it is recommended to declare the function as "INTEGER FUNCTION FACT(N)" to clarify the data type being returned. Additionally, there is advice to correct the loop syntax in the factorial calculation to ensure proper execution.
Tubs
Messages
20
Reaction score
0
I am trying to write a fortran program to take an integer n and print out the first n rows of Pascal's triangle. The code I have so far:

PROGRAM PascalTriangle
IMPLICIT NONE
INTEGER i, n, j, ktemp, ktemp1, ktemp2, ktemp3, ktemp4, ktemp5
WRITE(6,*), "Enter a positive integer n:> "

READ(5,*) n

do 10 i = 1, n
do 20 j = 1, i
ktemp1 = FACT(i)
ktemp2 = FACT(j)
ktemp3 = i - j
ktemp4 = FACT(ktemp3)
ktemp = ktemp1 / ktemp2 * ktemp4
write(*), ktemp
20 continue
10 continue
STOP
END

FUNCTION FACT(N)
FACT=1
DO 30 J=2,N
FACT=FACT*J
30 CONTINUE
RETURN
END

Whats really confusing me is that the compiler throws a single error at the ktemp1 = FACT(i) line, stating that its an invalid declaration of or reference to symbol FACT. It looks like it should be a simple fix but I can't seem to get around the error. Any help is greatly appreciated.
 
Technology news on Phys.org
Try using a local variable to hold the intermediate values of "fact" and only assigning it to the functions return value once at the completion of the function.


Code:
function fact(n)
tmpfact :: integer
tmpfact = 1 
do 30 j-2,n
    tmpfact = tmpfact * j
30 continue
fact=tmpfact
return
 
Tubs said:
FUNCTION FACT(N)

Try including the type of data being returned, in the function definition:

INTEGER FUNCTION FACT(N)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
25
Views
3K
Replies
4
Views
2K
Replies
12
Views
1K
Replies
5
Views
5K
Replies
3
Views
2K
Replies
17
Views
6K
Replies
12
Views
3K
Replies
2
Views
2K
Back
Top