Style
1.
All documentation should be placed in banners ABOVE the related code fragment.
No comments should be in
in-line in submissions.
In-line comments are for debugging. Code that is so long that comments
are needed to
break it up has not
used abstraction e ffectively and should be redesigned.
'In-line' comments means
comments that share a line with executable code. In general, the use of
comments
within an executable
block of code is discouraged because of the above reason. In general, if
in-block comments
are used, they should
be blocked off the same way as banner comments, to maintain continuity.
2. Contents of banners:
//********************************************************
// FILE:
As it is in the current directory
//
// AUTHOR:
Whomever + citations
//
// USAGE COMMENTS: On compilation, usage, etc.
//
// LIBRARIES: Included libraries
and why
//
// TYPES DEFINED: All typedefs, enumerations, classes
//
// CONSTANTS DEFINED:
//
// FUNCTIONS/METHODS DECLARED:
//
// void Myfun1 -- Description of MyFun1
// int Myfun2 --
Description of MyFun2
// Boolean Myfun3 -- Description of MyFun3, this one
//
carries over to another line
// void Myfun4 -- Description
of MyFun4
// myClass Myfun5 -- Description of MyFun5
//
// GLOBAL VARIABLES USED:
//
// GENERAL COMMENTS:
//
//********************************************************
3. Function/Method Prototypes (example template):
Documentation for function prototypes, being an interface only need to
tell the reader what the function
expects and what they can expect from the function. The header will tell
them the parameter types and
their order, as well as the function return type , but these can also be
included in the documentation for
clarity.
//********************************************************
// IDENTIFER: FUNCTION Of Function/Method
//
// PRE: Precondition of the function/method
//
(What conditions are assummed to true prior to calling.)
//
// POST: Postcondition of the method
//
(What conditions are now true when returning,
//
i.e. state any side effects of the function)
//
// INPUT: Description of input parameters
//
// OUTPUT: Descriptions of output parameters
//
// RETURN: Value returned by the function/method
//
//********************************************************
EXAMPLE:
BUBBLE SORT
//*****************************************************
// FILE: BubbleSort.C: Driver that sorts the items
//
in an array into ascending
//
order.
//
// AUTHOR: Frank Carrano, Chapter 9, pp 410-411
//
// LIBRARIES: stdlib.h - for rand
//
iostream.h - standard I/O
//
// FUNCTIONS DEFINED:
// void BubbleSort -- Sorts an array
// void Swap -- swaps two array
elements
//
// VARIABLES: int n - loop counter
// arrayType
a - the array to sort
//
// General Comment:
// This program randomly generates an array of
// integer elements and calls the bubble sort routine
// to sort the array.
//
//*****************************************************
#include <iostream.h>
#include <stdlib.h>
#define MAXSZ 12
typedef int dataType;
typedef dataType arrayType[MAXSZ];
void BubbleSort(dataType a[MAXSZ]);
void Swap(dataType& x, dataType& y);
int
main(void)
{
arrayType a;
int n;
for ( n = 0; n < MAXSZ; n++ )
a[n] = rand();
cout << "The array before sorting is:\n\n";
for ( n = 0; n < MAXSZ; n++ )
cout << a[n] << endl;
cout << endl;
BubbleSort(a);
cout << "The array after sorting is:\n\n";
for ( n = 0; n < MAXSZ; n++ )
cout << a[n] << endl;
cout << endl;
return(0);
}
//*****************************************************
// BubbleSort.C: Sorts the items in an array into
//
ascending order.
//
// PRE: a is an array of MAXSZ items
// POST: a is sorted into ascending order
//
// TYPES: enum boolean {FALSE, TRUE}
//
// VARIABLES: boolean sorted - logical result
// int
pass - current outer loop
// int
index - lower comparison
//
index
// int
nextIndex - upper comparison
//
index
//
// COMMENTS:
// Note that initially, the sorted regions is a[0] and the
// unsorted region is a[sorted..n - 1]. In
// general, the sorted region is a[0..unsorted - 1]
// and the unsorted region is a[unsorted..n - 1]
//
//*****************************************************
void
BubbleSort(dataType a[MAXSZ])
{
enum boolean {FALSE, TRUE};
boolean sorted = FALSE;
int pass, index, nextIndex;
for ( pass = 1; (pass < MAXSZ) && !sorted; pass++ ) {
sorted = TRUE;
for ( index = 0; index < MAXSZ - pass; index++ ) {
nextIndex = index + 1;
if ( a[index] > a[nextIndex] ) {
Swap(a[index], a[nextIndex]);
sorted = FALSE;
}
}
}
return;
}
//***********************************************************
// Swap: Swaps x and y
//
// PRE: None.
// INPUT: dataType& x, y -- data to exchange
// LOCAL: dataType temp -- temporary data
// OUTPUT: dataType& x, y -- swapped data
// POST: Contents of actual locations that x and y
// represent are swapped.
//
//***********************************************************
void
Swap(dataType& x, dataType& y)
{
dataType temp;
temp = x;
x = y;
y = temp;
return;
}