lab2 edit
This commit is contained in:
parent
dd32148817
commit
512acce7c1
|
|
@ -4,16 +4,107 @@
|
|||
|
||||
|
||||
### 2. Quadratic Function
|
||||
- Note: code needs to be formatted
|
||||
- credit to [Marco Biagini](https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation)
|
||||
- a quadratic equation of the form **ax^2 + bx + c = 0**
|
||||
- Code altered from [Marco Biagini](https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation)
|
||||
- Solves a quadratic equation of the form **ax^2 + bx + c = 0**
|
||||
|
||||
#### Input
|
||||
User will be prompted for a, b, and c (ex. 1, 5, 6)
|
||||
Coefficients are a fixed value with a = 1, b = 5, c = 6
|
||||
#### Output
|
||||
Program will display the real roots or if there are no real roots (ex. -2, -3)
|
||||
|
||||
EQUATION: (1x^2) + 5x + 6 = 0
|
||||
|
||||
The equation has two distinct real roots:
|
||||
Root 1: -02.00
|
||||
Root 2: -03.00
|
||||
|
||||
### 3. Integer Sort
|
||||
|
||||
### 4. Language Features
|
||||
- #### Identifiers
|
||||
All variables are declared within DATA DIVISION. Variable names accept letters(A-Z), digits(0-9), and hyphens(-). In the syntax of:
|
||||
|
||||
<level-number> <variable-name>
|
||||
[PIC/PICTURE <data-type(variable-length>)]
|
||||
[VALUE <literal-value>]
|
||||
Statements in [ ] are optional. Example:
|
||||
|
||||
01 INT-VAR PIC 9(03 VALUE 123).
|
||||
|
||||
- #### Special words
|
||||
- Note: Keywords are not case sensitive but good practice is to only use uppercase
|
||||
- Division statements divide the structure of the code. Some examples are PROGRAM-ID, DATA DIVISION, WORKING STORAGE DIVISION, PROCEDURE DIVISION, and STOP RUN.
|
||||
- DISPLAY outputs data to user
|
||||
- MOVE sends data from one variable to another
|
||||
- ACCEPT allows for user input to go into a variable
|
||||
- Supports boolean values TRUE and FALSE
|
||||
|
||||
|
||||
- #### Literal
|
||||
*> This is a comment in COBOL
|
||||
|
||||
Non-numeric
|
||||
|
||||
DISPLAY "This is a string in COBOL".
|
||||
DISPLAY 'This is a string in COBOL'.
|
||||
|
||||
Numeric
|
||||
|
||||
Digits 0 - 9
|
||||
Positive numbers: +10
|
||||
Negative numbers: -10
|
||||
Decimal: 10.00
|
||||
|
||||
- #### Math Operators
|
||||
Supports additon (+), subraction (-), multiplication (*), and division (/) and exponentation (**). Equations begin with COMPUTE.
|
||||
|
||||
COMPUTE total = 1 + 2
|
||||
|
||||
- #### Relational Operators
|
||||
Supports =, >, <, >=, <=, NOT =
|
||||
|
||||
IF a > b
|
||||
|
||||
- #### Delimiters
|
||||
Section headers and verb statements should end with a separator period (.)
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
MOVE "Hello" TO VAR1.
|
||||
|
||||
Commas (,) can separate two variables
|
||||
|
||||
ADD 3 TO VAR1, VAR2.
|
||||
|
||||
- #### Lists
|
||||
Supports arrays (known in COBOL as tables). Declared in DATA DIVISION. In the syntax of:
|
||||
|
||||
01 <table-name>.
|
||||
02 <variable-name> [PIC <data-type(length1)>]
|
||||
OCCURS <int1> [TO <int2>] TIMES
|
||||
[DEPENDING ON]
|
||||
[DESCENDING|ASCENDING KEY IS <key_var>]
|
||||
[INDEXED BY <index_name>]
|
||||
Statements in [ ] are optional. Example:
|
||||
|
||||
01 Students
|
||||
03 Student-grades PIC 9(03) OCCURS 6 TIMES.
|
||||
|
||||
- #### Loops
|
||||
Loops begin with a PERFORM statement and end with END-PERFORM. Note that a separator period (.) should not used within a PERFORM block.
|
||||
|
||||
PERFORM <conditional statement>
|
||||
<statements-block>
|
||||
END-PERFORM.
|
||||
|
||||
- #### Branching
|
||||
Uses IF/END-IF and ELSE statements and nested statements. Note that a separator period (.) should not used within an IF statement.
|
||||
|
||||
IF <condition>
|
||||
DISPLAY "Option 1"
|
||||
ELSE
|
||||
DISPLAY "Option 2"
|
||||
END-IF.
|
||||
|
||||
` Language Feature Sources `
|
||||
|
||||
- [tutorialspoint.com](https://www.tutorialspoint.com/cobol/cobol_basic_syntax.htm)
|
||||
|
||||
- [mainframestechhelp.com](https://www.mainframestechhelp.com/tutorials/cobol/)
|
||||
|
|
|
|||
|
|
@ -1,63 +1,35 @@
|
|||
/*code from https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation
|
||||
*> Code altered from https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation
|
||||
*> Program finds the roots to a simple quadratic equation
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
|
||||
PROGRAM-ID. QuadraticSolver.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
77 a PIC S9(5)V9(5) COMP-3 VALUE 1.
|
||||
77 b PIC S9(5)V9(5) COMP-3 VALUE 5.
|
||||
77 c PIC S9(5)V9(5) COMP-3 VALUE 6.
|
||||
77 discriminant PIC S9(5)V9(5) COMP-3.
|
||||
77 root1 PIC S9(5)V9(5) COMP-3.
|
||||
77 root2 PIC S9(5)V9(5) COMP-3.
|
||||
77 square-root-discriminant PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
PROCEDURE DIVISION. *> program begins here
|
||||
DISPLAY "EQUATION: (1x^2) + 5x + 6 = 0"
|
||||
COMPUTE discriminant = (b ** 2) - (4 * a * c)
|
||||
|
||||
77 a PIC S9(5)V9(5) COMP-3.
|
||||
IF discriminant > 0
|
||||
COMPUTE square-root-discriminant = FUNCTION SQRT(discriminant)
|
||||
COMPUTE root1 = (-b + square-root-discriminant) / (2 * a)
|
||||
COMPUTE root2 = (-b - square-root-discriminant) / (2 * a)
|
||||
DISPLAY "The equation has two distinct real roots: "
|
||||
DISPLAY "Root 1: " root1
|
||||
DISPLAY "Root 2: " root2
|
||||
|
||||
77 b PIC S9(5)V9(5) COMP-3.
|
||||
ELSE IF discriminant = 0
|
||||
COMPUTE root1 = -b / (2 * a)
|
||||
DISPLAY "The equation has one real root: "
|
||||
DISPLAY "Root: " root1
|
||||
ELSE
|
||||
DISPLAY "The equation has no real roots."
|
||||
|
||||
77 c PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
77 discriminant PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
77 root1 PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
77 root2 PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
77 square-root-discriminant PIC S9(5)V9(5) COMP-3.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
||||
DISPLAY "Enter the coefficients of the quadratic equation (a, b, c): ".
|
||||
|
||||
ACCEPT a
|
||||
|
||||
ACCEPT b
|
||||
|
||||
ACCEPT c
|
||||
|
||||
COMPUTE discriminant = (b ** 2) - (4 * a * c)
|
||||
|
||||
IF discriminant > 0
|
||||
|
||||
COMPUTE square-root-discriminant = FUNCTION SQRT(discriminant)
|
||||
|
||||
COMPUTE root1 = (-b + square-root-discriminant) / (2 * a)
|
||||
|
||||
COMPUTE root2 = (-b - square-root-discriminant) / (2 * a)
|
||||
|
||||
DISPLAY "The equation has two distinct real roots: "
|
||||
|
||||
DISPLAY "Root 1: " root1
|
||||
|
||||
DISPLAY "Root 2: " root2
|
||||
|
||||
ELSE IF discriminant = 0
|
||||
|
||||
COMPUTE root1 = -b / (2 * a)
|
||||
|
||||
DISPLAY "The equation has one real root: "
|
||||
|
||||
DISPLAY "Root: " root1
|
||||
|
||||
ELSE
|
||||
|
||||
DISPLAY "The equation has no real roots."
|
||||
|
||||
STOP RUN.
|
||||
STOP RUN.
|
||||
|
|
|
|||
Loading…
Reference in New Issue