Mini-Lab: Swap and Find Minimum


Introduction

The purpose of this mini-lab is to develop several helper functions (functions that can serve as building blocks for other functions) that are useful in manipulating items in an array.

For now, develop these functions in a new, bare-bones web page (e.g., copy the COMP 105 web page template).

There will be nothing to hand in for today's mini-lab, but you will need to have these functions finished in order to complete tomorrow's lab.


Exercises:

  1. Review: Create a <script> section in the BODY of your web page, and add the following code:

    
    var names = ["Tom", "Dick", "Harry"]
    alert(names);   // Expected results: "Tom,Dick,Harry" in an alert box.
    
    for ( var i = 0; i < names.length; i++ )
    {
        alert("i = " + i + "; names[" + i + "] = " + names[i]);
           // Expected results: 3 alert boxes;
           //   the first should say "i = 0; names[0] = Tom"
    }
    
    

    Make sure that you understand how to refer to the array, an index into the array, and a value at a particular index in the array before going further.

  2. Swap: Copy the following unimplemented function to the HEAD section of your web page. Replace the comments inside the function with code that implements the simple algorithm described in pseudo-code. Once complete, the function should swap two entries in an array.

    //-------------
    // swap - Exchange the elements at index1 and index2 in the array. 
    // No value is returned, the array itself is modified.
    function swap(theArray, index1, index2) 
    {
        // REPLACE THE FOLLOWING PSEUDO-CODE WITH ACTUAL CODE
        //   Save the value at index1 in the array to a temporary variable. 
        //   Copy the value at index2 to position index1. 
        //   Copy the temporary value into the array at index2. 
    }
    

    Test Swap: In the <script> section in the BODY of your web page, right after the code where you created and tested the ["Tom", "Dick", "Harry"] array, add the lines below to test the swap function:

    
    // Put after the "Tom, Dick, Harry" for loop....
    
    swap(names, 0, 1);
    alert(names);   // Expected results: "Dick,Tom,Harry" in an alert box.
    
    var numbers = [0, -3, 22, 8];
    alert(numbers);   // Expected results: "0,-3,22,8"
    
    swap(numbers, 0, 3);
    alert(numbers);   // Expected results: ???
    
    swap(numbers, 0, 4);
    alert(numbers);   // Expected results: ???
    
    

    Test your function with several different inputs to make sure that it is working as expected and that you understand how it behaves with incorrect parameters.

    ⇒ Once you are comfortable that the swap function is working and that you understand what the final call (swap the contents of index 0 and index 4) is doing, comment it out so that it doesn't complicate your next steps.
  3. Find Minimum Index: Copy and complete the findMinIndex function below. (This should go in the HEAD section of your page.) This function should return the index of the smallest element in an array (not the actual value).

    
    //----------------
    // findMinIndex - Returns the index of the smallest item in the array
    function findMinIndex(theArray)
    {
    
       // minValue stores the smallest value seen so far.
       var minValue = theArray[0];
    
       // minIndex stores the INDEX of the smallest value seen so far. 
       var minIndex = 0;
    
       // FUNCTION NOT FINISHED! 
       // THERE SHOULD BE A FOR LOOP HERE TO SEE IF THERE IS A SMALLER VALUE IN THE ARRAY.
       // WHEN A SMALLER VALUE IS DETECTED, CHANGE minValue AND minIndex.
    
       return minIndex;
    
    }
    
    

    Test findMinIndex: Test your function by finding the index of the minimum value in the names and numbers arrays defined above, and reporting the index using the alert function. In the case of the names array, since the minimum value is the one that comes first alphabetically ("Dick"), the minimum index is 1.

    
    alert(names);   // Remind me what current order is
    var minIndex = findMinIndex(names);
    alert(minIndex);   // Expected results: ???
    
    alert(numbers);   // Remind me what current order is
    minIndex = findMinIndex(numbers);
    alert(minIndex);   // Expected results: ???
    
    
  4. Find Minimum in a Subrange: Next, you will modify the findMinIndex function to take a second parameter, a starting index, and to find the minimum in the part of the array starting at that index. Start by modifying the comment with the function description and adding the new parameter.

    
    //----------------
    // findMinIndex - Returns the index of the smallest item in the array 
    // starting at position startIndex 
    function findMinIndex(theArray, startIndex)
    {
    
       ...
    
    }
    
    

    Understand the desired behavior: If the starting index (startIndex) is 0, then findMinIndex will return the same value as before. If the starting index is larger, say 2, then the function will now return the index of the smallest element between position 2 and the end of the array. For example, after the code below is executed, the variable tmp1 will contain 1, since the smallest item in the array, "a", is located at position 1. The variable tmp2, however, will contain 3, since the smallest value in the array between position 2 and the end is "c" at position 3. Make sure you fully understand what the new version of this function is supposed to do before you attempt to modify it.

    
    var letters = ["g", "a", "t", "c"];
    alert(letters);   // Expected results: "g,a,t,c"
    
                                         //            ["g", "a", "t", "c"];
                                         //              ↑         ↑
    var tmp1  = findMinIndex(letters, 0); // Starts here--'         |
    var tmp2  = findMinIndex(letters, 2); // Starts here------------'
    
    alert(tmp1);   // Expected results: "1" ("a" was minimum)
    alert(tmp2);   // Expected results: "3" ("c" was minimum)
    
    

    Implement changes: Once you understand what the new version of findMinIndex should do, modify the function to find the minimum in the part of the array starting at that index.

    Test: Test your function with several different inputs to make sure that it is working as expected. Make sure to save your work so that you can use and extend it in the next lab.