#include <iostream>
#include <iomanip>
#include <cstdlib>

const int ARRAYSIZE = 10;

// the function prototypes
void printArr (int[], int, int=15);
int readArr (int[], int);
void swap (int &v1, int &v2);
void sortArr (int arr[], int sz);
int findMin (int arr[], int start, int end);


main()
{
 int size;            // contains how many values are actually
                      // in the array
 int arr[ARRAYSIZE];
 int i;
 int temp;
 int pos;
 int target;

 cout << "Welcome" << endl << endl;

 cout << "Fill in the array with values" << endl;
 size = ARRAYSIZE;
 for (i = 0 ; i < size; i++)
     arr[size - i - 1] = rand() % 100;

 // call a function to print an array
 cout << endl << "The values in the array are:" << endl;
 printArr(arr, size);

 // sort the array
 sortArr (arr, size);

 // call a function to print an array
 cout << endl << "The values in the Sorted array are:" << endl;
 printArr(arr, size);

 cout << endl << "Goodbye" << endl;
}

// The following function will search for a value in an array
//   The function will return -1 if the value is not in the array.
//   The function will return the position of the value
//    if the value is in the array.

void sortArr (int arr[], int sz)
{
 int pos;
 int i;

 for (i = 0; i < sz; i++)
    {
     pos = findMin(arr, i, sz);
     swap (arr[i], arr[pos]);

     // print out to show the sorting
     cout << "Min Pos: " << pos << " Arr: ";
     printArr(arr, sz);
    }
}

int findMin (int arr[], int start, int end)
{
 int minPos = start;
 int i;

 for (i = start + 1; i < end; i++)
   if (arr[i] < arr[minPos])
      minPos = i;

 return minPos;
}

void swap (int &v1, int &v2)
{
 int temp = v1;
 v1 = v2;
 v2 = temp;
}

// The following function will read in values and store them
//  into an array.
// 
// Parameter 1: the array
//           2: the maximum size of the array
// Return: the number of values actually stored in the array.

int readArr(int arr[], int max)
{
 int i = 0;
 int fileSize;
 int temp;

 // read values into the array (feof)
 while (cin >> temp)
    {
     // verify the number of values read
     if (i >= max)
        {
         cout << "Error: attempted to overfill the array of size "
              << max << endl;
         fileSize = i + 1;
         while (cin >> temp)
              fileSize++;
         cout << "File contains " << fileSize << " values." << endl;
         return i;
        }
     arr[i] = temp;
     i++;
    }


 return i;
}



// The following function will print an array with 15 values
//  per line as a default.  If a third parameter is given, this
//  parameter will specify the number of value per line.
//
//  Parameter 1: The array
//            2: The size of the array
//            3: number of value per line (15 by default)
//  Returns nothing
void printArr(int arr[], int sz, int perLine = 15)
{
 int i;

 // Display the values in the array
 for (i = 0; i < sz; i++)
     {
      // cout.width(5);
      cout << setw (4) << arr[i] << " ";
      //cout << arr[i] << "  ";
      if ((i+1) % perLine == 0)
          cout << endl;
     }

 cout << endl;
}