COP 2500 Spring 2012

Lab #5

Home Labs Lecture Notes
 

Deliverables: To complete this assignment you must --
1.) Write the HTML and JavaScript page described below.
2.) Email your results to your lab instructor.

Motivation: How do we use lots of variables?

Introduction: We've seen how to sum over several variables:

var x = 5, y = 10, z = 15;
var sum = 0;
sum = sum + x;
sum = sum + y;
sum = sum + z;
Let's extend this to an arbitrary number, say 20, of variables with arrays. This would be tedious to input by hand with the method seen before.
var size = 20;
// Declare the array of 20 numbers
var numbers[size];
An array contains lots of variables, in this case, 20.
51015 ...
You can access each variable (element) in the array with a special syntax:
numbers[0] = 5;
This gets the 0th variable in the array (the first one) and assigns it to 5, just like we set x = 5 earlier. Setting the rest:
numbers[1] = 10;
numbers[2] = 15;
What about the rest of the 20 variables? Let's assign them to random integers.
As a review, a random decimal can be created with Math.random(), then scaled to some range, say 100 by multiplying: Math.random() * 100, then rounded to an integer with Math.round(): Math.round(Math.random() * 100).
numbers[3] = Math.round(Math.random() * 100);
numbers[4] = Math.round(Math.random() * 100);
...
numbers[19] = Math.round(Math.random() * 100);
It's still tedious to initialize 20 numbers, but it's easier to keep track of with our numbers array. Here's a very nice way of repeating code, the for loop, which has the general syntax:
for(initialize; condition; increment){
	statement
}
Which gets performed in the following order:
1: initialize
2: condition: if true, perform statement, otherwise skip statement and increment and continue executing like normal.
3: statement
4: increment, repeat from 2
var i, numbers[20];
for(i = 0; i < 20; i++)
	numbers[i] = Math.round(Math.random() * 100);
Let's trace how this code is executed:
1. i and numbers declared in memory.
2. i = 0
3. Check if i < 20. (0 < 20 yes), do statement.
4. numbers[i (which is 0)] is set to a random integer from 0-100.
5. Syntax: i++ is the same as i += 1 is the same as i = i + 1, so i = 0 + 1 = 1.
6. Check if i < 20. (1 < 20, yes), do statement.
7. numbers[1] = random integer from 0 to 100.
...

After examining the trace, we know this will create an array of 20 random 0-100 ranged integers.
Review loops and arrays lecture notes for further explanation. Let's return to the original example with
Problem 1: summing over a list of numbers.
Algorithm:
1. Create an array called list of size 10.
2. Initialize all elements of the list to random integers between 0 and 100 using a for loop.
3. Sum each element in the list with a for loop. You will need a new variable, sum, to store your total.
4. Output the sum with document.write or alert.

SectionCode OutlineComments
INPUT:var list[10]; This represents a list
of ten numbers.
OTHER VARIABLES:var sum = 0;
var i;
INITIALIZATION:FOR i = 0 to 9
list[i] = random integer between 0 and 100;
This fills the array with ten
random numbers from 0 - 100.
COMPUTATION:FOR i = 0 to 9
sum = sum + list[i];
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.

Problem 2: Comparing numbers.
Algorithm:
1. Create two arrays called list1 and list2 of size 10.
2. Initialize all elements of the lists to random integers between 0 and 100 using a for loop.
3. Compare each element in list1 to its corresponding element in list2 with a for loop.
4. Output the comparision with document.write.

SectionCode OutlineComments
INPUT:var list1[10], list2[10]; This represents two lists
of ten numbers each.
OTHER VARIABLES:var i; We only need the loop
variable.
INITIALIZATION:FOR i = 0 to 9
list1[i] = random number between 0 - 100;
list2[i] = random number between 0 - 100;
i = 0 to 10 not inclusive of 10.
COMPUTATION: FOR i = 0 to 9
IF ( list1[i] > list2[i])
Output list1[i] + "is greater than" + list2[i] with document.write
ELSE
Output list1[i] + "is less than" + list2[i] with document.write

This simply compares two
corresponding numbers
from each list and prints out
which one is bigger.
OUTPUT: No output required since we
have printed out the results
in the loop body.
Open a browser and verify that the file displays properly. In order to obtain full credit for the lab you need to email your source code to your grader.