Solving Fortran Error on Pascal's Triangle Program

In summary: This should resolve the error and allow the program to compile successfully.In summary, the conversation discusses the creation of a Fortran program to print out the first n rows of Pascal's triangle. The code presented includes a function called FACT that calculates the factorial of a given number. The compiler throws an error at the line where the function is called, indicating an invalid declaration or reference to the function. The suggested solution is to include the type of data being returned in the function definition.
  • #1
Tubs
20
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
  • #2
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
 
  • #3
Tubs said:
FUNCTION FACT(N)

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

INTEGER FUNCTION FACT(N)
 

1. What is the purpose of Pascal's Triangle in Fortran?

Pascal's Triangle is a mathematical structure used to represent binomial coefficients. In Fortran, it is commonly used to solve problems involving combinations and permutations.

2. What are some common errors encountered when working with Fortran programs involving Pascal's Triangle?

Some common errors include incorrect array indexing, failure to initialize variables, and using incorrect data types. These errors can often be resolved by carefully reviewing the code and checking for syntax errors.

3. How do I fix a "segmentation fault" error when running a Fortran program involving Pascal's Triangle?

A "segmentation fault" error occurs when the program attempts to access memory that it does not have permission to access. This could be caused by an array index that is out of bounds or trying to access unallocated memory. To fix this error, carefully check all array indexing and ensure that any memory allocation is done correctly.

4. Is there a specific method for solving errors in Fortran programs involving Pascal's Triangle?

There is no one specific method for solving errors in Fortran programs involving Pascal's Triangle. However, some general tips include carefully reviewing the code for syntax errors, checking for correct array indexing, and using appropriate data types for variables.

5. How can I prevent errors when working with Fortran programs involving Pascal's Triangle?

The best way to prevent errors is to write clean and well-structured code. This includes using proper indentation, commenting your code, and regularly testing and debugging your program. It is also important to have a good understanding of Fortran syntax and common errors to watch out for.

Similar threads

  • Programming and Computer Science
Replies
4
Views
463
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
921
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top