Exercises set Saturday 23rd February 2002




Number 1

(Based on the 8 a.m. exercise)

Hint 1

Remember that scanf and fscanf do not care about lines of text. If you use them to read the file, make sure that each fscanf reads exactly the right number of characters to consume one whole line.

Hint 2

An alternate way to tell when you have reached the end of the file is to use the feof function. Immediately after using any function that reads from the file (fscanf, fgets, fgetc, etc), feof will tell you if the previous operation failed because you were at the end of the file and there was nothing for it to read. feof takes a single FILE * parameter and returns 1 or 0. e.g.
                    fscanf(fil, "%.....", &......);
                    if (feof(fil))
                      break;


Hint 3

Use an Array to keep all the records read from the file.
If your struct is called observation then this declaration:
                    observation data[9000];
creates the 'variable' data, making it big enough to hold 9000 observation objects (the file has slightly less than 9000 records in it). The 9000 individual variables that make up data behave exactly like normal observation variables would; they are known by their number (starting from 0), the first is referred to as data[0] the secons as data[1] and the last as data[8999].

If you have a function read_observation which reads an observation from the file and returns it as its result, you could read the whole file like this:
                    int number=0;
                    while (1)
                    { data[number]=read_observation(fil);
                      if (feof(fil))
                        break;
                      number+=1; }
Then, if for some reason you wanted to print all the temperatures (all 9000-ish of them), you could do so without haveing to read from the file ever again, like this:
                    for (int i=0; i<number; i+=1)
                    { print("temperature in record ");
                      print(i);
                      print(" is ");
                      print(data[i].temp);
                      newline(); }



Number 2

(Easier than it sounds)
Write a program that allows the user to specify a year and month, then using the graphics library, draws a beautiful calendar for that month, looking something like this:
 
February 2002
Su
Mo
Tu
We
Th
Fr
Sa
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
27
28
29
20
21
22
23
24
25
26
27
28
  

Do not worry about the Julian calenday or any of that nonsense, but do make it work for any reasonable year.


Number 3

Implement the rational data type (as a struct), providing the standard arithmetic operations of addition, subtraction, multiplication, and division, plus the ability to compare two rationals checking for equality and a<b. These operations may be implemented as normal functions, as operators, or both.
Test your implementation thoroughly. For example,
          print(add(make_rational(1,4), make_rational(1,12)))
is supposed to be adding one quarter and one twelfth, which produces one third, so it should print
          1/3



Number 4

(Based on Number 1)
You should retain from number 1 the array of observations and the ability to read it from the data file.
Correlation
       
No Correlation
 
 
No Correlation
       
Correlation



Number 5

(if you have time)
Read the beginning of chapter 4 of the printed notes (pages 101 to 110) and perform the ecological simulations that it discusses (using realmgraphics rather than ugly character-based plotting):
Stable
Oscillating
Chaotic
Hassled