Lab #4 FAQ

(Frequently Asked Questions)

Updated: Monday, July 28, at 9:30 am

Here are some Questions and Answers for Lab #4. I've noticed that some students are asking questions similar to these:

******************************
Q: Most of the exercises we've done, including the ones in the book, follow the same approach:
  getData(...);             // prompts user for input
  calculateResults(...);    // calculates values based on input
  displayResults(...);      // prints input and results
Do we need to follow that for this assignment since we don't prompt the user for anything?

A: Not exactly. If you are only doing the first part of the assignment, it is OK to put all of the code in main. The program doesn't require any user-defined functions. If, however, you choose to do the complete extra credit assignment, you will have at least 3 functions (main, plus the 2 functions I've described in the handout). Since there is no user input, you don't need a getData(...) function, but you may want to write a function that prints a line of the table of PI values.


*****************************
Q: Why can't we use any global variables?
A: Global variables have their use, but not in this class. It was necessary to expose you to globals because of the scoping rules. However, a major goal of CS161 is to familiarize the student with inter-function communication, which is best accomplished by passing parameters between functions. If I assign a lab that could benefit from global variables, I will tell you.


****************************
Q: Can we use global constants?
A: Absolutely. If there is a value in your program that is used by several functions, and its value never changes, it should be declared as a global constant. If the constant is used in only one function, it may be better to declare it in the function where it is used.


****************************
Q: Why does my program issue a message like: sqrt: DOMAIN error?
A: The DOMAIN error is most likely because you are taking the sqare root of a negative number. The formula given in the handout to calculate h is:
   h = sqrt(r * r - x * x);
if x is greater than r, which it should never be, this will give you a negative number in parentheses. Due to the rounding errors with floating point math, the value of x actually ends up something like:
   2.000010000
and this is the problem since r is always 2. How you arrive at the value for x each time through the loop will probably determine whether or not you experience the DOMAIN error problem. If you use a method that increments the value of x by a small amount each time through the loop, after thousands of additions, the error may show up. You should try using a different method of calculating the next value of x instead of incrementing the same value for each iteration. If you look closer, you will see that there is a very simple way of determining the x value based on the width of the rectangle (which you know) and the current iteration of the loop (which you also know). Under UNIX, you may get a message saying na or NAN. That's their way of saying DOMAIN error. Usually, it's NAN, which stands for "Not A Number", and that is the error issued when you get an overflow (or underflow).


****************************
Q: In the Extra Credit assignment, to how many decimal places should I display PI?
A: You should follow the formatting shown in the handout. PI is displayed to 10 decimal places.


****************************
Q: I thought that floats were only accurate to 7 digits. How can I display PI to 10 decimal places?
A: Don't use floats. They are not accurate enough.


****************************
Q: My program works on the AIX system, but doesn't work with Borland Turbo C++. I thought C++ worked anywhere?
A: C++ does work everywhere (or should). The problem is that Turbo C++ is a 16-bit environment, so integers are in the range:
   -32,768 to +32,767
The AIX system is a 32 bit system, so integers are in the range:
   -2,147,483,648 to +2,147,483,647
If you remember, I showed you the relative sizes and ranges of the different data types in C++. 32 bit systems hide some of these sticky details from you. I have had a few students turn in Lab #4, and all of them failed under Borland Turbo C++. The reason is because the programs are not using the appropriate data types. I suggest that all students run their programs under Turbo C++ and the AIX platform (which is required of you anyway.) This is especially true for those of you using Borland C++ 5.0 or MSVC++ 4.0 or later. You won't notice that your program is wrong until you get it back with a grade showing that! Turbo C++ is actually very instructive when it comes to sizes of data types.


****************************
Other hints
Read the handout completely. Some students lose points because they failed to following the instructions in the handout


Back to Outline