For each new program you create in this course, a certain standard of style is required as described below.
The idea behind program style is to have a readable program. One that has a professional appearance. For example, see "Display 2.15" ( Savitch, p. 89). Also refer to "Program Style" (Savitch, p. 86-91) and "Chapter Summary" (Savitch, p. 91-92).
// File Name: ruleof72.cpp
// Email Address: you@yourmachine.bla.bla // Course: CSC 125 // Assignment: PreLab 2, Exercise 1 // Date: August 30, 1997 // Last Update: August 31, 1997 // Problem statement: This C++ program computes the number of years it takes // an investment to double using the rule of 72. // Input: Annual interest rate (as a percent of 100) // Output: Number of years it takes an investment to double
2. Use blank lines to separate logical sections and improve program readability. At a minimum, blank lines should separate into groups, the following types of statements: declarations, input, different kinds of processing, and output.
3. Use spaces around = and around operators and after commas and semicolons. For example:
int weight, height;
weight = 3.0 + height * 2.7;
For arrays, there should be NO SPACE between an array name and its index or subscript. For example:
score[i] NOT score [i] or score [ i ]
4. Use comments to describe major sections of programming or where something needs to be clarified. Comments, in general, should be on separate lines but very short comments are acceptable at the end of a line. For example:
int getValue(); // Return the current value
5. Naming Conventions:
For objects (variables) use lower case letters and either camelBackStyle or under_score_style. The names of classes should start with an upper case letter. For constants, the identifier should be all capital letters. For example:
const float PI = 3.14159;
float letterGrade, average_of_all_students;
class Fun
{
// stuff to define class
};
6. Use descriptive object and class names which relate the program to the problem.
7. Indent as discussed in Savitch, p. 86, and illustrated throughout the Savitch text. Use consistent indentation of 3 or 4 spaces and line up the closing bracket } with the corresponding open bracket {.
In particular, indent
if, for and do-while as shown: if( temperature > 98.6)
{
cout << "You have a fever!\n";
cout << "Drink lots of fluids and go to bed!\n";
}
else
{
cout << "You don't have a fever.\n";
cout << "Do your homework.\n";
}
for(i = 1; i <= n; i++)
{
s = s + i;
cout << " Don't Panic!\n";
}
do
{
i = i - 1;
cout << i << endl;
} while (i > 0);
8. Only ONE STATEMENT is permitted on a line.
9. All functions should be written to perform one and only one major task.
Also, all
int sum(int n)
{
// Intent: To sum the positive integers from 1 to n.
// Pre: The variable n must have a value and n > 0.
// Post: The function returns the sum from 1 to n.
}
10. Turn in:
The assumption is, if there is no test data for some option/feature, that option/feature does not work.
A primary factor in grading each project will be your choice of test case(s) and how well you comment your code.