Introduction to Programming

HW1 (due Jan. 21)

This page is located at http://www.cs.wpi.edu/~alvarez/CS1005/HW1/

Specifically, your program should prompt the user to input two positive (floating point) numbers, then compute the arithmetic and geometric means of these numbers, and finally report these means to the user. Careful documentation of your program is required. Further details are provided below.

- Review the material from recent lectures, chapter 2, and sections 3.1-3.3 of Astrachan. Remember that program examples are also available in the appropriate subdirectories of the directory named /cs/cs1005/ on the CCC Unix network.
- Log into your CCC Unix account. If you haven't already done so, I suggest that you create a directory named CS1005; inside the CS1005 directory, create a subdirectory named HW1. Keep all files for this assignment in that directory.
- Edit a (new) text file named hw1.cxx using emacs, pico, vi, or your favorite text editor. Write the basic outline of a C++ source program in this file: the appropriate #include directives, the declaration of the main function, etc. Initially give the main function the smallest possible body (can't leave the body completely empty though; why?). Compile your file using the g++ compiler. If the compiler reports errors or warnings, try to fix your source program before proceeding further.
- Fill in the body of the main() function in the file hw1.cxx
as follows:
int main() { FYI(); float num1 = GetNumber(); float num2 = GetNumber(); ReportArithmeticMean(num1, num2); ReportGeometricMean(num1, num2); return 0; }

This decomposes your program into several chunks, each involving a call to a function. - Write C++ statements in the file hw1.cxx that define the
functions FYI, GetNumber, ReportArithmeticMean, and
ReportGeometricMean that are needed to complete your program.
You may find it convenient, and you should feel free,
to further decompose the work of some of the above functions
in terms of additional functions for improved readability/flexibility.
Both a function header and a function body are required for
each function. GetNumber() should return an object of type float,
while the other three functions should have void as their return type.
Desired behaviors of the various functions are suggested by their
respective names, and are more explicitly described here:
- FYI: prints a short message on the screen describing the overall purpose of the program.
- GetNumber: prompts the user to input a positive floating point number, reads this number from cin, and returns this number.
- ReportArithmeticMean: computes the arithmetic mean of the two input parameters and reports the resulting value on the screen, including a brief explanation of how this value was computed (see sample run shown below). Recall that the arithmetic mean of two numbers x and y is one-half of their sum x+y.
- ReportGeometricMean: computes the geometric mean of the two input parameters and reports the resulting value on the screen, including a brief explanation of how this value was computed (see sample run shown below) Recall that the geometric mean of two numbers x and y is the square root of their product x*y.

Compile your program using the g++ compiler. Debug (fix) it as necessary, addressing any compiler error messages or warnings. It's good practice to write one function at a time, checking for errors or warnings before going on to the next function.

- Run the executable file resulting from compilation of your
program. Try out several combinations of values of the input
parameters. If necessary, fix the source code so that the
program produces correct output values in all cases.
Once you're satisfied that the program is working correctly,
generate a script file named hw1.script that shows several
runs of your program. To do this, just type script hw1.script
at the Unix prompt. Then run your program as usual. When you're
done, type exit to discontinue the logging process.
A script file containing three sample runs is shown below. Your script file may look slightly different, but the overall format should be more or less the same. In particular, note the format in which the output values are reported; your program should include brief explanations of how the values were computed, in the format shown below.

Script started on Fri Jan 14 18:50:20 2000 %hw1 This program computes the arithmetic and geometric means of two user-supplied positive numbers. Input a positive number and press enter: 2 Input a positive number and press enter: 4 The arithmetic mean of the two numbers is: (2+4)/2 = 3 The geometric mean of the two numbers is: (2*4)^(1/2) = 2.82843 %hw1 This program computes the arithmetic and geometric means of two user-supplied positive numbers. Input a positive number and press enter: 1 Input a positive number and press enter: 9 The arithmetic mean of the two numbers is: (1+9)/2 = 5 The geometric mean of the two numbers is: (1*9)^(1/2) = 3 %hw1 This program computes the arithmetic and geometric means of two user-supplied positive numbers. Input a positive number and press enter: 1.54 Input a positive number and press enter: 3.4925 The arithmetic mean of the two numbers is: (1.54+3.4925)/2 = 2.51625 The geometric mean of the two numbers is: (1.54*3.4925)^(1/2) = 2.31915 %exit exit script done on Fri Jan 14 18:51:03 2000

- The source code for your program in hw1.cxx should be
carefully commented. Provide additional documentation
for your code in a file named hw1.readme, following the
WPI CS documentation standard

.

- hw1.cxx
- hw1.readme
- hw1.script