Sie sind auf Seite 1von 6

LECTURE 11: Formating For example

To make columns line up in Fortran, you must use format statements. print *,x,y lets Fortran format the print.
Instead of
print ’(20f6.2)’,x,y prints both real numbers x and y in a field
print *, ... 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 num-
bers. But here, we have only two variables x, y and hence we use only
write
2 of the possible 20 fields. The remaining 18 used fields are ignored.
# format( *** )
If we write
where # is an arbitrary number (usually with 3 digits) used as a name
print *,x
for the format and *** are format instructions. Just as in Scialb, the
print *,y
format instructions depends on the type of variables that we are dealing
with and the space that we want to use for the printing:
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
a character strings of unspecified length. write(*,advance=’no’),x.
a12 reserves 12 spaces for characters (letters, numerals)
i for integers Here are four ways to print x in the ’f7.2’ real format.
i5 reserves 5 spaces an integer print ’(f7.2)’,x
f7.2 reserves 7 spaces (including spaces for ’.’ and any 101 format(f7.2) !101 is the name for the format
’-’) for a real (floating point) number with two decimal print 101,x !prints the value of x by using the format 101
places. write(*,101),x !prints the value of x by using the format
4f7.2 reserves 4 consecutive fields of type f7.2. 101
4x prints 4 spaces as does ’ ’. write(*,101,advance=’no’),x doesnt advance to the next line
/ begins a new 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. program print matrix test
Change this to get spaces between Hw 1 Hw 2... You should get real::a(2,2)
Hw 1 Hw 2 ... Exam Grade a(1,:)=[10.25,200.]
not H1H2 ... a(2,:)=[3.,4.5]
not Hw1 Hw2 .... call print matrix(a,2,2)
Once you get the program working, delete n=3 and uncomment the endprogram
read *,n line to enable user input.
c11.2 Write a program which reads in a 3×3 matrix a and then extends
program print title a to a 4 × 4 matrix with an added column on the right and added row
print *,’Enter # of homework assignments’ on the bottom. In each row, the value in the last column is 10 times the
n=3 !after the program works, delete this and rows first element plus the sum of the next two elements. Each bottom
!read *, n !uncomment this to accept input. row element is the average of the values above it. Print the resulting
do i=1,n matrix with the print matrix subroutine of the previous example.
print 100,’Hw’,i !keep the ,’Hw’,i
enddo c11.3 Write a program calculate which extends an n × m matrix a to a
100 format(a1,i1) larger (n + 1) × (m + 1) matrix. Get from the user the number of rows n
print ’(a6,a6)’,’Exam’,’Grade’ and the number of columns m. Allocate space for an (n+1)×(m+1) ma-
endprogram trix 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
We can call a subroutine to execute a printing. 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. Fi-
nally use the print matrix subroutine to print the matrix. But modify
e11.1 The following subroutine prints a matrix b with n rows and m
the subroutine so the printed rows start with student 1, student 2,
columns. The program print matrix test executes this subroutines to
...averages. For instance, if the user enters the 4 × 4 matrix
print a matrix a with 2 rows and 2 columns.

4 4 4.2 8
subroutine print matrix(b,n,m) 4 2 2.11 3
integer::n,m 1 0 0.5 0
real::b(n,m) !n = # rows, m = # columns 3.5 3 3 2
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
c11.6 Assume that there are n students in a class and each of them has
student 1 4 4 4.2 8 36.2 received m scores along the semester, corresponding to m−1 homeworks
student 2 4 2 2.11 3 17.11 and 1 exam.
student 3 1 0 0.5 0 1.5
student 4 3.5 3 3 2 15.5 Write a program which extends the n × m matrix of scores to a to a
averages 3.12 2.25 2.45 3.25 17.58 larger (n+1)×(m+1) matrix. Get from the user the number of students
c11.4 Let a = [a1 , a2 , . . . , an ] be a vector of length n and let m be the (n) and the number of scores per students (m). Allocate space for an
average value of the entries a1 , a2 , . . . , an . The standard deviation of a (n+1)×(m+1) matrix a. The user then enters the scores of the students
is given by (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
r students grade scores) which are the sum of the m-1 homework scores
(a1 − m)2 + (a2 − m)2 + · · · + (an − m)2
plus 3 times the exam score. Your program calculates each bottom-row
n
(n+1 row) entry which is the average (mean) of the n scores above it.
Write a function std dev(a,n) which, given a vector a of n real num- The user then enters the names of the students, which are listed in a
bers, returns the standard deviation of a. Write a program to test your vector names of length n+1 of characters strings. Assign ’averages’
function. to name(n+1).Using the std dev function from c11.5, find the standard
deviation of the grade scores (m+1 column).
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 head-
ing
• A if x is 2 std. dev. above the mean,
Hw 1 Hw 2 ... Exam Grade
• F if x is 2 std. dev. below the mean,

• 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
• D if x is 1 std. dev. below the mean, ), homework scores, exam score, final grade score, and, at the end of
each line, prints the letter grade calculated by the grade2 function from
• C otherwise. 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
Write a program test grade which applies grade to three inputs x, m letter grade at the end. The columns must line up vertically.
and s entered by the user
If the user enters the matrix of scores

3
LABWORK SESSION, Monday, May 4th, 2015.
4 4 4.2 8 Send the problems in one email to gautier@math.hawaii.edu with the
4 2 2.11 3 subject line 190 l11(10). Don’t attach files!
,
1 0 1.5 0 In linear algebra, a n × m matrix A is said to be a boolean matrix if all
3.5 3 3 2 its entries are equal to 0 or 1. For instance,
and the list of students  
  0 1 0 0
1 0 1
[jackson,johnson,nugimoto,weng] B= and C = 0 1 1 0
0 1 1
0 0 1 0
the output should look like
are boolean matrices. Moreover, a boolean matrix with constant row
hw 1 hw 2 hw 3 Exam Grade sums is called a design matrix. For instance, the matrix B above is a
jackson 4.00 4.00 4.20 8.00 36.20 B design matrix (its row sums are constant equal to 2) but the matrix C is
johnson 4.00 2.00 2.11 3.00 17.11 C not (the sum of its first row is 1 but the sum of its second row is 2).Write
nugimoto 1.00 0.00 1.50 0.00 2.50 B a function is boolean(a,n,m) which, given two integers n and m and a
weng 3.50 3.00 3.00 2.00 15.50 C n×m matrix a of integers, returns .TRUE. if a is a boolean matrix and
averages 3.12 2.25 2.70 3.25 17.83 .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

• 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 subrou-
tine 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 1 0 0 sum of the row: 1


0 1 1 0 sum of the row: 2
0 0 1 0 sum of the row: 1
This matrix is a boolean matrix
This matrix is not a design matrix

5
6

Das könnte Ihnen auch gefallen