COP2500 Lab 5

Deliverables: To complete this assignment you must --
1.) Write the HTML and JavaScript page described below and demonstrate to the lab instructor that it operates correctly.
2.) Complete the lab during the time provided or email your results to your lab instructor.
3.) Demonstrate youir program(s) to your lab instructor.

Introduction: The goal of this assignment is to give you experience working with arrays, IF-THEN-ELSE blocks, and LOOPS. Remember, arrays allow one variable name to refer to multiple memory locations. IF-THEN-ELSE blocks let you choose what parts of your code execute based on input data. LOOPS allow you to repeat sections of code. These three things exist in some Fform in all programming languages you are likely to encounter.

Procedures:
PART 1:

  1. Use a text editor to complete your assignment.
  2. Using your text editor create a new document named "cop2500lab5.html". Do not close out the window until after the lab instructor has had time to verify your work.
  3. Convert the following two algorithms into JavaScript code within your HTML document. Your final web page should display the algorithm title, the input and the output. The specific format you use to display the results is up to you, but it should be clear what the input and output means (in other words, don't just list rows of numbers without words or labels describing them). The first algorithm calculates the sum total of a list of numbers. The second algorithm, compares pairs of numbers from two different lists.

    Summing a list of numbers.
    SectionPseudocodeComments
    INPUT:var list[10]; This represents a list
    of ten numbers.
    OTHER VARIABLES:var sum;
    var i;
    INITIALIZATION:FOR i = 0 to 9 DO
    list[i] = random integer between 0 and 100;
    END DO
    sum = 0;
    This fills the array with ten
    random numbers from 0 - 100.
    Notice how we can use pseudo-code
    to avoid headache of having
    to build a random number generator.
    Most languages have predefined ones,
    and I'll demonstrate how to use the Javascript
    one in the lab.
    COMPUTATION:FOR i = 0 to 9 DO
    sum = sum + list[i];
    ENDDO
    This loop simply adds each list
    element to the sum one by one.
    OUTPUT:Display sum using document.write() or
    something similar.
    Remember to format your results
    in a sensible way.

    Compare numbers from two separate lists.
    SectionPseudocodeComments
    INPUT:var list1[10], list2[10]; This represents two lists
    of ten numbers each.
    OTHER VARIABLES:var i; We only need the loop
    variable (also called a
    counting variable.
    INITIALIZATION:FOR i = 0 to 9 DO
    list1[i] = random number between 0 - 100;
    list2[i] = random number between 0 - 100;
    ENDDO
    i = 0 to 10 not inclusive of 10.
    COMPUTATION: FOR i = 0 to 9 DO
    IF ( list1[i] > list2[i]) THEN
    print(list1[i] + "is greater than" + list2[i]);
    ELSE
    print(list1[i] + "is less than" + list2[i]);
    ENDIF
    ENDDO
    This simply compares two
    corresponding numbers
    from each list and prints out
    which one is bigger.
    The print() statements mean
    any display function, such as
    document.write().
    OUTPUT: No output required since we
    have printed out the results
    in the loop body.



  4. Open a browser and verify that the file displays properly. Keep this window open until the instructor has had time to verify your work. In order to obtain full credit for the lab you need to email your source code to your grader, and you need to show him that it is working in the lab.