// findMin.cc   Finding the minimum value in an array
// P. Conrad   10/17/05

#include <iostream>
using namespace std;

void printArray(int *a, int size, const char * const arrayName);
// a is a pointer to the first element in the array
// equivalent: void printArray(int a[], int size);


int minOfArray(int *a, int size);
int locationOfSmallest(int *a, int size);
// return minimum value in the array

void sortArray(int * const a, const int size);

void sortArray(int * const  , const int     );

void swap(int &a, int &b) // &signify that we are passing by reference (alias)
{
  int temp = a;
  a = b;
  b = temp;
}

void loadUpArray(int * const a, const int size)
{
  cout << "Enter " << size << " values for array separated by spaces: ";
  for (int i=0; i<size; i++)
    cin >> a[i];

}

int main(void)
{

  const int arraySize = 5;

  int b[arraySize];

  // load up array from user input

  loadUpArray( b, arraySize);

  // dump the contents of both arrays

  cout << "=====The b array=======\n";
  printArray(b,arraySize,"b");


  // find the minimum of array b and print it

  int smallest;
  smallest = minOfArray(  b, arraySize   );
  cout << "The smallest value is: "  << smallest << endl;
  
  // find location of minimum value

  // for example, if the array is   int b[] = {3, 17, 16, 5, -1, 8, 9};
  //  int locationOfSmallest = locationMinValue(b, arraySize);
  // if I do "cout << locationOfSmallest << endl", it should print out 4

  int whereIsTheSmallestValue = locationOfSmallest(b, arraySize);
  cout << "The smallest value is in location: " << whereIsTheSmallestValue << endl;

  // Now swap the smallest value with the value in position 0

  sortArray(b,arraySize);

    cout << "=====The sorted b array=======\n";
  printArray(b,arraySize,"b");

  return 0;
    
   
}


void printArray(int *a, int size, const char * const arrayName)
{
  // a is a pointer to the first element in the array
  // equivalent: void printArray(int a[], int size);

  for (int i=0; i<size; i++)
    cout << arrayName << "[" << i << "] = " << a[i] << endl; 

}


int minOfArray(int *a, int size)
{
  // return minimum value in the array

  int min = a[0];

  // I know at this point that max is the maximum value
  // of the elements in the array from 0 to 0

  for (int i=1; i< size; i++)   // or use   for (int i=1; i<=4; i++)
    {
      min = ( a[i] <= min ? a[i] : min );


      // I know at this point that min is the minimum value
      // of the elements in the array from 0 up through and including i
    }

  return min;

}


int locationOfSmallest(int *a, int size)
{
  // return minimum value in the array
  int locationOfMin = 0;

  // I know at this point that max is the maximum value
  // of the elements in the array from 0 to 0

  for (int i=1; i< size; i++)   // or use   for (int i=1; i<=4; i++)
    {
      locationOfMin = ( a[i] < a[locationOfMin] ? i : locationOfMin );
    }
  return locationOfMin;
}


int locationOfSmallest(int *a, int start, int size)
{
  // return minimum value in the array

  int locationOfMin = start;

  // I know at this point that max is the maximum value
  // of the elements in the array from 0 to 0

  for (int i=start +1 ; i< size; i++)   // or use   for (int i=1; i<=4; i++)
    {
      locationOfMin = ( a[i] < a[locationOfMin] ? i : locationOfMin );
    }

  return locationOfMin;

}



void sortArray(int * const a, const int size)
{

  for (int i=0; i<size - 1; i++)
    {
      int location = locationOfSmallest(a, i , size);
      swap(a[i],a[location]);
    }

}

