/*Type delcarations - NO MEMORY ALLOCATED (EVEN STATICALLY)*/
struct InvRec /*Inventory record with its fields*/
{
int PartID;
float Price;
};
typedef InvRec * InvRecptr; /*Inventory record pointer type*/
typedef InvRec arr[MAXDBSIZE]; /* Array type of pointers to inventory records*/
typedef InvRecptr * indexarr; /* Pointer to a dynamic array of inventory record
pointers*/
void deallocate (indexarr & ptrs, int & size)
readdata (index, dbsize);
cout << "Original data values:" << endl;
printdata (index, dbsize);
sortbyPartID (index, dbsize);
cout << "Data sorted by PartID:" << endl;
printdata (index, dbsize);
cout << "Please input a non-white space key to end the session." <<endl;
cin >> temp;
};
void readrec (InvRec& rec)
/*Pre: Input available as expected.
Post: Record rec filled with input data.*/
{
cin >> rec.PartID;
cin >> rec.Price;
};
void readdata (arr DB, int & size)
/*Pre: DB is an array of MAXDBSIZE inventory records.
Post: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are
filled with input data.*/
{
void readrec (InvRec&);
char YN;
size = 0;
do
{
cout << "More data (Y/y/N/n)? " << endl;
cin >> YN;
if ((YN == 'Y') || (YN == 'y'))
if (size < MAXDBSIZE) readrec(DB[size++]);
else cout << "Database size may not exceed " <<MAXDBSIZE << endl;
else if ((YN != 'N') && (YN != 'n')) cout << "Please answer Y/y/N/n.";
}
while ((YN != 'N') && (YN != 'n'));
};
void printrec (InvRec rec)
/*Pre: rec has data.
Post: The data in rec printed out.*/
{
cout << rec.PartID << ' ' << rec.Price << endl;
};
void printdata (arr DB, int size)
/*Pre: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are
inventory records containing data.
Post: They are all printed out in the order pointed at.*/
{
void printrec (InvRec);
int i;
for (i=0; i<size; i++) printrec(DB[i]);
};
void swap (InvRec & x, InvRec & y)
/*Pre: None.
Post: The values of x and y are swapped.*/
{
InvRec temp;
temp = x;
x = y;
y = temp;
};
int selectsmallestPartID (arr DB, int first, int last)
/*Pre: "first" through "last" elements of the DB array are inventory
records containing data.
Post: A value k is returned such that, for i ranging from first to last,
the relation DB[k].PartID <= DB[i].PartID holds.*/
{
int j, result;
result = first;
for (j=first+1; j<=last; j++)
if (DB[j].PartID < DB[result].PartID) result = j;
return result;
};
void sortbyPartID (arr DB, int size)
/*Pre: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are
inventory records containing data.
Post: for i ranging from 1 to size-2, the relation DB[i].PartID <=
DB[i+1].PartID holds.*/
{
int selectsmallestPartID (arr, int, int);
int i, smallindex;
for (i=0; i<=size-2; i++)
{
smallindex = selectsmallestPartID (DB, i, size-1);
swap (DB[i], DB[smallindex]);
};
};