// 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);
// return minimum value in the array


int main(void)
{

  int b[] = {3, 17, 16, 5, -1, 8, 9};

  // dump the contents of both arrays

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


  // find the minimum of array b and print it

  int smallest;
  smallest = minOfArray(  b, 7   );
  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, 7);
  // if I do "cout << locationOfSmallest << endl", it should print out 4

  int locationOfSmallest = locationMinValue(b, 7);
  cout << locationOfSmallest << endl;


  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;

}

