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. 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. 
    }
    

    Create a <script> section in the BODY of your web page, and test your function with code like the following:

    
    var names = ["Tom", "Dick", "Harry"]
    alert(names);   // Expected results: "Tom,Dick,Harry" in an alert box.
    
    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.

  2. Find Minimum: Copy and complete the findMinimum 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).

    
    //----------------
    // findMinimum - Returns the index of the smallest item in the array
    function findMinimum(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 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, the minimum value will be the one that comes first alphabetically ("Dick"), whose index is 1.

    
    var minIndex = findMinimum(names);
    alert(minIndex);   // Expected results: ???
    
    minIndex = findMinimum(numbers);
    alert(minIndex);   // Expected results: ???
    
    
  3. Find Minimum in a Subrange: Modify the findMinimum function to take a second parameter, a starting index, and to find the minimum in the part of the array starting at that index.

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

    If the starting index (startIndex) is 0, then findMinimum 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  = findMinimum(letters, 0); // Starts here--'         |
    var tmp2  = findMinimum(letters, 2); // Starts here------------'
    
    alert(tmp1);   // Expected results: "1" ("a" was minimum)
    alert(tmp2);   // Expected results: "3" ("c" was minimum)
    
    

    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.