mon 19 oct 2015
play

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


  1. Mon., 19 Oct. 2015 Questions about last week’s lab? Don’t 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)

  2. Three More Haskell Examples unlucky.hs, hearts.hs, google.hs (oct19 folder) The “unlucky” function uses helper functions named “ split ”, “ rejoin ”, and “ subst ”: split 32345 == [3,2,3,4,5] subst 3 7 [3,2,3,4,5] == [7,2,7,4,5] rejoin [7,2,7,4,5] = 72745l

  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”

  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!”

  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 x 0 , x 1 , x 2 , … or a 0,0 , a 0,1 , …, a 5,4 , a 5,5 ).

  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++.

  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)

  8. Arrays a[0] 99 Usually stored in consecutive memory locations. a[1] Example: -12 one int int a[10]; = 4 a[0] = 99; bytes, a[2] so 40 a[1] = -12; bytes 42 a[2] = 42; ...

  9. Arrays 10000 a[0] 99 To find an int array element, take its base location plus 10004 a[1] its index times 4: -12 a[2] is in location 10000 + 2*(sizeof int) = a[2] 10008 10000 + 2*4 = 10008 42

  10. Arrays c[0][0] 10000 c[0][1] c[0][2] What about two-dimensional c[0][3] arrays? c[1][0] one char c[3][4]; c[1][1] char = 1 c[1][2] We have a choice: bytes, c[1][3] ROW MAJOR so 12 c[2][0] bytes ORDER? c[2][1] … 2 more …

  11. Arrays c[0][0] 10000 c[0][1] c[0][2] char c[3][4]; c[0][3] c[1][0] ROW MAJOR c[1][1] ORDER: c[1][2] 10006 c[1][2] = base address c[1][3] c[2][0] + (1*(# cols) + 2)*sizeof char c[2][1] =10000+(1*4+2)*1=10006 … 2 more …

  12. Arrays c[0][0] 10000 c[1][0] c[2][0] c[0][1] char c[3][4]; c[1][1] … or c[2][1] c[0][2] COLUMN MAJOR c[1][2] ORDER? c[2][2] c[0][3] … 2 more …

  13. Arrays c[0][0] 10000 c[1][0] c[2][0] char c[3][4]; c[0][1] c[1][1] COLUMN MAJOR c[2][1] ORDER: c[0][2] c[1][2] = base address c[1][2] 10007 c[2][2] + (2*(#rows)+1)*sizeof char c[0][1] = 10000+(6+1)*1 = 10007 … 2 more …

  14. Arrays Java, C, and many other languages use row major order. (See program array2.cpp in oct19 repo.) Fortran uses column major order. (See program array.for in oct19 repo.) Does it matter?

  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.

  16. Efficiency Concerns Experiments show that in C (which uses row major order), the loop on the left is faster than the loop on the right: for (c=0;c<1000;c++) for (r=0;r<1000;r++) for (r=0;r<1000;r++) for (c=0;c<1000;c++) x[r][c] = ...; x[r][c] = ...; FASTER SLOWER

  17. Efficiency Concerns However, in Fortran, the opposite holds: do, r = 1,1000 do, c = 1,1000 do, c = 1,1000 do, r = 1,1000 a(r,c) = ... a(r,c) = ... enddo enddo enddo enddo SLOWER FASTER

  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], …

  19. Exercise int a[3][4][2][5]; ? Come up with a formula for the address of a[i][j][k] [l] , assuming zero-based indexing, assuming that each array element is 4 bytes and that row-major ordering is used, and, of course, assuming that i,j,k, and l are within the array bounds. Same, but for column-major ordering.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend