`
guoyiqi
  • 浏览: 965362 次
社区版块
存档分类
最新评论

C++小测1

 
阅读更多
CorrectIncorrectUngraded
1.
This is the first in a series of thirteen recitation activities for CMPSC 122. These activities are designed to be performed in a computer lab with some assistance from the instructor, teaching assistant, or learning assistants. All activities have a fifty-minute timer attached to them -- so complete as much as is feasible within that time period.

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?

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): There should be four C++ Source files (triarea.cpp, quadarea.cpp, heroarea,cpp, polyarea.cpp)



CorrectIncorrectUngraded
2.
It is common for a large project to be divided into many source files, instead of being one huge program file. Today's exercise is very small, so might not be a representative example, but the homework for this course will develop into a large project, with new components added incrementally. This recitation will then give an introduction to programming with many files in Visual Studio.

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?

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): There will be multiple declarations for some of the functions in this program!


CorrectIncorrectUngraded
3.
To correct the reported error, click on the quadarea.cpp and heroarea.cpp entries in the Solution Explorer, and Remove them from the Project (but don't erase the files!)

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?)

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): This uses the calculation in polyarea.cpp.


CorrectIncorrectUngraded
4.
Not only should the program build correctly, but it should run successfully and produce the correct results.

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?

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): None! The interface, as defined in triarea.h, has been preserved.


CorrectIncorrectUngraded
5.
The principles of separate compilation allow different implementations for a sub-problem to be substituted interchangeably, if they have the same interface.

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?

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): Still, no changes should be required!


CorrectIncorrectUngraded
6.
One might note that there are five data vertices listed for a four-sided figure; one might further note that the first and last vertices are the same.

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?

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): They are unequal. One is actually two large.


CorrectIncorrectUngraded
7.
We can actually get enough information to diagnose the problem.

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?

 
 
Table for Individual Question Feedback
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.


CorrectIncorrectUngraded
8.
Instead of figuring out how to fix the program to correctly handle both concave and convex polygons, we should just make the program to the correct thing in the first place.

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.

 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): Something like:
area1 = polyArea(Xs1, Ys1, 4);
area2 = polyArea(Xs2, Ys2, 4);


CorrectIncorrectUngraded
9.
For the last question, we had arrays of five elements describing a four-sided figure. One might be tempted to use either 5 or 4 as an actual argument to the polyArea function.

Does it matter here? Why or why not?

 
 
Table for Individual Question Feedback
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.


CorrectIncorrectUngraded
10.
The extern declaration in the last question declares the function and says that it appears in some other program file.

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).

 
 
Table for Individual Question Feedback
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.
编程一对一辅导:qq 928900200

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics