CPS 1 - Spring, 1999 - Ramm 2/12/99 #13
- Announce
- Continue to Read Chapter 3
- Exam Next Friday
- No Quiz Next Wed
- Review Session
- Sample Exam
Chapt 3. Numerical Computation and the Study of Functions
- Use Arrays in a Loop
- int k = 0; sum = 0;
- while ( k < 10 )
- {
- sum = sum + name[k];
- k = k + 1;
- }
- Array Creation and Use
- int num[] = new int[6];
- +--+--+--+--+--+--+
- | ?| ?| ?| ?| ?| ?|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
- num[1] = 21; num[5] = 13;
- +--+--+--+--+--+--+
- | ?|21| ?| ?| ?|13|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
-
- int k = 2;
- while(k<6){num[k] = k*k; k = k+1;}
- +--+--+--+--+--+--+
- | ?|21| 4| 9|16|25|
- +--+--+--+--+--+--+
- --0--1--2--3--4--5-
Setting it Up
- Declaration:
-
double weights[];
- Definition:
-
weights = new double[50];
- Or Combined:
-
double weights[] = new double[50];
Subscript Range!!!
- Dangers of scribbling over memory
- Java checks; some languages don't
- Tradeoffs
Simple Examples: Develop Algorithms in Class
Maximum Example (code fragment)
-
tallest = 0;
-
k = 0;
-
while (k < attending)
-
{
-
if (height[k] > tallest)
-
{
-
tallest = height[k];
-
}
-
k = k + 1;
-
}
Applet Array Example
Hotel.java,
Hotel.html
Expanding the Hotel Example