05-arrays-bigoh

05-arrays-bigoh - CS216: Program and Data Representation...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon
CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield Primitive Arrays and Order of Growth
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Topics • Primitive Arrays in C/C++ – C++ implementation • Orders of Growth – Why? – What? O, Ω , Θ , o, ω – How?
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Primitive Arrays in C++ Declaring (two ways): 1. Specify name, type, and size int someInts[3]; 2. Specify name, type, and initialization list int someInts[ ] = {4, 37, 18}; Creates 3 ints someInts[0], someInts[1], someInts[2]
Background image of page 4
5 Things to Note about Arrays in C++ 1. No index checking! Compiler and runtime system will not catch accesses out of array index bounds Out of bounds access will cause unpredictable behavior 2. When passed as an argument to a function Function does not know the size of the array Need to pass another argument containing the size 3. Cannot be copied with = Must be copied element by element
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 C++ Implementation of Arrays 1. Array names are pointers to beginning of array int someInts[3]; size is only used by compiler to set aside memory for the array someInts is set to &someInts[0] Also called the base address Local constant – Cannot be changed Note that the previous declaration is almost the same as int someInts* = new int[3];
Background image of page 6
7 int someInts[3]; Address &someInts[0] &someInts[1] &someInts[2] Assumes ints are 4 bytes someInts[2] someInts[1] someInts[0] 1000 1004 1008 someInts=1000 local constant
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Where is someInt[i] located in memory? Remember someInts is a pointer to address of someInts[0] ( &someInt[0] ) Array elements are laid out sequentially in contiguous memory addresses &someInts[i] = {addr of someInts} + (sizeof(int) * i)
Background image of page 8
9 Operations on Arrays • someInts=someOtherArray; // illegal – Array base addresses are considered constant (cannot be reassigned) • This is one example of when pointers and arrays are NOT the same • Is someInts==someOtherArray allowed? – Yes, but will only evaluate to true if someInts and someOtherArray point at the same memory location
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Function Calls and Arrays someFunc(int arrayOfInts[]){ … } int main() { int someInts[3]; someFunc(someInts); //no brackets } someInts is a memory address size of someInts[] unknown Is this pass by reference? Pass by value?
Background image of page 10
11 Multidimensional Arrays int m[2][3]; //2 rows, 3 columns int m[][] = {{1,2,3},{4,5,6}}; // initialize row 0: 1,2,3 // initialize row 1: 4,5,6
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 C++ Implementation • Single row layout 0 1 0 1 2 1000 m 1000 m[0][0] m[1][2] m[1][1] m[1][0] m[0][2] m[0][1] row major
Background image of page 12
13 Command-line parameters • You can define the main() method as: – int main() – int main (int argc, char **argv) • Or: int main (int argc, char* argv[]) • The second gives us two parameters: – The number of command line parameters • Note that the program executable is 1 – An array of C-style strings (char*) containing those parameters
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 End of lecture on Mon, Feb 9 • Also did the last 11 slides of 04-numbers
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Orders of Growth • Algorithm Analysis – Orders of growth classes • Running Time Calculations
Background image of page 16
17 Classifying functions by their Asymptotic Growth Rates • Asymptotic growth rate, asymptotic order, or order of functions
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/11/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Page1 / 58

05-arrays-bigoh - CS216: Program and Data Representation...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online