// myMin.cc  P. Conrad CISC106, Sect 99 11/15/2006

#include <iostream>
#include <cstdlib> // for exit()
using namespace std;

//function myMin
//  return the minimum value in an array of integers
//
//  consumes:
//      v: an array of integers
//      length: the length of that array
//  produces:
//      result: a scalar number from v, such that every element of v
//         is greater than or equal to result.
//
//  Example 1:
//
//       int a[] = {3, 4, 1, 7, -2, 9};
//       cout << myMin(a,6) << endl;
// 
//  Output: -2
//
//  Example 2:
//
//       int b[] = {3, 2, 6, 2, 9};
//       cout << myMin(b,5) << endl;
// 
//  Output: 2
//          
int myMin(int v[], int length)
{
 
  // if the input is the empty vector, return an empty vector

  if (length < 1)
    {
      cerr << "Error" << __FILE__ << "," << __LINE__ << endl;
      cerr << "length is < 1" << endl;
      exit(1);
    }

  // start with the assumption that v[0] is the minimum element
  int min = v[0];

  // precondition: min is the smallest element in elements 0 thru 0

  for (int k=1; k<length; k++) // step through every element in the array
    {
      // invariant: min is smallest among elements 1 thru k-1
      
      if (v[k] < min) // see if this element is smaller than min 
	min = v[k];
      
      // invariant: min is smallest among all elements 0 thru k
    }
  
  // post-condition: min is the minimum element in v

  return min;
}



