Starting from:

$20

Assignment 1: Line Drawing in Fortran and COBOL solution

Introduction
In this assignment, you will implement a program to raster segments of straight lines to output. The program reads a sequence of data points in 2-D space, connects the points one by one with straight lines, and then output the lines. You will write the program once using Fortran and once using COBOL. You also need to submit a simple report (less than one page) to tell us your experience in
using the two languages. The aim of this assignment is to let you have a taste of some old fashioned languages and have an understanding of what problems programmers encountered in the past.
Digital Differential Analyzer (DDA)
The straight line connecting two distinct points 𝑝𝑖 = (𝑥𝑖, 𝑦𝑗) and 𝑝𝑗 = (𝑥𝑗, 𝑦𝑗) can be represented using the equation 𝑦 − 𝑦𝑖 = 𝑚(𝑥 − 𝑥𝑖), where 𝑚 = 𝑦𝑗−𝑦𝑖
𝑥𝑗−𝑥𝑖 is the slope of the line. Suppose 𝑝𝑖 and 𝑝𝑗
contain integer coordinates only, in order to raster the straight line between 𝑝𝑖 and 𝑝𝑗 to a matrix (like a picture of pixels, the console, a text file, etc.), we need to approximate the straight line by interpolating the points between 𝑝𝑖 and 𝑝𝑗 with integer coordinates also. The digital differential analyzer (DDA) is an algorithm for such purpose. It considers two cases depending on the slope 𝑚.
Case 1: |𝑚| ≤ 1
Assume without loss of generality that 𝑥𝑖 < 𝑥𝑗. We sample the integer x-coordinates in the interval [𝑥𝑖, 𝑥𝑗] in steps of 1, that is, 𝑥𝑖, 𝑥𝑖 + 1, 𝑥𝑖 + 2, 𝑥𝑖 + 3, … , 𝑥𝑗. Then, the y-coordinates are interpolated as: (𝑥𝑖, 𝑦𝑖), (𝑥𝑖 + 1, 𝑟𝑑(𝑦𝑖 + 𝑚)), (𝑥𝑖 + 2, 𝑟𝑑(𝑦𝑖 + 2𝑚)), (𝑥𝑖 + 3, 𝑟𝑑(𝑦𝑖 + 3𝑚)), … , (𝑥𝑗, 𝑦𝑗) , where 𝑟𝑑(𝑘) means the round-off of 𝑘 to the nearest integer (四捨五入; 5 or above is rounded up, 4 or below is truncated).
Case 2: |𝑚| 1
Assume without loss of generality that 𝑦𝑖 < 𝑦𝑗. We sample the integer y-coordinates in the interval [𝑦𝑖, 𝑦𝑗] in steps of 1, that is, 𝑦𝑖, 𝑦𝑖 + 1, 𝑦𝑖 + 2, 𝑦𝑖 + 3, … , 𝑦𝑗. Then, the x-coordinates are interpolated
as: (𝑥𝑖, 𝑦𝑖), (𝑟𝑑(𝑥𝑖 + 1
𝑚), 𝑦𝑖 + 1), (𝑟𝑑(𝑥𝑖 + 2
𝑚), 𝑦𝑖 + 2), (𝑟𝑑(𝑥𝑖 + 3
𝑚), 𝑦𝑖 + 3), … , (𝑥𝑗, 𝑦𝑗).
Example 1. Two points (4,2) and (9,6). Slope 𝑚 = 6−2
9−4 = 0.8. (Case 1)
The interpolated points would be (4,2), (5, 𝑟𝑑(2.8)), (6, 𝑟𝑑(3.6)), (7, 𝑟𝑑(4.4)), (8, 𝑟𝑑(5.2)), (9,6).
That is, (4,2), (5,3), (6,4), (7,4), (8,5), (9,6). The following shows a plot of the points.
6 *
5 *
4 * *
3 *
2 *
y x 4 5 6 7 8 9

Example 2. Two points (7,5) and (6,9). Slope 𝑚 = 9−5
6−7 = −4. (Case 2)
The interpolated points would be (7,5), (𝑟𝑑(6.75), 6), (𝑟𝑑(6.5), 7), (𝑟𝑑(6.25), 8), (6,9). That is, (7,5), (7,6), (7,7), (6,8), (6,9). The following shows a plot of the points.
9 *
8 *
7 *
6 *
5 *
y x 6 7
Program Specification
You are required to write two programs, one in Fortran (file name dda.for) and the other in COBOL (file name dda.cob). The program flow and other requirements are specified below.
Program Flow
􀂾 The programs read 𝑛 points 𝑝1, 𝑝2, 𝑝3, … , 𝑝𝑛 from an input file which must be named input.txt.
You can assume that the input file always has the following format:
<n
<x_1␣<y_1
<x_2␣<y_2

<x_n␣<y_n
The first line contains an integer specifying the number of data points. Then in each of the subsequent <n lines, there are two integers <x_i and <y_i, separated by a space character
␣, specifying the x-coordinate and y-coordinate of data point 𝑝𝑖. Besides, all integers in the input file must have a fixed width of 2. The following shows an example input file specifying five data points (3,2), (40,22), (32,5), (28,12), and (0,15):
5
3 2
40 22
32 5
28 12
0 15
􀂾 You can assume that <n must be at least 2, and all points must have x-coordinates in the range [0,78] and y-coordinates in the range [0,22]. With the standard 2-D coordinate system, i.e., x-axis being the horizontal axis and y-axis being the vertical axis, the origin (0,0) is in the lower left corner.
􀂾 The programs then use the DDA algorithm to plot 𝑛 − 1 lines 𝑝1 ↔ 𝑝2, 𝑝2 ↔ 𝑝3, 𝑝3 ↔ 𝑝4, …, 𝑝𝑛−1 ↔ 𝑝𝑛. Your Fortran program should plot the lines to the standard output (console), while your COBOL program should plot the lines to an output file named output.txt.
􀂾 In the output, you should print out 23 lines, each line of which contains 79 characters. This corresponds to the 2-D plane spanning the space between (0,0) and (78,22). The origin (0,0) is in the lower left corner of your output and is printed as a ‘+’. The positive x-axis is printed as ‘-‘.

The positive y-axis is printed as ‘|‘. A point which is part of any of the 𝑛 − 1 lines is printed as ‘*’.
A point which is neither a part of any of the 𝑛 − 1 lines nor the axes is printed as a space ‘␣‘.
Note that if a ‘*’ has to be plotted at some point on the axes, then ‘+’, ‘-‘, or ‘|’ would not be printed at that point. (That is, ‘*’ has higher priority than ‘+’, ‘-‘, and ‘|’.)
􀂾 The following shows an example output using the five data points (3,2), (40,22), (32,5), (28,12), and (0,15) in the example input file above:
| *
| ***
| ** *
| ** *
| ** *
| ** *
| ** *
***** * *
| ********** ** *
| ********** *
| ** ***** *
| ** * *
| ** * *
| * * *
| ** * *
| ** * *
| ** **
| ** *
| **
| **
| *
|
+------------------------------------------------------------------------------
Other requirements
􀂾 The only control flow construct that you can use in your programs is IF-GOTO. All other selection and repetition constructs are not allowed (such as DO, WHILE, UNTIL, ELSE, ELSEIF in Fortran and similar constructs in COBOL). The purpose of this restriction is to let you understand the difficulties that programmers encountered in the past.
􀂾 In case of any file opening error, you have to print an error message to standard output (console) and terminate the program.
􀂾 You cannot write your programs in other languages and then initiate system calls or external library calls in Fortran and COBOL.
􀂾 Your program should include suitable comments as documentation. Failure to do so will suffer from mark deduction.
􀂾 You should decompose your programs into subroutines. Do not clog the main programs.
Report
Your written report (report.pdf) should answer the following questions within one A4 page.

1. Compare the conveniences and difficulties in implementing the program in Fortran and COBOL.
You can divide your comparison into specific tasks such as “file I/O”, “DDA implementation”, etc.
2. Compare Fortran and COBOL with a modern programming language (e.g., C/C++/Java/…) in different aspects (e.g., data types, parameter passing, paradigm, paradigm, etc.). You are free to pick your favorite modern programming language.
Submission and Marking
􀂾 Your program file names should be dda.for and dda.cob. Name your report as report.pdf.

More products