Multi-file programs: a concrete example
All this is rather abstract. Here's a program that
demonstrates a simple math package:
#include
#include "mymath.hpp"
int main(void) {
cout << gcd(1122, 1265) << endl;
return 0;
}
// Function to compute a greatest common
// divisor of two integers.
int gcd(int a, int b);
// ... other math functions here
int gcd(int a, int b) {
// Uses Euclid's algorithm to compute the
// greatest common denominator of a and b
int larger, smaller;
if (a > b) {
larger = a;
smaller = b;
} else {
larger = b;
smaller = a;
}
int remainder = larger % smaller;
if (remainder == 0) {
return smaller;
} else {
return gcd(smaller, remainder);
}
}
Last modified: Wed Jun 21 20:33:28 PDT 2000