/*========
  Fall 2024 - CS 111
  Week 15 Lab Exercise

  date: 2024-12-13
========*/

/*---
    USING pair-programming
    *   COPY and PASTE the contents of this 
        file into a lab15.cpp file within the CS50 IDE

    *   ADD/MODIFY the parts asked for below
        (one student saying what to type,
        the other student typing it into the CS50 IDE)

    *   each time you want to compile:
        in a CS50 terminal that is open to the folder         
        CONTAINING this .cpp file, 
        ("Open in Integrated Terminal"), type:
      
        g++ lab15.cpp -o lab15

    *   IF it compiles with no errors:
        to run: in that same CS50 terminal that is open to the folder
        CONTAINING this .cpp file, type:

        ./lab15

    *   When you are satisfied with its output, create an
        example output file by typing:

        ./lab15 > lab15-out.txt

    *   Download copies of your resulting lab15.cpp and lab15-out.cpp
        by right-clicking on their names in the file explorer on the
        left of the CS50 IDE, and use Gmail to MAIL a copy of these
        files to BOTH of you.

    *   And, EACH of you should SUBMIT these TWO files
        lab15.cpp and lab15-out.txt to Canvas
---*/

/*---
    by: PUT BOTH of YOUR NAMES HERE 
    last modified: 2024-12-13
---*/

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

/*--- WEEK 15 LAB EXERCISE - PROBLEM 1 ---*/

/*---
    The purpose of this problem is to practice writing
    a for-loop.

    During Week 15 Lecture 1, we REFACTORED our get_smallest
    function from Week 14 Lecture 2, replacing its 
    count-controlled while loop with a for-loop.

    Below is Week 13 Lecture 1's function cheer, which uses a
    count-controlled while-loop.

    REFACTOR it -- MODIFY its function body -- as follows:
    *   Replace its current while-loop with an
        appropriate for-loop.

    *   As you are doing so, include at least ONE appropriate use
        of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=)

    (Its tests are already in the main function below, ready to help
    you determine if your refactoring was successful.)
----*/

/*===
    signature: cheer: int -> int
    purpose: expects the number of HIPs desired in a cheer,
        has the SIDE-EFFECT of printing THAT many
        HIP cheers to the screen, each on its own line,
        followed by HOORAY!, and then it returns how many
        HIPs it actually printed. (If called with a negative
        number, it just prints HOORAY! and returns 0)

    tests:
        cheer(3) == 3
        and also has the side-effect of printing to the screen:
HIP
HIP
HIP
HOORAY!

        cheer(5) == 5
        and also has the side-effect of printing to the screen:
HIP
HIP
HIP
HIP
HIP
HOORAY!

        cheer(0) == 0
        and also has the side-effect of printing to the screen
HOORAY!

        cheer(-5) == 0
        and also has the side-effect of printing to the screen
HOORAY!
===*/

int cheer(int num_hips)
{  
    int count = 0;

    // print desired quantity of HIPs

    while (count < num_hips)
    {
        cout << "HIP" << endl;
        count = count + 1;
    }

    // print a final HOORAY! after all of those HIPs 
    
    cout << "HOORAY!" << endl;
    return count;
}


/*--- WEEK 15 LAB EXERCISE - PROBLEM 2 ---*/

/*---
    The purpose of this problem is to get more practice
    writing a for-loop.

    Below is Week 13 Lecture 1's function sum_ints, which uses a
    count-controlled while-loop.

    REFACTOR it -- MODIFY its function body -- as follows:
    *   Replace its current while-loop with an
        appropriate for-loop.

    *   As you are doing so, include at least ONE appropriate use
        of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=)

    (Its tests are already in the main function below, ready to help
    you determine if your refactoring was successful.)
----*/

/*===
    signature: sum_ints: int -> int
    purpose: expects a positive integer, and returns the
        sum of the integers from 1 to (and including) thart
        positive integer.
        (if given 0 or a negative integer, it returns 0)

    tests:
        sum_ints(4) == (1 + 2 + 3 + 4)
        sum_ints(2) == (1 + 2)
        sum_ints(0) == 0
        sum_ints(-5) == 0
***/

int sum_ints(int up_to_val)
{
    // want to start summing at 1
  
    int next_num = 1;

    int sum_so_far = 0;

    // repeatedly add the next number up to desired number
    
    while (next_num <= up_to_val)
    {
        sum_so_far = sum_so_far + next_num;
        next_num = next_num + 1;
    }

    // when done adding those numbers, return the resulting sum
    
    return sum_so_far;
}


/*--- WEEK 15 LAB EXERCISE - PROBLEM 3 ---*/

/*---
    The purpose of this problem is to get more practice
    writing a for-loop.

    Below is Week 13 Lecture 2's function vertical, which uses a
    count-controlled while-loop.

    REFACTOR it -- MODIFY its function body -- as follows:
    *   Replace its current while-loop with an
        appropriate for-loop.

    *   As you are doing so, include at least ONE appropriate use
        of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=)

    (Its tests are already in the main function below, ready to help
    you determine if your refactoring was successful.)
----*/

/*===
    signature: vertical: string -> int
    purpose: expects any string, has the SIDE-EFFECT
        of printing that string to the screen in
        a "vertical" fashion, and returns the
        length of the string
    tests:
        vertical("moo") == 3
        and has the side-effect of printing to the screen
m
o
o
         vertical("oink") == 4
         and has the side-effect of printing to the screen
o
i
n
k
         vertical("") == 0
         and does not print anything to the screen
===*/

int vertical(string desired_msg)
{
    int curr_loc = 0;

    // print each character of desired_msg on its own line

    while (curr_loc < desired_msg.length())
    {
        cout << desired_msg.at(curr_loc) << endl;
        curr_loc = curr_loc + 1;
    }

    return desired_msg.length();
}


/*--- WEEK 15 LAB EXERCISE - PROBLEM 4 ---*/

/*---
    The purpose of this problem is to get more practice
    writing a for-loop.

    Below is Week 14 Lecture 1's function sum_array, which uses a
    count-controlled while-loop.

    REFACTOR it -- MODIFY its function body -- as follows:
    *   Replace its current while-loop with an
        appropriate for-loop.

    *   As you are doing so, include at least ONE appropriate use
        of a C++ "SHORTHAND" operator (++, --, +=, *=, /=, or -=)

    (Its tests are already in the main function below, ready to help
    you determine if your refactoring was successful.)
----*/

/*===
    signature: sum_array: double[] int -> double
    purpose: expects an array of numbers and its size,
        and returns the sum of all of the elements in
        that array.

    tests:
        if I have:
        double my_list[5] = {10, 20.1, 3, 4, 100};

        sum_array(my_list, 5) == 137.1

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

        sum_array(my_quants, 4) == 115
===*/

double sum_array(double nums[], int size)
{
     int i = 0;  
     double sum_so_far = 0;

     // add each array value to the sum-in-progress

     while (i < size)
     {
        sum_so_far = sum_so_far + nums[i];
        i = i + 1;
     }

     return sum_so_far;
}


/*---
   test the functions above, write one MORE for-loop, and 
   reflect on tools HERE PLEASE
---*/

int main()
{
    cout << boolalpha;

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

    cout << endl;
    cout << "You should see 3 HIPs then a HOORAY!, " << endl
         << "    followed by true: " << endl;
    cout << (cheer(3) == 3) << endl;

    cout << endl;
    cout << "You should see 5 HIPs then a HOORAY!, " << endl
         << "    followed by true: " << endl;
    cout << (cheer(5) == 5) << endl;

    cout << endl;
    cout << "You should see 0 HIPs then a HOORAY!, " << endl
         << "    followed by true: " << endl;
    cout << (cheer(0) == 0) << endl;

    cout << endl;
    cout << "You should see 0 HIPs then a HOORAY!, " << endl
         << "    followed by true: " << endl;
    cout << (cheer(-5) == 0) << endl;

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

    cout << (sum_ints(4) == (1 + 2 + 3 + 4)) << endl;
    cout << (sum_ints(2) == (1 + 2)) << endl;
    cout << (sum_ints(0) == 0) << endl;
    cout << (sum_ints(-5) == 0) << endl;

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

    cout << endl;
    cout << "Should see a vertical moo followed by true:"
         << endl;
    cout << (vertical("moo") == 3) << endl;

    cout << endl;
    cout << "Should see a vertical oink followed by true:"
         << endl;
    cout << (vertical("oink") == 4) << endl;

    cout << endl;
    cout << "Should see just true:"
         << endl;
    cout << (vertical("") == 0) << endl;

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

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

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

    /*---
    WEEK 15 LAB EXERCISE - PROBLEM 5

    For one last bit of for-loop practice:

    Write a for-loop, as well as local variable declarations as needed,
    to call at least one of the non-main functions in this program at least
    10 times, displaying something readable to the screen as a result of each
    function call.

    HINTS: Consider:
    *   Does your chosen function have side-effects? 
    *   Does your chosen function return a value you might like to print?
    ---*/








    /*---
    WEEK 15 LAB EXERCISE - PROBLEM 6

    Type your pair's answer to the following in the comment below,
    as a bit of end-of-semester reflection:

    One could think of different types of programming statements and
    data structures as TOOLS for solving problems.

    What's ONE of these tools you think you will likely use
    going forward, and why?

    (Note: your answer does not need to be an entire paragraph;
    several sentences is fine.)
    ---*/

    /*--- TYPE YOUR PROBLEM 6 ANSWER HERE:








    ---*/
    
    return EXIT_SUCCESS;
}