tailieunhanh - Lecture Fundamentals of computing 1: Lecture 6 - Duy Tan University
Lecture Fundamentals of computing 1: Lecture 6 introduce the arrays. In this chapter, the following content will be discussed: Arrays, arrays as parameter, reference semantics, arrays for tallying. Inviting you refer. | Lecture Title: Arrays Fundamentals of Computing 1 Agenda Arrays Arrays as parameter Reference semantics Arrays for tallying ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 4 days were above average. Why the problem is hard We need each input value twice: to compute the average (a cumulative sum) to count how many were above average We could read each value into a variable. but we: don't know how many days are needed until the program runs don't know how many variables to declare We need a way to declare many variables in one step. Arrays array: object that stores many values of the same type. element: One value in an array. index: A 0-based integer to access an element from an array. index 0 1 2 3 4 5 6 7 8 9 value 12 49 -2 26 5 17 -6 84 72 3 element 0 element 4 element 9 Array declaration type[] name = new type[length]; Example: int[] numbers = new int[10]; index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0 0 0 0 0 0 Array declaration, cont. The length can be any integer expression. int x = 2 * 3 + 1; int[] data = new int[x % 5 + 2]; Each element initially gets a "zero-equivalent" value. Type Default value int 0 double boolean false String or other object null (means, "no object") Accessing elements name[index] // access name[index] = value; // modify Example: numbers[0] = 27; numbers[3] = -6; (numbers[0]); if (numbers[3] ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 4 days were above average. Why the problem is hard We need each input value twice: to compute the average (a cumulative sum) to count how many were above average We could read each value into a variable. but we: don't know how many days are needed until the program runs don't know how many variables to declare We need a way to declare many variables in one step. Arrays array: object that stores many values of the same type. element: One value in an array. index: A 0-based integer to .
đang nạp các trang xem trước