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 2D 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 xcoordinates in the interval [π₯π, π₯π] in steps of 1, that is, π₯π, π₯π + 1, π₯π + 2, π₯π + 3, β¦ , π₯π. Then, the ycoordinates are interpolated as: (π₯π, π¦π), (π₯π + 1, ππ(π¦π + π)), (π₯π + 2, ππ(π¦π + 2π)), (π₯π + 3, ππ(π¦π + 3π)), β¦ , (π₯π, π¦π) , where ππ(π) means the roundoff 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 ycoordinates in the interval [π¦π, π¦π] in steps of 1, that is, π¦π, π¦π + 1, π¦π + 2, π¦π + 3, β¦ , π¦π. Then, the xcoordinates 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 xcoordinate and ycoordinate 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 xcoordinates in the range [0,78] and ycoordinates in the range [0,22]. With the standard 2D coordinate system, i.e., xaxis being the horizontal axis and yaxis 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 2D 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 xaxis is printed as ββ.
The positive yaxis 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 IFGOTO. 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.
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 2D 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 xcoordinates in the interval [π₯π, π₯π] in steps of 1, that is, π₯π, π₯π + 1, π₯π + 2, π₯π + 3, β¦ , π₯π. Then, the ycoordinates are interpolated as: (π₯π, π¦π), (π₯π + 1, ππ(π¦π + π)), (π₯π + 2, ππ(π¦π + 2π)), (π₯π + 3, ππ(π¦π + 3π)), β¦ , (π₯π, π¦π) , where ππ(π) means the roundoff 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 ycoordinates in the interval [π¦π, π¦π] in steps of 1, that is, π¦π, π¦π + 1, π¦π + 2, π¦π + 3, β¦ , π¦π. Then, the xcoordinates 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 xcoordinate and ycoordinate 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 xcoordinates in the range [0,78] and ycoordinates in the range [0,22]. With the standard 2D coordinate system, i.e., xaxis being the horizontal axis and yaxis 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 2D 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 xaxis is printed as ββ.
The positive yaxis 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 IFGOTO. 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.
You'll get 1 file (517.1KB)