FORTRAN Help: Using Functions and Arrays

In summary, The program is a simple calculator that allows the user to input two numbers and choose from four operations (addition, subtraction, multiplication, division). It uses a single function, "read_data", to prompt the user for input and return the chosen operation. The program then uses a select case statement to perform the chosen operation and output the result. The program also includes four separate functions for each operation: addition, subtraction, multiplication, and division.
  • #1
mattmac.nuke
22
0
Alright, so I've updated it once more... The only thing I need to do is figure out how to convert the functions x_data, y_data and choi_1 that store all 3 of the inputs I need for the main program and other functions.

program calculator
IMPLICIT NONE
REAL :: addition, subtraction, multiplication, division, x, y, j, k, x_data, y_data !x = A, y = B
CHARACTER :: choice, a, b, c, d, l, choi_1WRITE (*,*) ' '
WRITE (*,*) 'LAB 6- Baby Calculator'
WRITE (*,*) '----------------------'j = x_data(x)
k = y_data(y)
l = choi_1(choice)

SELECT CASE (l)
CASE ('A','a')
WRITE (*,*) 'A+B = ', addition (x, y)
CASE ('B','b')
WRITE (*,*) 'A-B = ', subtraction (x, y)
CASE ('C','c')
WRITE (*,*) 'A*B = ', multiplication (x, y)
CASE ('D','d')
WRITE (*,*) 'A/B = ', division(x, y)
END SELECT

end program calculator

CHARACTER FUNCTION choi_1 (choice)
IMPLICIT NONE
CHARACTER :: choice
WRITE (*,*) ' A) A+B'
WRITE (*,*) ' B) A-B'
WRITE (*,*) ' C) A*B'
WRITE (*,*) ' D) A/B'
WRITE (*,*) 'Enter choice:'
READ (*,*) choice
choi_1 = choice
END FUNCTION choi_1

REAL FUNCTION x_data (x)
IMPLICIT NONE
REAL :: x
WRITE (*,*) 'Enter a value A:'
READ (*,*) x
x_data = x
END FUNCTION x_data

REAL FUNCTION y_data (y)
IMPLICIT NONE
REAL :: y
WRITE (*,*) 'Enter a value B:'
READ (*,*) y
y_data = y
END FUNCTION y_dataREAL FUNCTION addition (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
addition = x + y
END FUNCTION addition

REAL FUNCTION subtraction (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
subtraction = x - y
END FUNCTION subtraction

REAL FUNCTION multiplication (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
multiplication = x*y
END FUNCTION multiplication

REAL FUNCTION division (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
IF ( y == 0) THEN
division = 0
ELSE
division = x/y
END IF
END FUNCTION division
 

Attachments

  • lab6_f11.pdf
    7.4 KB · Views: 305
Last edited:
Physics news on Phys.org
  • #2
mattmac.nuke said:
Alright, so I've updated it once more... The only thing I need to do is figure out how to convert the functions x_data, y_data and choi_1 that store all 3 of the inputs I need for the main program and other functions.

program calculator
IMPLICIT NONE
REAL :: addition, subtraction, multiplication, division, x, y, j, k, x_data, y_data !x = A, y = B
CHARACTER :: choice, a, b, c, d, l, choi_1


WRITE (*,*) ' '
WRITE (*,*) 'LAB 6- Baby Calculator'
WRITE (*,*) '----------------------'


j = x_data(x)
k = y_data(y)
l = choi_1(choice)

SELECT CASE (l)
CASE ('A','a')
WRITE (*,*) 'A+B = ', addition (x, y)
CASE ('B','b')
WRITE (*,*) 'A-B = ', subtraction (x, y)
CASE ('C','c')
WRITE (*,*) 'A*B = ', multiplication (x, y)
CASE ('D','d')
WRITE (*,*) 'A/B = ', division(x, y)
END SELECT

end program calculator

CHARACTER FUNCTION choi_1 (choice)
IMPLICIT NONE
CHARACTER :: choice
WRITE (*,*) ' A) A+B'
WRITE (*,*) ' B) A-B'
WRITE (*,*) ' C) A*B'
WRITE (*,*) ' D) A/B'
WRITE (*,*) 'Enter choice:'
READ (*,*) choice
choi_1 = choice
END FUNCTION choi_1

REAL FUNCTION x_data (x)
IMPLICIT NONE
REAL :: x
WRITE (*,*) 'Enter a value A:'
READ (*,*) x
x_data = x
END FUNCTION x_data

REAL FUNCTION y_data (y)
IMPLICIT NONE
REAL :: y
WRITE (*,*) 'Enter a value B:'
READ (*,*) y
y_data = y
END FUNCTION y_data


REAL FUNCTION addition (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
addition = x + y
END FUNCTION addition

REAL FUNCTION subtraction (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
subtraction = x - y
END FUNCTION subtraction

REAL FUNCTION multiplication (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
multiplication = x*y
END FUNCTION multiplication

REAL FUNCTION division (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
IF ( y == 0) THEN
division = 0
ELSE
division = x/y
END IF
END FUNCTION division

You're missing the READDATA function, so there should be a function with this name. From your PDF, the specification is:
Create a FUNCTION READDATA that will ask the user to enter real numbers A and B and a character CHOICE and stores that data in variables A and B and returns CHOICE.

Instead of having two functions - x_data and y_data - that each read a number from input, write a single function with two parameters that returns a character. This function should prompt the user to enter two numbers and a character that represents the choice of operation.
 
  • #3
I've written it as one function now, but after making a choice selection it tells me I'm missing a left parenthesis in format.

program calculator
IMPLICIT NONE
REAL :: addition, subtraction, multiplication, division, x, y, j, k, x_data, y_data !x = A, y = B
CHARACTER :: choice, a, b, c, d, l, choi_1


WRITE (*,*) ' '
WRITE (*,*) 'LAB 6- Baby Calculator'
WRITE (*,*) '----------------------'


READ choi_1(x, y, choice)

SELECT CASE (l)
CASE ('A','a')
WRITE (*,*) 'A+B = ', addition (x, y)
CASE ('B','b')
WRITE (*,*) 'A-B = ', subtraction (x, y)
CASE ('C','c')
WRITE (*,*) 'A*B = ', multiplication (x, y)
CASE ('D','d')
WRITE (*,*) 'A/B = ', division(x, y)
END SELECT

end program calculator



CHARACTER FUNCTION choi_1 (x, y, choice)
IMPLICIT NONE
CHARACTER :: choice
REAL :: x, y
WRITE (*,*) 'Enter a value A:'
READ (*,*) x
WRITE (*,*) 'Enter a value B:'
READ (*,*) y
WRITE (*,*) ' A) A+B'
WRITE (*,*) ' B) A-B'
WRITE (*,*) ' C) A*B'
WRITE (*,*) ' D) A/B'
WRITE (*,*) 'Enter choice:'
READ (*,*) choice
choi_1 = choice
END FUNCTION choi_1

REAL FUNCTION addition (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
addition = x + y
END FUNCTION addition

REAL FUNCTION subtraction (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
subtraction = x - y
END FUNCTION subtraction

REAL FUNCTION multiplication (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
multiplication = x*y
END FUNCTION multiplication

REAL FUNCTION division (x, y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
IF ( y == 0) THEN
division = 0
ELSE
division = x/y
END IF
END FUNCTION division
 
  • #4
It's this line:
READ choi_1(x, y, choice)

This is not how to call this function. What you want is something like
Code:
choice = choi_1(x, y, choice)

BTW, you have way too many character variables.
Code:
CHARACTER :: choice, a, b, c, d, l, choi_1
Get rid of a, b, c, d, and l. You aren't using a, b, c, or d, so they should not be in your program.

You also have way too many numeric variables; namely, x, y, j, k, x_data, y_data. Get rid of all but two of these. Having extra variables makes your program needlessly confusing, especially to you.

Also, you should pick a better name for your function than choi_1, which is silly. A better name would be GetInput.
 
  • #5
When calling a FUNCTION, the syntax is VARIABLE = FUNCTION (Arg1. Arg2, ...)

In the main program section, your FUNCTION calls need correction.

Also, you have READ CHOI_1 () and CHARACTER FUNCTION CHOI1 (). You cannot have the same name assigned to a variable and a FUNCTION at the same time.
 
  • #6
SteamKing said:
When calling a FUNCTION, the syntax is VARIABLE = FUNCTION (Arg1. Arg2, ...)
Another possibility is to use the returned value from the function, such as in a call to WRITE; e.g.,
WRITE *,* ADDITION(x, y)
SteamKing said:
In the main program section, your FUNCTION calls need correction.
The one in the READ statement definitely needs to be fixed. The others are OK.
SteamKing said:
Also, you have READ CHOI_1 () and CHARACTER FUNCTION CHOI1 (). You cannot have the same name assigned to a variable and a FUNCTION at the same time.
 
  • #7
I really appreciate all the help guys! I actually wound up debugging it during class. I discovered that I can reference the function in my case statements, where all 3 of the data inputs can be used in the arguments and computations. The TA's were amazed that it worked...

Now I have a slightly trivial question, you know how the cursor moves to the next line following a WRITE statement where there is a READ execution, well I need for the cursor to remain on the same line in which the WRITE statement was made. I believe the slash descriptor is used to do this, but I can't seem to apply it correctly, could you perhaps show me a simple example of it's use, where the user is prompted to enter a simple real number?
 

Related to FORTRAN Help: Using Functions and Arrays

What is FORTRAN?

FORTRAN, short for "Formula Translation", is a high-level programming language used for scientific and engineering applications. It was one of the first programming languages and is still widely used today due to its efficiency and ability to handle complex mathematical operations.

What are functions and arrays in FORTRAN?

Functions are subroutines that perform a specific task and return a value. They can be used to simplify complex calculations and improve code organization. Arrays are a collection of data elements of the same type that are stored in a single variable. They are useful for storing and manipulating large amounts of data.

How do I declare and use functions in FORTRAN?

To declare a function in FORTRAN, use the "FUNCTION" statement followed by the function name and its arguments. The function body should be enclosed in a "CONTAINS" statement. To use a function, simply call it by its name and provide the necessary arguments. The function will return a value that can be assigned to a variable or used in other calculations.

How do I declare and use arrays in FORTRAN?

To declare an array in FORTRAN, use the "DIMENSION" statement followed by the array name and its dimensions. Arrays in FORTRAN are indexed starting from 1, not 0. To access elements of an array, use the array name followed by the index in parentheses. Arrays can be used in mathematical operations and can also be passed as arguments to functions.

What are some common mistakes to avoid when using functions and arrays in FORTRAN?

Some common mistakes to avoid when using functions and arrays in FORTRAN include not properly declaring the function or array, using incorrect indices for array elements, and not properly passing arguments to functions. It is also important to ensure that the data types of arguments and return values match the function's specifications. Additionally, it is important to use descriptive and meaningful names for functions and arrays to improve code readability and organization.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
963
Back
Top