Practical Exercise 3 -------------------- Question 1 ---------- 1.1 How many times are the loops, defined by the following statements, executed: a) DO I = 2, N/2, 3 with N having the value 2. b) DO I = 1, N, 4 with N having the value 2. c) DO I = 3, N**2, 5 with N having the value 2. d) DO I = 2, N/2, 3 with N having the value 15. e) DO I = 1, N, 4 with N having the value 15. f) DO I = 3, N**2, 5 with N having the value 15. Question 2 ---------- 2.1 Write a program to read in a vector defined by a length and an angle in degrees, which prints out the corresponding Cartesian (x, y) co-ordinates. Recall that arguments to trigonometric functions are in radians. Demonstrate correctness by giving the Cartesian co-ordinates for the following vectors: length angle 12 77 1000 0 1000 90 20 100 12 437 Question 3 ---------- 3.1 Write a Fortran program to count how many of a set of ten integers typed in at the keyboard are multiples of five. Ignore any negative numbers. If you read in an array of length 10 (using just the array name in the READ statement), you can put all the numbers on one line. Test it using the following sets of numbers: 5 73 53 12 65 92 39 -11 60 13 0 6 8 8 -2 5 -3 -3 8 3 33 -2 69 -6 -15 93 52 76 56 11 Question 4 ---------- 4.1 Write a program to accept three integer lengths and report back on whether these lengths could define an equilateral, isoceles or scalene triangle (3, 2 or 0 equal length sides), or whether they cannot form a triangle. Demonstrate that the program works by classifying the following: Sides of length 1 1 1 2 2 1 1 1 0 3 4 5 3 2 1 1 2 4 Hint: if three lengths form a triangle then 2 times the longest side must be less than the sum of all three sides. In Fortran 90 terms, the following must be true: 2*MAX(sidel,side2,side3) < sidel+side2+side3 Question 5 ---------- 5.1 If you take a positive integer, halve it if it is even or triple it and add one if it is odd, and repeat, then the number will eventually become one. Warning: it may take a long time! Set up a loop containing a statement to read in a number (input terminated by zero) and to print out the sequence obtained from each input. The number 13 is considered to be very unlucky and if it is obtained as part of the sequence then execution should immediately terminate with an appropriate message. Demonstrate that your program works by outputting the sequences generated by the following numbers: 7 106 46 3 0 Question 6 ---------- 6.1 Write a program that will read a sequence of colors input as names, 'red', 'green', 'blue', 'yellow', 'black', 'white' or something else. Stop the program if a color 'blank' is read. Count the number of occurrences of each color, including the unspecified colors and print out the totals for each color before terminating the program. Use the file Programs/spectrum.data to test it. You can do that by running a command like: