Answer to Question 08F-2
// countOrderedPairsBeginningWith(i,n) returns a count of
// the ordered pairs of the form (i,j) where 1 <= j <= n and i > j.
int countOrderedPairsBeginningWith(int i, int n)
{
int j, count = 0;
j = 1;
while(j <= n)
{
if(i > j)
{
count++;
}
j++;
}
return count;
}
// countOrderedPairs(n) returns a count of the number of
// ordered pairs (i,j) where 1 <= i,j <= n and i > j.
// For example, the ordered pairs containing integers from
// 1 to 3 are: (1,1), (1,2), (1,3), (2,1), (2,2), (2,3),
// (3,1), (3,2), (3,3). In 3 of those ordered pairs, the first
// number is larger than the second number. So countOrderedPairs(3)
// returns 3.
int countOrderedPairs(int n)
{
int i, j, count = 0;
i = 1;
while(i <= n)
{
count += countOrderedPairsBeginningWith(i,n);
i++;
}
return count;
}