// Arup Guha
// 10/14/2010
// Example for COT 4210
// Enumerates all ordered pairs with coordinates in between 0 and max,
// inclusive, where max is the value passed to the function enumerator.

void enumerator(int max);
int maximum(int a, int b);
int minimum(int a, int b);
int rank(int x, int y);

int main() {
    
    enumerator(10);
    
    printf("Rank of the ordered pair (3, 4) is %d.\n", rank(2, 8));
        
    system("PAUSE");
    return 0;
}

// Ordered Pair enumerator. Enumerates all ordered pairs with a sum of
// coodinates less than or equal to max.
void enumerator(int max) {
     
    int i, j;
    int cnt = 1;
    
    // First value ranges from 0 to max.
    for (i=0; i<=max; i++) 
    
        // Second value ranges from 0 to i.
        for (j=0; j<=i; j++)
            printf("%d. (%d, %d)\n", cnt++, j, i-j);
}

// Returns the maximum of a and b.
int maximum(int a, int b) {
    if (a > b) return a;
    return b;
}

// Returns the minimum of and b.
int minimum(int a, int b) {
    if (a < b) return a;
    return b;
}

// Returns the rank of the ordered pair (x,y) on the enumerated list, to
// prove countability.
int rank(int x, int y) {
    
    // These are how many ordered pairs have a sum of coordinates less than
    // this ordered pair.
    int sum = x + y;
    int comebefore = sum*(sum+1)/2;    
    
    return comebefore + x + 1;
}
