# UCL WIKI

##### Child pages
• Exercises - vector, results, roots etc.
Go to start of banner

# Exercises - vector, results, roots etc.

Contents:

## Exercise - vector1

Type in the simple program below, which reads in the components of two vectors and then calculates the sum of the vectors. Call your program vector1.f90. Use some trial values to check that the program sums the vectors correctly. ## Exercise - vector2

The length of a vector r is given by LaTeX Markup problem Show/Hide latex markup \documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} \usepackage{bm} \newcommand{\mx}{\mathbf{\bm{#1}}} % Matrix command \newcommand{\vc}{\mathbf{\bm{#1}}} % Vector command \newcommand{\T}{\text{T}} % Transpose \pagestyle{empty} \begin{document}$(r_1^2+r_2^2+r_3^2 )$\end{document} Show/Hide latex error Unable to find DVI conversion log file.

, where r1, r2 and r3 are the components of the vector. Edit your program vector1.f90 to find the length rlen of the vector vectsum. Call your new program vector2.f90 and test it with some trial values.

Hint: try the functions sqrt and sum.

## Exercise - results1

Imagine that your department has asked you to write a program that records students' exam marks. Every student who scores more than 50% has passed the exam. You will need to use arrays to record the students' names, their marks and whether they have passed. The department has also asked that you calculate the total number who have passed, the average mark, and identify the person with the top mark. Write a prototype program called results1.f90 which processes results for 5 students and produces output similar to the following:

Student:

Fred

Susie

Tom

Anita

Peter

Mark:

64

57

49

71

37

Pass?

P

P

P

 No. of passes = 3 Average mark = 55.6 Prize awarded to Anita

### Parameters and initial values*

If you have a program with many arrays, it can be difficult to remember why particular arrays have particular sizes. One way round this is to use parameters in your array declarations, as illustrated below:

program arrays
integer,parameter:: imax=100, jmax=200 real,dimension(imax):: array1 real,dimension(imax,jmax+imax):: array2 real,dimension(3):: array3
etc....


In this program fragment, definitions of some parameters are included among the declarations. The parameters are not variables because they cannot be changed later in the program. Notice that the value of the parameter is set in its declaration statement.
Once it has been declared, a parameter can be used wherever you would use a constant, including in array declarations as shown. This can help avoid errors if you edit your program later to accommodate different array sizes, because you need only make the changes in one place. Notice the position of the parameter declaration: it must come before the parameters are used.

Values for variables can also be set in the declaration statements.
However, because the values of variables can be changed during operation of the program, these will be initial values only. The example above could have initialised the three arrays as follows:

Program arrays
integer,parameter:: imax=100, jmax=200 real,parameter:: pi=3.14159 real,dimension(imax):: array1=pi real,dimension(imax,jmax+imax):: array2=0.0 real,dimension(3):: array3=(/1.5,2.0,2.5/) etc....


All the elements in an array can be set to the same initial value as shown for array1 and array2, while the syntax for entering several initial values is shown for array3. Notice that initial values can only be constant expressions.

## Exercise - results2

Edit your program results1.f90 so that the number of students in the class is described as a parameter.

## Exercise - roots1

Type the following program into a file called roots1.f90 and then try running it with trial values of n.

program roots1
implicit none
integer:: i,n
real:: rooti
!
! Program to demonstrate the use of a DO loop.
!
write(*,*)' Enter an integer'
do i=2, 2*n, 2
rooti=sqrt(real(i))
write(*,*) i, rooti
end do
stop
end program roots1


This program reads in an integer (n) and then writes out the first n even numbers, together with their square roots. The do loop starts with the loop index i set to 2, then executes all statements down to the end do statement. In the first pass through the loop, the numbers are displayed on the screen. At the beginning of the second pass i is are incremented by 2 and takes the value 4. As long as i is not greater than 2*n, the statements will be executed again and the numbers 4 and 2 will be displayed on the screen. The loop will continue until the last numbers are displayed

## Exercise - roots2

Alter the program roots1.f90 so that it displays the results in reverse order. Name the altered program roots2.f90.

## Exercise - results2

Previously you wrote a program results1.f90, which recorded the exam marks of 5 students. The head of department is so pleased with your program that he would like a version that can handle large classes. Unfortunately, your original output layout is not suitable for larger numbers of students. Modify your program so that it uses a do loop to display the arrays in a vertical layout rather than a horizontal one, e.g:

 Student: Mark: Pass? Fred 64 P Susie 57 P Tom 49 Anita 71 P Peter 37 No. of passes = 3 Average mark = 55.6 Prize awarded to Anita

Test the program by entering marks for a larger number of students.

Hint: you may not know how many students are in a class. To make sure that your arrays are big enough to handle a whole class, either use allocatable arrays or declare the arrays with the maximum dimensions that you think they could need.

• No labels