Jump to content

Multiplication tables: Difference between revisions

→‎{{header|Fortran}}: Traditionalists stick with whole lines at a go.
(Nimrod -> Nim)
(→‎{{header|Fortran}}: Traditionalists stick with whole lines at a go.)
Line 1,390:
 
end program multtable</lang>
 
===Traditional approach===
The usual style is to write whole lines at a go, traditionally to fast lineprinters. Producing a tabular layout is easy (four characters per field to allow 144 with a space separator), the difficulty lies in having blank parts at the start of the line followed by results. Having results followed by blanks is normal. The simplest way to achieve this would be to have a CHARACTER*4 function IFMT4(n) that returns four spaces for n <= 0, otherwise the digits, similar to the above example. But the plan is to write a line of such function calls at a go (with n = 0 for unwanted results), and alas, very few Fortran implementations allow recursive use of the formatted I/O system - here one level would be inside the function to produce the result for N > 0, and the other is the original WRITE statement that invokes the function.
 
So instead, write the table by first writing a line to a CHARACTER variable then blanking out the unwanted part.
<lang Fortran>
Cast forth a twelve times table, suitable for chanting at school.
INTEGER I,J !Steppers.
CHARACTER*52 ALINE !Scratchpad.
WRITE(6,1) (I,I = 1,12) !Present the heading.
1 FORMAT (" ×|",12I4,/," --+",12("----")) !Alas, can't do overprinting with underlines now.
DO 3 I = 1,12 !Step down the lines.
WRITE (ALINE,2) I,(I*J, J = 1,12) !Prepare one line.
2 FORMAT (I3,"|",12I4) !Aligned with the heading.
ALINE(5:1 + 4*I) = "" !Scrub the unwanted part.
3 WRITE (6,"(A)") ALINE !Print the text.
END !"One one is one! One two is two! One three is three!...
</lang>
Output in the same style as above, with underlining unavailable: those who have used a lineprinter's overprint facility to properly underline find the flabby modern requirement of a second line vexing, but, few output devices support underlining in so easy a way.
×| 1 2 3 4 5 6 7 8 9 10 11 12
--+------------------------------------------------
1| 1 2 3 4 5 6 7 8 9 10 11 12
2| 4 6 8 10 12 14 16 18 20 22 24
3| 9 12 15 18 21 24 27 30 33 36
4| 16 20 24 28 32 36 40 44 48
5| 25 30 35 40 45 50 55 60
6| 36 42 48 54 60 66 72
7| 49 56 63 70 77 84
8| 64 72 80 88 96
9| 81 90 99 108
10| 100 110 120
11| 121 132
12| 144
Going to the trouble of preparing results, and then blanking some might seem a little too crude. An alternative would be to use a different FORMAT statement for each line of output. But, a collection of a dozen output statements hardly represents a programming solution. Instead, create and then use the text of FORMAT statements, as follows. Notice that there are ''no reserved words'' in Fortran.
<lang Fortran>
Cast forth a twelve times table, suitable for chanting at school.
INTEGER I,J !Steppers.
CHARACTER*16 FORMAT !Scratchpad.
WRITE(6,1) (I,I = 1,12) !Present the heading.
1 FORMAT (" ×|",12I4,/," --+",12("----")) !Alas, can't do overprinting with underlines now.
DO 3 I = 1,12 !Step down the lines.
WRITE (FORMAT,2) (I - 1)*4,13 - I !Spacing for omitted fields, count of wanted fields.
2 FORMAT ("(I3,'|',",I0,"X,",I0,"I4)") !The format of the FORMAT statement.
3 WRITE (6,FORMAT) I,(I*J, J = I,12) !Use it.
END !"One one is one! One two is two! One three is three!...
</lang>
The output is the same, so instead, here are the generated FORMAT texts:
(I3,'|',0X,12I4)
(I3,'|',4X,11I4)
(I3,'|',8X,10I4)
(I3,'|',12X,9I4)
(I3,'|',16X,8I4)
(I3,'|',20X,7I4)
(I3,'|',24X,6I4)
(I3,'|',28X,5I4)
(I3,'|',32X,4I4)
(I3,'|',36X,3I4)
(I3,'|',40X,2I4)
(I3,'|',44X,1I4)
A zero count for spacing (the 0X, due no omitted results on the first line) was possibly a weak point, but if not handled, the fallback position would have been to arrange that instead of 12I4 format, the first would be 1X,I3.
 
Some fortrans offer an extension to FORMAT statements, whereby a variable can appear in place of an integer constant, thus instead of say FORMAT (12I4) there could be FORMAT (<n>I4) for example. Then, during the interpretation of the FORMAT text, the current value of variable ''n'' would be accessed. Note that this is on-the-fly:
READ(in,"(I2,<N>I4)") N,(A(I),I = 1,N)
would read N as a two-digit integer, and, as the READ ststement executes further, use that value of N both in the FORMAT text's interpretation and in the further processing of the READ statement.
 
=={{header|Go}}==
<lang go>
1,220

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.