Advanced Placement Computer Science AB:
Quiz 10:  Sorting, Part 3 (treeSort) and apstack
   Sewickley Academy, 2000-2001

See Course Home Page.

This is a programming quiz.  You must use Visual C++.  You may not use any notes and you may not start from a preexisting VC++ project (you must only use newly-created files in a newly-created project).

Note that there are 2 separate questions on this quiz -- one on treeSort, one on apstack.


1.  Write Tree::treeSort in the following program (be sure to create a Tree class and that treeSort is a public static method):
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "apvector.h"

const int BIG_NUM = 500;

typedef apvector<int> intVector;
typedef intVector& intVectorByReference;

// loadVector() loads the vector v with
// random elements between 0 and BIG_NUM

void loadVector(intVectorByReference v)
{
 // load vector with numbers mostly random in [0,BIG_NUM]
 int i, len;
 len = v.length();
 for (i=0;i<len;i++)
   // not perfect, but good enough selector in [0,BIG_NUM]
   v[i] = rand() * BIG_NUM / RAND_MAX;
}
 

void printVector(intVectorByReference v)
{
 // print vector elements separated by two spaces with
 // a carriage return at the end
  int i, len;
  len = v.length();
  for (i=0;i<len;i++)
    cout << v[i] << "  ";
  cout << endl;
}

void testSortFunction(char* name,void(*sortFn)(intVector&v),intVector& v)
{
    intVector v2;
 int time1,time2;
    v2 = v;
 cout << "Starting " << name << endl;
 time1 = time(NULL);
    sortFn(v2);
 time2 = time(NULL);
 cout << "Sort took: " << (time2 - time1) << " seconds" << endl;
}

void main()
{
    intVector v;
    int vSize;

    while (true)
    {
        cout << "Enter size of vector (<0 to exit): ";
        cin >> vSize;
        if (vSize < 0)
            return;
        v.resize(vSize);
        loadVector(v);
        // printVector(v);
        testSortFunction("treeSort",Tree::treeSort,v);
    }
}



2.  Write a program which demonstrates mastery of apstack.  For example, a postfix arithmetic expression evaluator is a fine idea.  While your program may be very brief, it should not be useless (ie, you will be graded both on the quality of the problem you solve as well as the quality of your solution).  Hint:  This should take you only 5-10 minutes.



See Course Home Page.