CISC 3142
Programming Paradigms in C++
Lab #1
Basic, Imperative C++

How to Develop and Submit your Labs

Overview

Reimplement Labs 0.1-0.3 in C++.

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: 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

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

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:

Submitting to Codelab

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: