It is expected that all students will participate in the recitation during the regularly scheduled class. Those who participate elsewhere may not get the in-person assistance available during class.
Today's recitation will introduce the practice of multiple-file compilation, which will be used for the homework throughout the semester.
Start by accessing the Recitation Starter Files link on the ANGEL page and opening the folder named 1 which will be used for today's class.
What C++ source files are in that folder?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | There should be four C++ Source files (triarea.cpp, quadarea.cpp, heroarea,cpp, polyarea.cpp) |
NOTE: It is permissible to use other platforms to develop homework solutions, but today's class will assume Visual Studio, since that is in the classroom.
Be sure to read each group of instruction carefully before proceeding, so you do not have to spend valuable class time back-tracking to fix problems!
To create a new project in Visual Studio:
-- Open up the Visual Studio Package and then either Create a New Project from the welcome screen or from the File pull-down menu.
-- Request a C++ Win32 Console Application (not Visual Basic; not Win32 Project; not Empty Project)
-- At the bottom of the dialog box, assure that the new project will appear somewhere on the V drive and NOT the X drive.
How you organize your folders on that drive is up to you; if you have an X-drive folder already existing, you can move it to the V-drive.
-- On the second dialog box, be sure to modify Applications Settings to deactivate Precompiled Headers and make this an otherwise Empty Project
Once the project is created, right click on the Sources folder to Add New Item for each of the four given .cpp files (using the same file names is recommended). Also, install the given .h file into the Headers folder.
Once you have all of the files present, you may attempt to Build the Project.
What error arises when you try to do so?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | There will be multiple declarations for some of the functions in this program! |
The project should now Build correctly.
Take a look at triarea.cpp first. This contains a main function.
There really is very little in this file; it describes two triangles using arrays for vertices, and then computes their areas.
The actual computation is not visible here. Instead, near the top of the file is #include "triarea.h", which includes a little more information.
Go ahead and look at that file. Not much shows up there either.
But even without the implementation, there is enough information to compile the main function -- all it needs to know is what the parameter list is for triangleArea to call it properly. The details of the implementation are not required.
This is an important note about multiple-source file compilation. By implementing different features of a project separately, they can be thought about separately, and even implemented by different people.
Consider the project as it is now. How is this triangle area actually computed? (In which file does the function really appear?)
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | This uses the calculation in polyarea.cpp. |
Now let us compute this area using a different function.
Right-click on polyarea.cpp to temporarily remove it from the project, and then right-click on the Sources folder to bring back heroarea.cpp.
Then try to compile and run the program.
Important note:
What changes had to be made to the triarea.cpp to use this other function?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | None! The interface, as defined in triarea.h, has been preserved. |
But this idea can also be used to share modules between unrelated projects.
Remove "triarea.cpp" from the Project and bring in "quadarea.cpp". This actually computes the area of a quadrilateral by imagining it to be the combination of two triangles.
What changes have to be made to "heroarea.cpp" to make it work with this new main function?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | Still, no changes should be required! |
I defined these arrays in this way so that I can still use the existing triangleArea function, which expects three-value arrays.
If you were to pass Xs as a parameter to that function, it would use only the first three elements (enough for a triangle).
But if you were to pass &Xs[2] as a parameter, that would use the address of the element Xw[2] as if it were the beginning of the array, and would use the last three elements of that array.
If we were to consider the two triangles described by the previous paragraphs, they should be two opposing parts of the quadrilateral.
A glance at the main function should discover that the second and third pairs of arrays actually describe the same figure -- they just start from a different vertex. Since it is the same figure, the areas should be the same. Are they?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | They are unequal. One is actually two large. |
In Visual Studio, you can set a breakpoint by clicking to the left of any program statement, which would make a Red dot appear in the margin.
Set a breakpoint on the return statement in quadArea.
Then from the Debug Menu, Start with Debugging to encounter that breakpoint. You should be able to observe the values of all the variables in your program by moving the cursor over them.
If you have variables for your triangle areas, it should be easy to see what values contributed to the incorrect area; and should also be able to determine what calculation would be required.
Also from the Debug menu, you can select Continue so you can find out what happened to the third calculation.
Take a look at the two triangle areas at this point in time, and the correct quadrilateral area (from the second figure).
What calculation would be required to give the correct answer from the two triangle areas in this situation?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | The correct answer is the difference of these two values; this is actually a concave figure, so both triangles include areas outside the quadrilateral. |
Fortunately, we have a function already present that would consistently give correct results.
Inside polyarea.cpp is a function called polyArea that would work for any size polygon (not just triangles). We just need to be able to use it.
Add the following line before the declaration of the main function:
extern double polyArea( const double[], const double[], int );.
Then use it in your main function instead of quadArea to compute the areas of both versions of this figure.
You will want to make sure that you have the correct files in your project when you test this!
Once it seems to work, place the function calls you used to compute your area in the Answer box for this question.
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | Something like: area1 = polyArea(Xs1, Ys1, 4); area2 = polyArea(Xs2, Ys2, 4); |
Does it matter here? Why or why not?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | Here it does not matter, since the first and last vertex are identical. There are only four distinct vertices in the array, so it is treated as a quadrilateral either way. |
It would have been possible to have that declaration appear automatically if it was inside the triarea.h file, but I chose not to do that.
Why would I resist placing that declaration in the header file?
(Consider this recitation as a whole).
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | If we chose to use the heroarea.cpp for triangles instead of polyarea.cpp, then there would be a declaration for a non-existent function, which could interfere with compilation. |