Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSIS 110 - Lecture 17: Arrays and Loops - Prof. Brian F. Hanks, Study notes of Javascript programming

The results of quiz 17 in csis 110, focusing on arrays and loops. Topics include: array basics, for loop usage, and deciding which loop to use. Examples and exercises are provided.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-8wy-1
koofers-user-8wy-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 17
Quiz Results:
Mean: 20.3
Median: 21.25
Max: 24.5
Distribution: <17.5: 3 17.5-19.5: 1 20-22: 6 22.5-24.5: 6 25: 0
Last Time:
Arrays
- what is an array?
- How do we declare an array variable?
- How do we allocate the array elements?
- How do we access an individual element?
- What is the initial value of an array element?
For Loop
We've seen two types of loops: foreach and while. There is a third variety of loop: the for
loop. This loop is also commonly used with arrays.
for ( ForInit; ForExpression; ForUpdate )
Loop Body
[Draw flowchart]
Here is an example:
for ( int i = 5; i < 30; i = i + 7 ) {
System.out.print( i + " " );
}
And here is an example that uses an array:
int[] quizScores = new int[ numStudents ];
int sum = 0;
for ( int index = 0; index < quizScores.length; index++ )
{
sum = sum + quizScores[ index ];
}
Why would we use this loop instead of a foreach or while? Use it when we
- Want to execute loop a fixed number of times. (like foreach)
pf3
pf4

Partial preview of the text

Download CSIS 110 - Lecture 17: Arrays and Loops - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 17

Quiz Results: Mean: 20. Median: 21. Max: 24. Distribution: <17.5: 3 17.5-19.5: 1 20-22: 6 22.5-24.5: 6 25: 0 Last Time: Arrays

  • what is an array?
  • How do we declare an array variable?
  • How do we allocate the array elements?
  • How do we access an individual element?
  • What is the initial value of an array element? For Loop We've seen two types of loops: foreach and while. There is a third variety of loop: the for loop. This loop is also commonly used with arrays. for ( ForInit; ForExpression; ForUpdate ) Loop Body [Draw flowchart] Here is an example: for ( int i = 5; i < 30; i = i + 7 ) { System.out.print( i + " " ); } And here is an example that uses an array: int[] quizScores = new int[ numStudents ]; int sum = 0; for ( int index = 0; index < quizScores.length; index++ ) { sum = sum + quizScores[ index ]; } Why would we use this loop instead of a foreach or while? Use it when we
  • Want to execute loop a fixed number of times. (like foreach)
  • Need an index or other counter inside the loop. (like while) Discuss development of foreach, and how it has reduced the use of the for loop. Look at methods in project array-loop:
  • loop1: basic for loop [Redraw flowchart using this loop]
  • loop2: basic for loop, but value multiplied by 2 each time
  • loop3: index changes by 2 each time – note use of assignment instead of ++
  • loop4: count down [Use debugger on one of these loops] How would we calculate the average of the values contained in an array? [discuss integer division. Introduce the type double] Deciding which loop to use What are the ‘rules’ for deciding which type of loop to use?
  • Foreach loop: when you want to iterate through every item in a collection, and: o You don't need a index/counter o You don't need to modify the collection
  • For loop: when you know ahead of time how many iterations there are. o Usable without collection o Gives you a counter o Can modify collection inside loop
  • While loop: when you just keep going until some condition is true o Don't know ahead of time how many iterations are required o Usable with and without collections Examples: Given a sorted array of ints: int[] myArray; Use a for, foreach, or while?
  • to display all the values in the array
  • to find the first element whose value is larger than 15?
  • to display the last 5 elements of the array?
  • to calculate the sum of the elements?
  • to search for an element with the value 27?

Lab Do the following Exercises from the textbook: 4.52, 4.53, 4.54, and 4.57, but read the following notes first.

  1. Do exercises 4.52 and 4.53 at the same time – they are really one exercise broken into two pieces.
  2. Exercise 4.53 gives you a hint. This hint tells you that the LogFileReader class has two constructors: one that has a formal parameter and one that does not. The one that takes a formal parameter expects a filename to use as the log file. You should modify your LogFileAnalyzer constructor to create a LogFileReader using this constructor, and pass it a log file of your choice (such as "myweblog.txt"). Then you can create a log file that contains only a few entries, which will make it easier to see if your methods work correctly.
  3. For Exercise 4.57, name the method busiestTwoHours. Extra Credit: Do Exercise 4.55. Email your LogAnalyzer.java file to me, using the subject "Lab 13". Lab Do the following Exercises from the textbook: 4.52, 4.53, 4.54, and 4.57, but read the following notes first.
  4. Do exercises 4.52 and 4.53 at the same time – they are really one exercise broken into two pieces.
  5. Exercise 4.53 gives you a hint. This hint tells you that the LogFileReader class has two constructors: one that has a formal parameter and one that does not. The one that takes a formal parameter expects a filename to use as the log file. You should modify your LogFileAnalyzer constructor to create a LogFileReader using this constructor, and pass it a log file of your choice (such as "myweblog.txt"). Then you can create a log file that contains only a few entries, which will make it easier to see if your methods work correctly.
  6. For Exercise 4.57, name the method busiestTwoHours. Extra Credit: Do Exercise 4.55. Email your LogAnalyzer.java file to me, using the subject "Lab 13".