/*---
    CS 111 - Week 14 Lecture 2 - 2024-12-05

    by: Sharon Tuttle
    last modified: 2024-12-05
---*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>   // so I can use file i/o
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(s)
        in that array
    tests:
        if I have:
        double my_list[5] = {10, 20.1, 3, 4, 100};

        get_smallest(my_list, 5) == 3

        if I have:
        double my_quants[4] = {20, 66, 34, -5};

        get_smallest(my_quants, 4) == -5
===*/

double get_smallest(double numbers[], int size)
{
    /* considering pseudocode for this:
       (you do NOT need to include this pseudocode 
       in your finished function body! I am including
       it here just to remind you that we used this
       during class in completing the design of
       get_smallest's function body,
       AFTER writing its signature/purpose/header/tests!)

    *   set up a smallest_so_far variable

    *   walk through array checking each element
        *   if the current element is the smallest
            I have yet seen,
            *   make the current element the new
                smallest element so far

    *   return the smallest so far
    */

    // make the smallest yet seen the 1st element

    double smallest_so_far = numbers[0];

    // look for smaller elements within the numbers array

    int i = 0;

    while (i < size)
    {
        // is the current element the smallest yet seen?
        //     update smallest_so_far if so

        if (numbers[i] < smallest_so_far)
            smallest_so_far = numbers[i];

        i = i + 1;
    }

    // when the while loop finished, smallest_so_far
    //    should now contain the smallest value in
    //    the numbers array

    return smallest_so_far;
}

/*---
   test the function above AND try out some file input/output
---*/

int main()
{
    cout << boolalpha;

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

    double my_list[5] = {10, 20.1, 3, 4, 100};
    cout << (get_smallest(my_list, 5) == 3) << endl;

    double my_quants[4] = {20, 66, 34, -5};
    cout << (get_smallest(my_quants, 4) == -5) << endl;

    //=====
    // TRYING OUT some file input/output!!

    // let's write some waffle toppings to a file!
    // let's say we want the file to contain a quantity
    //    of waffle toppings on its first line,
    //    then that many waffle toppings, 1 per line

    ofstream waffle_fout;
    waffle_fout.open("week14-toppings.txt");

    waffle_fout << 5 << endl;
    waffle_fout << "banana" << endl
                << "bacon" << endl
                << "blueberry" << endl
                << "cheese" << endl
                << "waffle" << endl;

    waffle_fout.close();

    // let's read those waffle toppings!

    ifstream waffle_fin;
    waffle_fin.open("week14-toppings.txt");

    // note that you don't need a user prompt
    //    to read from a file...

    int num_toppings;
    waffle_fin >> num_toppings;

    // now we can make an array of this size to hold
    //     that many waffle toppings
    
    string today_toppings[num_toppings];

    // read in that many waffle toppings into
    //    this array
    
    int i = 0;
    while (i < num_toppings)
    {
        waffle_fin >> today_toppings[i];
        i = i + 1;
    }

    waffle_fin.close();

    // let's show we filled this array

    i = 0;
    cout << endl;
    cout << "Today's toppings:" << endl;

    while (i < num_toppings)
    {
        cout << today_toppings[i] << endl;
        i = i + 1;
    }

    return EXIT_SUCCESS;
}