/*---
    get_smallest example from 
        LA-run Final Exam Reviews in 9:00 am AND 3:00 pm
        CS 111 - Week 15 Labs
    
    presented by: Raul Cano Canul, Kai Frimodig, Enrique Lopez, Ambrose Sturgill
    adapted by:   Sharon Tuttle
    last modified: 2025-12-12
---*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

/*===
    signature: get_smallest: double[] int -> double
    purpose: expects an array of numbers and its size, and returns
             the value of the smallest element in that array.
             Will happen to return 0.0 for an empty array.
    tests:
        double scores[5] = {7, 8, 28, 4, 22};
	get_smallest(scores, 5) == 4

        double widget_set[3] = {-3.5, -3.5, -3.5};
        get_smallest(widget_set, 3) == -3.5

        to "SIMULATE" passing an EMPTY array -- um, KLUGE by calling
	with a size of 0?!?

        get_smallest(widget_set, 0) == 0.0
===*/

double get_smallest(double values[], int size)
{
    double smallest_so_far;

    // if array's size is 0, just return 0.0

    if (size == 0)
    {
        return 0.0;
    }

    // otherwise, look for the array's smallest value

    else
    {
        // the first thing in the array will be the
	//     smallest item we have checked so far
	
        smallest_so_far = values[0];

        // now "walk" through the array, looking to see if
	//     anything in it is smaller than that

        for (int index = 0; index < size; index++)
        {
	    // have we found a "new" smallest thing?
	    
            if (values[index] < smallest_so_far)
            {
                smallest_so_far = values[index];
            }
        }			

        // when we are done checking all of the array items,
	//    smallest_so_far should contain the smallest value
	//    in the array

        return smallest_so_far;
    }
}

/*---
   test the function above
---*/

int main()
{
    cout << boolalpha;

    cout << endl;
    cout << "*** Testing: get_smallest ***" << endl;

    double scores[5] = {7, 8, 28, 4, 22};

    cout << (get_smallest(scores, 5) == 4) << endl;

    double widget_set[3] = {-3.5, -3.5, -3.5};

    cout << (get_smallest(widget_set, 3) == -3.5) << endl;

    // to "SIMULATE" passing an EMPTY array -- um, KLUGE by 
    //     calling with a size of 0?!?

    cout << (get_smallest(widget_set, 0) == 0.0) << endl;

    cout << endl;
    return EXIT_SUCCESS;
}