CS 1440 Lab 2
Cover sheetPrevious ActivityNext Activity

Activity 2-3: Documenting Programs

Activity Procedure

There are two kinds of comments (text ignored by the compiler) in C++.
All text following "//" until the end of the line is ignored by the compiler. This is an easy way to insert short items of documentation. For longer (multi-line) comments, we use "/*" to open the comment and "*/" to close it. So these are equivalent:

// It is
// a very
// nice day.
 
and
 
/* It is
a very
nice day. */

Every program you turn in this term should include the following:
There is room for differences in style here. For example, these are equivalent headers for the last program:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Program:        ave.C
* Date:           1/22/01
* Programmer:     Fred Flintstone
* Class:          CS 1440, Dr. Alfred E. Newman
*
* This program prompts the user for three integers, then calculates and prints
* the average.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/* ave.C, 1/22/01 by Fred Flintstone, written for CS 1440, Dr. Alfred E. Newman
Prompts the user for three integers, then calculates and prints the average. */


// ave.C, 1/22/01 by Fred Flintstone, written for CS 1440, Dr. Alfred E. Newman
// Prompts the user for three integers, then calculates and prints the average.



The main Function

C++ programs are written in pieces called functions. A function has a type, a name, a list of arguments, and a body. Every program must have one function whose name is "main". Our garden program has only one function (main) and so will most of the programs we write at the beginning of our class. The line

int main()

says that the type of value produced by the main function is an integer, that the function's name is main, and that its argument list is empty. Press the space bar and look at the last line of the program

return 0;

That zero is the value being produced by main. Zero is an integer and that's why the type of value produced by main is an int. Always start your main function with the line "int main (void)" and make the last line of main be "return 0;". Advanced programmers may change some of those things, but we will not. An equivalent way to write int main() is int main(void), which makes more explicit the fact that there are no arguments passed to main.

Variable Declarations

        int x,y,z;
        double average;

A variable declaration statement in C++ consists of a data type and a list of identifiers separated by commas. All statements in C++ end with a semicolon. In our ave program, we had two declarations:

       int x,y,z;
       double average;

We could have written this as:

        int x;
        int y;
        int z;
        double average;

The advantage of using separate declarations is that it allows for more documentation:

        int x; // first grade
        int y; // second grade
        int z; // third grade
        double average; // average of the three grades

Edit ave.C so that it is fully documented including a header using your relevant information.