
int SumOfSquares(int m, int n)
{
int i, sum;
for(i = 0; i <= n; i++)
sum += i*i;
return sum;
}
int SumOfSquares(int m, int n)
{
if(m < n)
return m*m + SumOfSquares(m+1, n); // Recursive call
else
return m*m;
}
int SumOfSquares(int m, int n)
{
if(m < n)
return SumOfSquares(m, n-1) + n*n; // Recursive call
else
return n*n;
}
int SumOfSquares(int m, int n)
{
int middle;
if(m == n)
return m*m; // Base call
else
{
middle = (m+n)/2; // Note: this is integer division
return SumOfSquares(m, middle) + SumOfSquares(middle+1, n);
}
}






NodeType *Reverse(NodeType *List)
{
NodeType *Head;
NodeType *Tail;
NodeType *RList;
if(List == NULL) // Base Case - the empty list
return (NULL);
else
{
Partition(List, &Head, &Tail); // Note we are passing pointers to Head and Tail, that is Handles
RList = Concat(Reverse(Tail), Head);
return (RList);
}
}
void Partition(NodeType *Lst, NodeType **Hd, NodeType **Tl)
{
if(Lst != NULL)
{
*Tl = Lst->next; // By using a handle we are actually changing what Tail in Reverse() is pointing to.
*Hd = Lst; // This will set Head in Reverse() pointing to the rest of the list
(*Hd)->next = NULL; // This will set the next pointer in the node Head, in Reverse(), pointing to NULL
} // Note the use of parentheses due to binding of *
}
NodeType *Concat(Nodetype *L1, Nodetype *L2)
{ // L1 = reversed tail of a list, L2 = head of a list
NodeType *temp; // A search pointer used to find the end of list L1
if(L1 == NULL)
return (L2); // This is L2 concated to a NULL list
else
{
temp = L1;
while(temp->next != NULL)
temp = temp->next; // Search for end of list L1
temp->next = L2; // Concatenate L2 to end of L1
return L1;
}
}
int Factorial(int N)
{
if(N == 1)
return N;
else
return (N * Factorial(N-1);
}
This function works fine except for certain situations. What would happen if
this function were called with Factorial(0) or Factorial(-1). This will
continue infinitely until memory for call frames is used up or the integer
finally cycles around to the positive and then
finally reduces to 1. In other words there is an arithmetic underflow error
when calculating (-MAXINT - 1).
void ListClass::ReverseIt()
{
Reverse(head);
}