using namespace std;1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// program-structure/ulam.cpp - Read number, print its Ulam sequence.
// Michael Maus, 2004-10-26
//========================================================= includes
#include <iostream>
using namespace std;
//======================================================= prototypes
int nextUlam(int x);
//============================================================= main
int main() {
int n; // Ulam sequence start
cout << "Enter an integer to see its Ulam sequence." << endl;
while (cin >> n) {
cout << "Ulam sequence for " << n << " is " << n;
while (n > 1) {
n = nextUlam(n);
cout << " " << n;
}
cout << endl;
}
return 0;
}
//========================================================= nextUlam
int nextUlam(int x) {
int result;
if (x%2 == 0) { // if even
result = x / 2;
} else { // if odd
result = 3*x + 1;
}
return result;
}
|
A program that defines classes or numerous methods is divided into many source files. To pass common definitions between these files, header files are created which contain declarations.