SLIDE 1
Mon., 19 Oct. 2015 Questions about last weeks lab? Dont forget: - - PowerPoint PPT Presentation
Mon., 19 Oct. 2015 Questions about last weeks lab? Dont forget: - - PowerPoint PPT Presentation
Mon., 19 Oct. 2015 Questions about last weeks lab? Dont forget: Department coffee/tea tomorrow afternoon, 2 p.m. A few more Haskell examples Arrays Read chapter 7; try to get at least partway through 7.7 (pointers) Three More Haskell
SLIDE 2
SLIDE 3
More Haskell Examples
hearts.hs recursively checks each digit in an integer and, if it is less than 3, a “heart” (<3) is added on to the result string.
hearts n = if n < 3 then "<3" else if n < 10 then "" else hearts (n `quot` 10) ++ (if (n `mod` 10) < 3 then "<3" else "") hearts 10352 = “<3<3<3”
SLIDE 4
More Haskell Examples
google.hs takes a string and recursively replaces occurrences of “oo” with “oogleoo”:
google s = if (length s) < 2 then s else if take 2 s `elem` ["oo","OO","Oo","oO"] then "oogleoo" ++ google (drop 2 s) else (take 1 s) ++ google (drop 1 s) google “too cool!” == “toogleoo coogleool!”
SLIDE 5
Arrays
One of the first composite data types and still used very often. Fixed-size collection of values, all of the same type, accessed by one or more indices (sometimes called “subscripts” in analogy to mathematical notation such as x0, x1, x2, … or a0,0, a0,1, …, a5,4, a5,5).
SLIDE 6
Arrays
See program “array.cpp” for two different ways to represent arrays in C++: the “traditional” C array and the “array class” in C++.
SLIDE 7
Arrays
Index ranges: often zero-based for ease of calculation (see later slide). Thus, elements of int a[4] are a[0], a[1], a[2], and a[3]. Older languages (e.g., older versions of Fortran): 1-based (a[1],...,a[4]). Some languages allow other ranges (see Fortran 95 program array2.for in repo)
SLIDE 8
Arrays
Usually stored in consecutive memory locations. Example:
int a[10]; a[0] = 99; a[1] = -12; a[2] = 42; ...
a[0] a[1] a[2]
- ne int
= 4 bytes, so 40 bytes
99
- 12
42
SLIDE 9
Arrays
To find an int array element, take its base location plus its index times 4: a[2] is in location 10000 + 2*(sizeof int) = 10000 + 2*4 = 10008
a[0] a[1] a[2]
99
- 12
42
10000 10004 10008
SLIDE 10
Arrays
What about two-dimensional arrays? char c[3][4]; We have a choice: ROW MAJOR ORDER?
10000 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1]
- ne
char = 1 bytes, so 12 bytes
… 2 more …
SLIDE 11
Arrays
char c[3][4]; ROW MAJOR ORDER: c[1][2] = base address + (1*(# cols) + 2)*sizeof char =10000+(1*4+2)*1=10006
10000 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] … 2 more … 10006
SLIDE 12
Arrays
char c[3][4]; … or COLUMN MAJOR ORDER?
10000 c[0][0] c[1][0] c[2][0] c[0][1] c[1][1] c[2][1] c[0][2] c[1][2] c[2][2] c[0][3] … 2 more …
SLIDE 13
Arrays
char c[3][4]; COLUMN MAJOR ORDER: c[1][2] = base address + (2*(#rows)+1)*sizeof char = 10000+(6+1)*1 = 10007
10000 c[0][0] c[1][0] c[2][0] c[0][1] c[1][1] c[2][1] c[0][2] c[1][2] c[2][2] c[0][1] … 2 more … 10007
SLIDE 14
Arrays
Java, C, and many other languages use row major order. (See program array2.cpp in
- ct19 repo.)
Fortran uses column major order. (See program array.for in oct19 repo.) Does it matter?
SLIDE 15
Efficiency Concerns
In a language that uses row-major order, it is more efficient to access elements by rows because they are in consecutive locations; accessing by columns involves a lot of jumping around in memory. See programs byrows.c and bycols.c; byrows.for and bycols. for in the class repository.
SLIDE 16
Efficiency Concerns
for (c=0;c<1000;c++) for (r=0;r<1000;r++) x[r][c] = ...; FASTER SLOWER
Experiments show that in C (which uses row major order), the loop on the left is faster than the loop on the right:
for (r=0;r<1000;r++) for (c=0;c<1000;c++) x[r][c] = ...;
SLIDE 17
Efficiency Concerns
However, in Fortran, the opposite holds:
do, r = 1,1000 do, c = 1,1000 a(r,c) = ... enddo enddo SLOWER FASTER
do, c = 1,1000 do, r = 1,1000 a(r,c) = ... enddo enddo
SLIDE 18
Multiple Dimensions
What about int a[3][4][2][5]; ? Row-major: a[0][0][0][0], a[0][0][0][1], a[0][0][0][2], … Column-major: a[0][0][0][0], a[1][0][0][0], a[2][0][0][0], …
SLIDE 19