## Overview

Reimplement Labs 0.1-0.3 in C++.
• This lab is nothing more than Lab 0 — Java Review (except for Lab 1.4, which deals with classes), reimplemented in simple C++.
• It's meant as a quick way to get into the portions of the language you already know from Java, as well as getting acquainted with your development environment.
• Reasonable basic algorithms are the same in both languages for these exercises; the primary changes are the I/O and basic program structure
• i.e., any implementation comments made in Lab 0 still hold, and you don't have to worry about 'doing it differently' here

### Lab 1.1 — Fun with Numbers

Write a program that reads three numbers from the keyboard, and then prints out some information about the relationships between the numbers-- whether they are all equal, in ascending order, in descending order, and so on. To determine these relationships, write the following boolean-valued functions:
• `boolean allAreEqual(int a, int b, int c);`
• `boolean twoAreEqual(int a, int b, int c);` // `false` if all three are equal
• `boolean noneAreEqual(int a, int b, int c);`
• `boolean areAscending(int a, int b, int c);` // `true` if a <= b <= c
• `boolean areDescending(int a, int b, int c);` // `true` if a >= b >= c
• `boolean strictlyAscending(int a, int b, int c);` // `true` if a < b < c
• `boolean strictlyDescending(int a, int b, int c);` // `true` if a > b > c
If for nothing more than practice, place the function definitions after your `main` and place function headers above.

Sample Test Run

first number? 1
second number? 2
third number? 3
allAreEqual: false
twoAreEqual: false
noneAreEqual: true
areAscending: true
areDescending: false
strictlyAscending: true
strictlyDescending: false

• Simple, basic C++ code
• No classes
• Interactive I/O (keyboard/screen)
• Conditionals and logical operators
• Simple functions

### Lab 1.2 — Calculating Averages

The file numbers.text consists of sequences of numbers, each sequence preceded by a header value and then followed by that many integers. Read in the sequences and print their averages. When all sequences have been read in, print out the number of sequences processed.

Sample Test Run

For example if the file `numbers.text` contains:

```3 1 2 3
5 12 14 6 4 0
10 1 2 3 4 5 6 7 8 9 10
1 17
2 90 80
```
the program should produce the following output:
```The average of the 3 integers 1 2 3 is 2.0
The average of the 5 integers 12 14 6 4 0 is 7.2
The average of the 10 integers 1 2 3 4 5 6 7 8 9 10 is 5.5
The average of the 1 integers 17 is 17.0
The average of the 2 integers 90 80 is 85.0
5 sets of numbers processed
```
• Basic file I/O
• More simple imperative processing

### Reading to eof

C++'s approach to reading until end-of-file is somewhat different than that for Java: rather than having a `hasNext` method (like Java, the C+ I/O runtime set the status of the stream after I/O operations. While we will eventually learn the details, for the moment, it is sufficient to simply perform the read and then treat the stream variable as if it were a boolean.

The structure is nearly identical to the above patterns for data files with trailer values, the only exceptions being:

• No trailer value need be placed into the file
• The loop condition tests for end of file rather than the trailer value.
• There are several ways to test for the end of file condition; you can use the `eof` function, or use syntax similar to the test for successful file open:
```infile >> value;
while (infile) {	// true only if the read was successful -- i.e., the file is still valid to be read from
...
infile >> value;
}
```

### Submitting to Codelab

• As mentioned in class and on the Labs Home Page, Codelab checking of full programs consists of a strict checking of your output against that of the official solution, which poses a bit of a challenege (and an opportunity) in this exercise which involves floating point output.
• The problem is this exercise is specified for multiple languages (in particular, Java originally), each of which has a different convention for printing floating point. In particular, whereas Java will print the floating point value 45 as 45.0, C++ will (by default) print it as 45 (and since the exercise was originally spec'ed for Java, your C++ output would fail Codelab).
• I could have created a separate exercise for this lab, but decided to use this as an opportunity to have you work with I/O manipulators
• To have your code print out the floating point results in the manner that Codelab is expecting, you need to manipulate the output stream in two ways:
• Specify the value should be printed out in 'fixed' (rather than 'floating point') format
• Set the precision to 1 (so even integer-like values get printed with a `.0`.
• As mentioned in the page on manipulators, these effects apply only to the next item... so you want something along the lines of:
```cout << fixed << setprecision(1) << result << endl;
```

### Lab (1.3) — Finding the Last Occurrence of a Value in a File

Write a program that prompts the user (at the keyboard)for a value and searches the file numbers.text for that value, printing out the last position where the number appears in the file (the first number in the file is at position 1). If the number does not appear in the file, print out an appropriate message. Continue asking the user for values until they signal end-of-file (i.e., in Windows one enters end-of-file at the keyboard by entering Ctl-Z, in Unix end-of-file is indicated by entering Ctl-D). The logic is the same as end-of-file for a disk file-- i.e., if your Scanner variable is named 'keyboard', you would have the loop condition 'while (keyboard.hasNextInt())'.

Sample Test Run

For example if the file `numbers.text` contains:

```10
23
43
5
12
23
9
8
10
1
16
9
```
execution of the program should look like:

Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:
• More of the same