Rules for Structured Programming
1. Put only one statement on a line.
2. Use meaningful variable names and function names.
3. Use all CAPITALS for names of constants. Use lowercase or mixed
capital and lowercase for
names of variables.
4. Split long lines so that they fit on the page without running over.
Indent line continuation.
5. Divide program into functions.
6. Each function should perform one task.
7. Each function must have a comment.
8. Put prototypes above main, functions after main (if you have a main
function).
9. Skip a line between functions.
10. Skip a line after the declaration in main and in each function.
11. Skip lines when useful for clarity, but do not double-space your
program.
12. Never put more than one } on a line.
13. Put each } on a line by itself.
14. With a few exceptions, align { and }.
15. For functions, put { and } on separate lines.
16. For loops and if statements, you may put { on the line with the
keyword and put } on a line
by itself aligned under the keyword.
17. My preference is that you do not comment closing braces (other
instructors like it).
18. Follow rules of indenting.
How to indent:
a.
Each indent must be at least 5 spaces.
b. Each time a construct (loop, if statement) occurs inside another
construct, indent
5
more spaces.
c. Move back out at the end of the construct.
d. Align statements that are indented at the same level.
What to
indent:
a. Indent body of main and body of a function (not the header or
comment).
b. Indent body of a loop.
c. Indent body of an if
statement or an else statement.
d. Indent statements in a switch
statement (indent each case,
and further indent
each action).
e. Indent items within a struct definition or declaration.
f. Indent continued lines.
Example:
// Program gives examples of structured
programming techniques
#include <iostream>
using namepace std.
void readarr(int &,int &);
void changearr(int &,int);
void printarr(int arr,int n);
const int SIZE=50;
class justtoshowone {
int firstitem;
string seconditem;
double thirditem;
};
int main()
{
int n,arr[SIZE];
justtoshowone example;
readarr(arr,n);
changearr(arr,n);
sum = sumarray(arr,n);
cout << "the sum of the array is"
<< sum << endl;
printarr(arr,n);
return 0;
}
// reads n and then reads n values into an array,
// changing values outside the range -100 to
+100
void readarr(int &n,int arr[])
{
cin >> n;
while (n < 0 || n >= SIZE)
cin
>> n;
for(int i = 0; i < n; i++) {
cin >>
arr[i];
if (arr[i] >
100)
arr[i] -= 25;
else if (arr[i]
< -100)
arr[i] += 25;
cout <<
arr[i] << endl;
}
return;
}
// changes array values based on sign of original value;
// counts changes
void changearr(int arr[],int n)
{
int increased=0, decreased=0,negated=0;
for(int i = 0; i < n; i++) {
if
(arr[i] == 0) {
arr[i]++;
increased++;
}
else if (arr[i]
< 0) {
arr[i] = -arr[i];
negated++;
}
else {
arr[i]--;
decreased++;
}
}
cout << "# increased is " <<
increased <<
<< "# decreased is " << decreased <<
<<
"# negated
is " << negated << endl;
return;
}
// prints resulting
array
void printarr(int arr[],int n)
{
for(int i = 0; i < n; i++)
cout <<
arr[i];
return;
}