intDriver.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/templates/code/intDriver.cpp
 
#include <iostream>
#include "ArrayT.h"

using namespace std;

const size_t SIZE{10};

using IntArrayT = ArrayT<int>;

void FillArray(IntArrayT & ary);
void PrintArray(const IntArrayT & ary);
void SortArray(IntArrayT & ary);

int main() {
   srand(static_cast<unsigned int >(time(nullptr)));

   IntArrayT ints;

   FillArray(ints);
   
   cout << "The array is " << endl;
   PrintArray(ints);

   SortArray(ints);
   cout << endl << endl;

   cout << "After the array is sorted it is " << endl;
   PrintArray(ints);

   //cout << ints.BadFun() << endl;

   return 0;
}


void FillArray(IntArrayT & ary){
   for(size_t i = 0; i < SIZE; ++i) {
      ary.push_back(rand() % 100);
   }
}

void PrintArray(const IntArrayT & ary){
   cout << ary[0];
   for(size_t i = 1; i < ary.Size(); ++i) {
      cout << ", " << ary[i];
   }
   cout << endl;
}

void SortArray(IntArrayT & ary){

   for(size_t pos = 0; pos < ary.Size(); ++pos) {
      size_t smallest{pos};

      for(size_t j =pos+1; j < ary.Size(); ++j) {
         if (ary[smallest] > ary[j]) {
             smallest = j;
         }
      }

      if (smallest != pos) {
         auto tmp = ary[pos];
         ary[pos] = ary[smallest];
         ary[smallest] = tmp;
      }
   }
}