LECTURE 11: Formating To make columns line up in Fortran, you

LECTURE 11: Formating
For example
To make columns line up in Fortran, you must use format statements.
Instead of
print *,x,y
lets Fortran format the print.
print ’(20f6.2)’,x,y
prints both real numbers x and y in a field
of 6 spaces (including . and -) and 2 decimal places. The 20 specifies
that this format may to be used to print as many as 20 real numbers. But here, we have only two variables x, y and hence we use only
2 of the possible 20 fields. The remaining 18 used fields are ignored.
print *, ...
write
# format( *** )
If we write
where # is an arbitrary number (usually with 3 digits) used as a name
for the format and *** are format instructions. Just as in Scialb, the
format instructions depends on the type of variables that we are dealing
with and the space that we want to use for the printing:
print *,x
print *,y
then the first statement prints x, advances to the next line and
prints y. To print both on the same line, replace print *,x with
write(*,advance=’no’),x.
a character strings of unspecified length.
a12 reserves 12 spaces for characters (letters, numerals)
i for integers
i5 reserves 5 spaces an integer
f7.2 reserves 7 spaces (including spaces for ’.’ and any
’-’) for a real (floating point) number with two decimal
places.
4f7.2 reserves 4 consecutive fields of type f7.2.
4x prints 4 spaces as does ’
’.
/ begins a new line
Here are four ways to print x in the ’f7.2’ real format.
print ’(f7.2)’,x
101 format(f7.2) !101 is the name for the format
print 101,x !prints the value of x by using the format 101
write(*,101),x !prints the value of x by using the format
101
write(*,101,advance=’no’),x doesnt advance to the next line
after printing.
Items are right-justified in their fields (blanks added on the left).For
instance, when printing in the f7.2 real format:
’-999.44’ fills its 7 space field,
’
1.00’ fills only the rightmost 4 of the 7 spaces
1
c11.1 Modify the 5 red statements to make the title print on one line.
Change this to get spaces between Hw 1 Hw 2... You should get
Hw 1 Hw 2 ... Exam Grade
not H1H2 ...
not Hw1 Hw2 ....
Once you get the program working, delete n=3 and uncomment the
read *,n line to enable user input.
program print matrix test
real::a(2,2)
a(1,:)=[10.25,200.]
a(2,:)=[3.,4.5]
call print matrix(a,2,2)
endprogram
c11.2 Write a program which reads in a 3×3 matrix a and then extends
a to a 4 × 4 matrix with an added column on the right and added row
on the bottom. In each row, the value in the last column is 10 times the
rows first element plus the sum of the next two elements. Each bottom
row element is the average of the values above it. Print the resulting
matrix with the print matrix subroutine of the previous example.
program print title
print *,’Enter # of homework assignments’
n=3 !after the program works, delete this and
!read *, n !uncomment this to accept input.
do i=1,n
print 100,’Hw’,i !keep the ,’Hw’,i
enddo
100 format(a1,i1)
print ’(a6,a6)’,’Exam’,’Grade’
endprogram
c11.3 Write a program calculate which extends an n × m matrix a to a
larger (n + 1) × (m + 1) matrix. Get from the user the number of rows n
and the number of columns m. Allocate space for an (n+1)×(m+1) matrix a. The user then enters n rows, one at a time, each with m columns.
These are the first n rows and m columns of a. Your program calculates
the last m+1 column whose entries are the sum of the m-1 first columns
scores plus 3 times the m column. Then, your program calculates each
n+1 row entry which is the average (mean) of the n scores above it. Finally use the print matrix subroutine to print the matrix. But modify
the subroutine so the printed rows start with student 1, student 2,
...averages. For instance, if the user enters the 4 × 4 matrix
We can call a subroutine to execute a printing.
e11.1 The following subroutine prints a matrix b with n rows and m
columns. The program print matrix test executes this subroutines to
print a matrix a with 2 rows and 2 columns.
4 4
4.2 8
4 2 2.11 3
1 0
0.5 0
3.5 3
3 2
subroutine print matrix(b,n,m)
integer::n,m
real::b(n,m) !n = # rows, m = # columns
do i=1,n; print ’(20f6.2)’,b(i,1:m); enddo
endsubroutine !2 decimal reals printed in fields of 6 spaces
the output should look like
2
student 1
student 2
student 3
student 4
averages
4
4
1
3.5
3.12
4
4.2
2 2.11
0
0.5
3
3
2.25 2.45
c11.6 Assume that there are n students in a class and each of them has
received m scores along the semester, corresponding to m−1 homeworks
and 1 exam.
8
36.2
3 17.11
0
1.5
2
15.5
3.25 17.58
Write a program which extends the n × m matrix of scores to a to a
larger (n+1)×(m+1) matrix. Get from the user the number of students
(n) and the number of scores per students (m). Allocate space for an
(n+1)×(m+1) matrix a. The user then enters the scores of the students
(m-1 homework scores and 1 exam score). These are the first n rows and
m columns of a. The program calculates the last (m+1) column (the
students grade scores) which are the sum of the m-1 homework scores
plus 3 times the exam score. Your program calculates each bottom-row
(n+1 row) entry which is the average (mean) of the n scores above it.
The user then enters the names of the students, which are listed in a
vector names of length n+1 of characters strings. Assign ’averages’
to name(n+1).Using the std dev function from c11.5, find the standard
deviation of the grade scores (m+1 column).
c11.4 Let a = [a1 , a2 , . . . , an ] be a vector of length n and let m be the
average value of the entries a1 , a2 , . . . , an . The standard deviation of a
is given by
r
(a1 − m)2 + (a2 − m)2 + · · · + (an − m)2
n
Write a function std dev(a,n) which, given a vector a of n real numbers, returns the standard deviation of a. Write a program to test your
function.
c11.5 Write a function grade2(x,m,s) which, given an real score x, a
mean value m and a standard deviation s returns the character
Modify print matrix to a subroutine which prints first the initial heading
• A if x is 2 std. dev. above the mean,
• F if x is 2 std. dev. below the mean,
Hw 1 Hw 2 ...
• B if x is 1 std. dev. above the mean,
to the screen and then prints for each student, the students name (use
the built-in function trim to trim off trailing blanks in students’ names
), homework scores, exam score, final grade score, and, at the end of
each line, prints the letter grade calculated by the grade2 function from
c11.5. (Use advance=’no’ with the write function to keep the letter
grade on the same line). The last line will be the averages row, with no
letter grade at the end. The columns must line up vertically.
• D if x is 1 std. dev. below the mean,
• C otherwise.
Write a program test grade which applies grade to three inputs x, m
and s entered by the user
Exam Grade
If the user enters the matrix of scores
3
LABWORK SESSION, Monday, May 4th, 2015.
Send the problems in one email to [email protected] with the
subject line 190 l11(10). Don’t attach files!
In linear algebra, a n × m matrix A is said to be a boolean matrix if all
its entries are equal to 0 or 1. For instance,
4 4
4.2 8
4 2 2.11 3
,
1 0
1.5 0
3.5 3
3 2
and the list of students


0 1 0 0
1 0 1
B=
and C = 0 1 1 0
0 1 1
0 0 1 0
[jackson,johnson,nugimoto,weng]
the output should look like
jackson
johnson
nugimoto
weng
averages
hw 1
4.00
4.00
1.00
3.50
3.12
hw 2
4.00
2.00
0.00
3.00
2.25
hw 3
4.20
2.11
1.50
3.00
2.70
Exam
8.00
3.00
0.00
2.00
3.25
Grade
36.20
17.11
2.50
15.50
17.83
are boolean matrices. Moreover, a boolean matrix with constant row
sums is called a design matrix. For instance, the matrix B above is a
design matrix (its row sums are constant equal to 2) but the matrix C is
not (the sum of its first row is 1 but the sum of its second row is 2).Write
a function is boolean(a,n,m) which, given two integers n and m and a
n×m matrix a of integers, returns .TRUE. if a is a boolean matrix and
.FALSE. otherwise. Write a function is design(a,n,m) which, given
two integers n and m and a n×m matrix a of integers, returns.TRUE. if a
is a design matrix and .FALSE. otherwise. Then, write a program which
B
C
B
C
• asks for and reads a number n,
• asks for and reads a number m,
• allocates space for a n×m matrix a,
• asks for and reads the n rows of a, one at a time,
• use a subroutine print mat(a,n,m) to print the matrix according
to the following format: for each row of the matrix a, the subroutine prints the entries of the row (those entries may be considered
as integers with 3 spaces saved) then prints the character string
’sum of the row:’ and finally prints the row sum (which may
be considered as an integer with 3 spaces saved),
4
• uses the function is boolean to print ’This matrix is a
boolean matrix’ if a is a boolean matrix and ’This matrix is
not a boolean matrix’ otherwise,
• uses the function is design to print ’This matrix is a design
matrix’ if a is a design matrix and ’This matrix is not a
design matrix’ otherwise.
For instance, if the user enters the matrix C above, the output should
look like
0
0
0
1
1
0
This
This
0 0
1 0
1 0
matrix
matrix
sum of the row: 1
sum of the row: 2
sum of the row: 1
is a boolean matrix
is not a design matrix
5
6