.h/.cpp module pair named intfuncs containing several functions operating on integers:
Sample Test Run
first number? 2 second number? 4 max(2, 4): 4 min(2, 4): 2 average(2, 4): 3 abs(2): 2 abs(4): 4 first number? 5 second number? 1 max(5, 1): 5 min(5, 1): 1 average(5, 1): 3 abs(5): 5 abs(1): 1 first number? 7 second number? 4 max(7, 4): 7 min(7, 4): 4 average(7, 4): 5.5 abs(7): 7 abs(4): 4 first number? 6 second number? 6 max(6, 6): 6 min(6, 6): 6 average(6, 6): 6 abs(6): 6 abs(6): 6 first number? 1 second number? 2 max(1, 2): 2 min(1, 2): 1 average(1, 2): 1.5 abs(1): 1 abs(2): 2 first number?
.h file you are writing, you should read this web page
on working with the preprocessor, in particular this section on include file guards.
intfuncsxs module of Lab 3.1, write a minmax.h/minmax.cpp module that provides the function void minmax(x, y, minVal, maxVal)
(I am deliberately omitting the parameter types). The function accepts the two in parameter x and y, and returns their minimum and maximum in the out parameters minVal and
maxVal respectively.
You must use the min and max functions of the intfuncs module.
intfuncs module you wrote for Lab 3.1.
intfuncs and app code; you need only supply the minmax module.
min Function min — they differ in their arguments and return values:
main function demonstrating use of the above functions. The app should read pairs of numbers (until eof)
from the file numbers.text, call each of the functions and print out the results (using the format illustrated below).
If the file is not found, print out a message to that effect and exit with an exit code of 1.
Sample Test Run
int min(x, y): The minimum of 3 and 4 is 3 bool min(x, y, min): The minimum of 3 and 4 is 3 int min(x, y, ok): The minimum of 3 and 4 is 3 void min(x, y, ok, m): The minimum of 3 and 4 is 3 int min(x, y): The minimum of 5 and 2 is 2 bool min(x, y, min): The minimum of 5 and 2 is 2 int min(x, y, ok): The minimum of 5 and 2 is 2 void min(x, y, ok, m): The minimum of 5 and 2 is 2 int min(x, y): The minimum of 6 and 6 is 6 bool min(x, y, min): The numbers are equal (6) int min(x, y, ok): The numbers are equal (6) void min(x, y, ok, m): The numbers are equal (6)
.h) file
.cpp) file,
inline keyword)
The tags are often implemented as integers corresponding to the subscripts (locations) of the elements in the data array:
Before sorting:
(index) 0 1 2 3 4
+-------------------+
data array: | 4 | 7 | 1 | 8 | 5 |
+-------------------+
+-------------------+
tag array : | 0 | 1 | 2 | 3 | 4 |
+-------------------+
After sorting:
(index) 0 1 2 3 4
+-------------------+
data array: | 4 | 7 | 1 | 8 | 5 |
+-------------------+
+-------------------+
tag array: | 2 | 0 | 4 | 1 | 3 |
+-------------------+
Iterating through the tag array after the sort, following the tags, and printing the values produces:
1 4 5 7 8however, pointers often take the place of the integers. In the following example, the array begins at location 0100 (and each integer element is 4 bytes in length):
Before sorting:
(index) 0 1 2 3 4
+-------------------+
data array: | 4 | 7 | 1 | 8 | 5 |
+-------------------+
+-------------------+
tag array: |100|104|108|10A|200|
+-------------------+
After sorting:
(index) 0 1 2 3 4
+-------------------+
data array: | 4 | 7 | 1 | 8 | 5 |
+-------------------+
+-------------------+
tag array: |108|100|200|104|10A|
+-------------------+
and again, iterating through the pointers, and printing out the values at the other end of each pointer, results in:
1 4 5 7 8Notes
tagsort.h/.cpp with a single function named sort in the .h
file. sort accepts an array of integer pointers, and a integer representing the number of elements in the array,
and performs a tagsort on the data pointed at by the elements of the (tag) array parameter.
main method in its own file (tagsort_app.cpp) that
declares two arrays … one of integers and the other of pointers to integer (i.e., int[] and int *[]. Populate the first array with integers
read from the file numbers.text, and initialize the second so that each element points to the corresponding element of the
integer array (i.e., element 0 of the pointer/tag array points at element 0 of the integer array, element 1 to element 1, etc. —
the situation should thus look like the 'Before sorting' diagram above).
tagsort_app.cpp supplied to you is the test driver used by CodeLab
swap, please make sure they are in the correct order, or use function
headers.
Rational class in C++. As before, I will be supplying a test program (rational_test.cpp) as well as an exception class
(rational_exception.h). In addition, I am providing a partial rational.h to illustrate the proper parameter passing conventions used.
You might want to not look at these files initially, and see if you can transliterate them yourself from the Java version I supplied, using the parameter transmission
rules we discussed.
The basic specs are the same — the Notes section that follows contains language-specific differences. Any suggestions I provide are exactly that — suggestions. As long as you conform to the specs, and the CodeLab interface (see below), your are free to implement this in any manner you see fit.
The only additional function you need to write is a void print(ostream &os) const; member function that prints out the Rational. This replaces
the toString of your Java implementation, and should have the same semantics.
rational_test.cpp
rational.h
<< operator
rational.cpp
rational_exception.h
gcd.h
gcd function here. Since it's an .h file and potentially included by many sources in a program using it,
I declared it inline to prevent 'multiple definitions' link errors.
rational.h and rational.cpp as two separate exercises in CodeLab. As I will be supplying the other,
the function signatures in the .h and .cpp files must match (which is the purpose of this — to force you to use the
correct signatures). All the function definitions (bodies) should go into the .cpp, i.e., none should be coded inline in the class declaration
in the .h.
&); this is true of both fundamental as well as class types
const … &)
this is a pointer, so to get to fields of the object, you need -> and to refer to the object
you need *this
<< operator for Rational. This function relies on your print function
described in the overview.
cout << where r is of type Rational
rational.h.skeleton that you can access (like all the other files provided)
in the link at the bottom of this page.
Rational class declaration, with the operator below and declared inline
using namespace std; in your .h file (we will discuss the reason for this when we get to separate compilation in Part II).
The consequence of this is that anything from the Standard Library (e.g. ostream, cout, etc) must be fully qualified; i.e.,
must be prefixed with std::.
rational.h
rational.cpp
h / .cpp file; it may also be the case that I made a mistake in the signature). In the event of such (interface) errors, the compiler
will complain about the two functions signatures not matching. In order for you to figure out your error, you should know which signature is your
and which is mine, and where the compiler will be doing the complaining. In 4.1.1, the compiler will encounter the .h file first (because the
#include is encountered at the top of the .cpp file), and thus the error will be manifested when it reaches my signature
in the .cpp which won't match. Conversely, in 4.1.2 — where I am providing the .h file, the error will again appear
in the .cpp signature, which is yours this time.