CS 46A Lab 9, Style
Problems
Name:
CS46A Instructor:
1. How would you re-write the following code fragment to adhere to the coding style guide regarding blanks:
if(fabs(x)!=0.0)
return(exp(x)+(0.435/x)*(exp(x)-1.0)-1.564);
else
return-0.1290;
2. How would you re-write the following code fragment to adhere to the coding style guide regarding blanks (and indentation):
for(i=0 ; i<MAX ; i++) if(a[ i ] < 0.0)
cout<<"Negative value found for i = "<<i<<"\n";
3. Suppose y is defined mathematically as follows:
ì
0.0 if x is <= 0.0
y = í
x if x is > 0.0 and x <= 1.0
î
1.0 if x > 1.0
Complete the following if statement so that y is set to the correct value. Make appropriate use of { }.
if (x > 0.0)
if (x > 1.0)
y = 1.0;
...
4. The waiting room program allows a receptionist to record the time a client arrives and the time the client is served. (Only one client can be waiting.) The program keeps a running total of the number of clients served and the number of minutes they waited. At the end of the day the program displays the total number of minutes clients waited, and the average wait time. Here is a sample run:
enter arrival time: 9:00 AM
you entered: 9:00AM
enter served time: 9:15 AM
you entered: 9:15AM
difference = 15
total = 15 minutes
More? (y/n): y
enter arrival time: 10:30 AM
you entered: 10:30AM
enter served time: 1:00 PM
you entered: 1:00PM
difference = 150
total = 165 minutes
More? (y/n): y
enter arrival time: 2:15 PM
you entered: 2:15PM
enter served time: 4:30 PM
you entered: 4:30PM
difference = 135
total = 300 minutes
More? (y/n): n
total = 300 minutes
average = 100 minutes
The program is divided into three files: main.cpp, time.h, and time.cpp. The first file contains the implementation of main(), a control loop that perpetually prompts the receptionist for arrival and served times. It computes and displays the total and average waiting times. The second file, time.h, contains the declaration of the time class used in main(). Time is represented by the number of minutes elapsed since midnight. (So clients can't spend the night!) The last file, time.cpp, contains the implementations of the member functions declared in time.h. These include functions for reading and writing times, as well as adding and subtracting times.
Unfortunately, all three files contain a number of violation of the department style guidelines. The use of white space (indenting and blank lines) doesn't make sense. Comments are minimal. Names of some variables are unhelpful. Delimiters aren't properly aligned. Header comments are absent. Naming conventions are ignored.
Copy the three files into a new waiting room project. Fix all violations of the department style guidelines. Build and test your program. Turn in printouts of the three files together with a printout of a sample run.