+ All Categories
Home > Documents > CSC 142 Computer Science II

CSC 142 Computer Science II

Date post: 31-Dec-2015
Category:
Upload: arthur-norris
View: 12 times
Download: 2 times
Share this document with a friend
Description:
CSC 142 Computer Science II. Zhen Jiang West Chester University [email protected]. Outline. Problem Arrays Arrays and Loops A Multi-counter Problem Multi-dimensional Arrays Exercises Summary of Learning Materials. How would you solve this?. Consider the following program: - PowerPoint PPT Presentation
36
CSC 142 Computer Science II Zhen Jiang West Chester University [email protected]
Transcript
Page 1: CSC 142 Computer Science II

CSC 142 Computer Science II

Zhen Jiang West Chester University

[email protected]

Page 2: CSC 142 Computer Science II

Outline Problem Arrays Arrays and Loops A Multi-counter Problem Multi-dimensional Arrays Exercises Summary of Learning Materials

Page 3: CSC 142 Computer Science II

3

How would you solve this?

Consider the following program:

How many days' temperatures? 7Day 1's high temp: 45Day 2's high temp: 44Day 3's high temp: 39Day 4's high temp: 48Day 5's high temp: 37Day 6's high temp: 46Day 7's high temp: 53Average temp = 44.571428571428574 days were above average.04/19/23

Page 4: CSC 142 Computer Science II

4

We need each input value twice … to compute the average via a cumulative sum … to count how many were above the average

What about putting the values into variables? How many variables would we declare?

Need a way to declare many variables at once (and also to control that number of variables declared).

04/19/23

Page 5: CSC 142 Computer Science II

5

Arrays array: An object that stores many values of

the same type. Figure 7-2, page 408 element: a value in an array, page 409 subscript: an integer indicating the position of a

value in an array, Figure 7-4, page 410

37284-617526-24912value

9876543210subscript

element 0 element 4 element 9

04/19/23

Page 6: CSC 142 Computer Science II

6

Declaring/initializing an array, page 408<type>[] <name> = new <type>[<length>];

Example:int[] numbers = new int[10];

The length can be any integer expression:int x = 2 * 3 + 1;int[] data = new int[x % 5 + 2];

0000000000value

9876543210subscript

04/19/23

Page 7: CSC 142 Computer Science II

7

When arrays are initially constructed, every element is automatically initialized to a "zero-equivalent" value.

int: 0 double: 0.0 boolean: false object type: null (null means "no object")

04/19/23

Page 8: CSC 142 Computer Science II

8

An array of doubles

An array of booleans

0.0

0.0

0.0

0.0

0.0

value

43210subscript

false

false

false

false

value

3210subscript

04/19/23

Page 9: CSC 142 Computer Science II

9

Assigning a value to an array element:<array name>[<subscript>] = <value>;

Example:numbers[0] = 27;

numbers[3] = -6;

Code 7-1, 7-2, page 410-412

000000-60027

value

9876543210subscript

04/19/23

Page 10: CSC 142 Computer Science II

10

Using an array element's value in an expression:<array name>[<index>]

Example:System.out.println(numbers[0]);

if (numbers[3] < 0) {

System.out.println("Element 3 is negative.");

}

Code 7-1, 7-2, page 410-412

000000-60027

value

9876543210subscript

04/19/23

Page 11: CSC 142 Computer Science II

11

Reading or writing any index outside the valid range will throw an ArrayIndexOutOfBoundsException.

Example:int[] data = new int[10];System.out.println(data[0]); // okaySystem.out.println(data[-1]); // exception!System.out.println(data[9]); // okaySystem.out.println(data[10]); // exception!

0000000000value

9876543210subscript

04/19/23

Page 12: CSC 142 Computer Science II

Code 7-3, page 413

Page 13: CSC 142 Computer Science II

13

int[] numbers = new int[8];

numbers[1] = 4;

numbers[4] = 99;

numbers[7] = 2;

int x = numbers[1];

numbers[x] = 44;

numbers[numbers[7]] = 11; // use numbers[7] as subscript!

6 7543210

numbers:

0

6

0000000

7543210

0

6

2099

0040

7543210

0

6

2044

011

40

7543210

x: 4x:

04/19/23

Page 14: CSC 142 Computer Science II

14

Arrays and Loops

Arrays are very commonly used with for loops to access each element

This is a space problem: 1st element, 2nd element, … etc, compared with the time sequence problem we mentioned earlier (1st iteration, 2nd iteration, 3rd iteration, …)

Solution: 1st iteration handles 1st element, 2nd iteration handles 2nd element, …., etc

04/19/23

Page 15: CSC 142 Computer Science II

15

Example:

for (int i = 0; i < 8; i++) {

System.out.print(numbers[i] + " ");

}

System.out.println(); // end the line of output

Output:0 4 11 0 44 0 0 2

6 7543210

numbers:

0

6

0000000

7543210

0

6

2099

0040

7543210

0

6

2044

011

40

7543210

04/19/23

Page 16: CSC 142 Computer Science II

16

Make an array:

for (int i = 0; i < 8; i++) {

numbers[i] = 2 * i;

}

14121086420value

76543210subscript

04/19/23

Page 17: CSC 142 Computer Science II

17

Make an array:

for (int i = 0; i < 8; i++) {

numbers[i] = i * i;

}

493625169410value

76543210subscript

04/19/23

Page 18: CSC 142 Computer Science II

18

An array's length field stores its number of elements, page 419.

General syntax:<array name>.length

NB: Because it's a field (i.e. not a method), it does not use parentheses like a String's .length()!

04/19/23

Page 19: CSC 142 Computer Science II

19

for (int i = 0; i < numbers.length; i++) {

System.out.print(numbers[i] + " ");

}

Output:0 1 4 9 16 25 36 49

What expression refers to the last element of an array? The middle element?

493625169410value

76543210subscript

04/19/23

Page 20: CSC 142 Computer Science II

20

Solve the following problem:

How many days' temperatures? 7Day 1's high temp: 45Day 2's high temp: 44Day 3's high temp: 39Day 4's high temp: 48Day 5's high temp: 37Day 6's high temp: 46Day 7's high temp: 53Average temp = 44.571428571428574 days were above average.

04/19/23

Page 21: CSC 142 Computer Science II

21

import java.util.*;

public class Weather { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextInt(); int[] temperatures = new int[days]; // to store temperatures int sum = 0;

for (int i = 0; i < days; i++) { // process each day System.out.print("Day " + (i + 1) + "'s high temp: "); temperatures[i] = console.nextInt(); sum += temperatures[i]; } double average = (double) sum / days;

int count = 0; // see if above average for (int i = 0; i < days; i++) { if (temperatures[i] > average) { count++; } } // report results System.out.println("Average temp = " + average); System.out.println(count + " days above average"); }}

04/19/23

Page 22: CSC 142 Computer Science II

22

A multi-counter problem

Problem: Examine a number and count the number of occurrences of every digit.

Example: The number 229231007 contains: two 0s, one 1, three 2s, one 7, and one 9

Solution? Declare 10 counter variables—one per digit.

Eeewww!!!!

int counter0, counter1, counter2, counter3;

int counter4, counter5, counter6, counter7;

int counter8, counter9;

04/19/23

Page 23: CSC 142 Computer Science II

23

Problem: Examine a number and count the number of occurrences of every digit.

Example: The number 229231007 contains: two 0s, one 1, three 2s, one 7, and one 9

Solution: Declare an array of 10 elements—the element at

index i will store the counter for digit value i.

int[] counts = new int[10];

04/19/23

Page 24: CSC 142 Computer Science II

24

int num = 229231007;

//ensure num > 0

int[] counts = new int[10];

while (num > 0) {

int digit = num % 10;

counts[digit]++;

num = num / 10;

}

1010000312value

9876543210subscript

04/19/23

Page 25: CSC 142 Computer Science II

Write a program that will print a histogram of stars indicating the number of occurrences of every digit.

0: **1: *2: ***7: *9: *

for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); }}

04/19/23 25

Page 26: CSC 142 Computer Science II

26

Quick array initialization, p415, general syntax:<type>[] <name> = {<value>, <value>, ...,

<value>};

Example:int[] numbers = { 12, 49, -2, 26, 5, 17, -6 };

Useful when you know in advance what the array's element values will be.

-617526-24912value

6543210

04/19/23

Page 27: CSC 142 Computer Science II

27

int[] a = { 2, 5, 1, 6, 14, 7, 9 };

for (int i = 1; i < a.length; i++) {

a[i] += a[i - 1];

}

What’s in the array?

97146152value

6543210index 44352814872value

6543210index

04/19/23

Page 28: CSC 142 Computer Science II

28

Multi-dimensional ArraysLet’s say there’s a class of 8 students,

with 10 quiz grades each:

How can we declare variables to store all this data?

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

4.5 2 9 9 5.5 4 7.5 6 5 9

04/19/23

Page 29: CSC 142 Computer Science II

29

double [] student1 = new double[10];

...

double [] student8 = new double[10];

student1:

...

student8:

Solution 1: An array per student. Tedious!

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

4.5 2 9 9 5.5 4 7.5 6 5 9

04/19/23

Page 30: CSC 142 Computer Science II

30

double [] quiz1 = new double[8];

...

double [] quiz10 = new double[8];

quiz1:

...

quiz10:

Solution 2: An array per quiz. Same problem!

7.5 … … … … … … 4.5

6 … … … … … … 9

04/19/23

Page 31: CSC 142 Computer Science II

31

double [][] quizScores = new double[8][10];

quizScores:

7.5 4.5 9.5 2 7 8.5 1 8.5 7.5 6

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

… … … … … … … … … …

4.5 2 9 9 5.5 4 7.5 6 5 9

quizScores[0][6]

quizScores[7][9]

04/19/23

Page 32: CSC 142 Computer Science II

A 2-D array is really an array of arrays! Figure 7-18, p454

double [][] quizScores = new double[4][3];quizScores

quizScores[0]

quizScores[1]

quizScores[2]

quizScores[3]

04/19/23 32

Page 33: CSC 142 Computer Science II

double [][] quizScores = new double[4][];

quizScores[0] = new double[3];

quizScores[2] = new double[5];quizScores

quizScores[0]

quizScores[1]

quizScores[2]

quizScores[3]

04/19/23 33

Page 34: CSC 142 Computer Science II

Exercises Compute the sum of the elements

on the diagonal of a matrix.

Swap each element in a square 2-D array with elements on the opposite side of the main diagonal.

04/19/23 34

Page 35: CSC 142 Computer Science II

Exercises, slides 14-16, 18, 21, 23-24, 26-27, 34

Summary

04/19/23 35

Page 36: CSC 142 Computer Science II

Arrays, element, subscript, p408-410 New, p408 Inputting and outputting contents, p410 Valid range, p413 Declaration and initialization, p408, p415 Length, p419 Multi-dimensional arrays, p454

04/19/23 36


Recommended