Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.

Similar presentations


Presentation on theme: "CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a."— Presentation transcript:

1 CIS 130 Numeric Arrays Chapter 10

2 What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a common name All the pieces of data are the same type Each piece of data is called an ‘array element’

3 Why use an array? Related information can be stored in a single place Scenario 1: For company payroll, the total payroll for the 12 months can be stored in 12 different variables. Better programming would be to store them in one variable (an array) so all the related data is grouped together.

4 Why use an array? Scenario 2: A user in a company selects part numbers from a list box with 1000 possible selections. All of their selections (up to 1000) can be stored in an array. It is unknown ahead of time how many will be selected, so this would be harder to store in individual variables.

5 How do arrays work? Assume we need a 12 element array to hold the total payroll (one element for each month). Declaring the array: float monthlyPayroll[12] Each of the 12 elements in monthlyPayroll() is a float, and can be treated like any float variable

6 How do arrays work? When array is declared, compiler sets aside enough memory to hold the entire array Elements are stored in adjacent memory locations int x[5] causes the compiler to set aside memory:

7 Accessing array elements Elements are accessed by the array name followed by brackets with an integer x[1] = 3; x[3] = x[1] * 3; a = 100; b = 2 x[b] = a;

8 Accessing array elements The first element an array is always subscript 0 –the n th element in an array is subscript (n-1) Store the value 25 in the first element of x: x[0] = 25; Store the value 23 in the 102 element of a 200 element array called q: q[101] = 23;

9 Accessing array elements Some programmers like the first element in the array to correspond to subscript #1 –Declare the array 1 element too big –Ignore the 0 th subscript

10 Declaring arrays Can use a constant, or numeric literal Acceptable:Compiler Error: --------------------------------------------------- int x[3]; int q = 3 int x[q]; #define q 3 int x[q]; const int q = 3; int x[q]

11 Initializing Arrays Assigning values at declaration time: –int x[5] = {100, 200, 300, 400, 500}; Letting the compiler determine array size: –int x[] = {100, 200, 300, 400, 500}; Too few initializers - Compiles fine –int x[5] = {100, 200, 300}; Too many initializers - Compiler error –int x[5] = {100, 200, 300, 400, 500, 600};

12 The size of an array Arrays are very powerful programming tools Can be inefficient at run time –memory usage –searching can be time consuming Size of array should be kept to minimum, or other data structures can be used –Some other structures are covered in 131 and 221

13 The size of an array Keep arrays as small as possible Keep track of the size of an array –size = type size * number of elements How much memory does a 100 element integer array use? –integers take 4 bytes in Code Warrior –100 * 4 = 400 bytes

14 Data Types and Sizes TypeSize in bytes ----------------------------------- int2 - 4 (4 in our system) short2 long 4 float4 double8

15 How can size be determined? Size of a type can be determined by sizeof –printf(“The size of an integer is %d”, sizeof(int)); –printf(“The size of the array x is %d”, sizeof(x));

16 Multi-dimensional Arrays Arrays with more than 1 subscript –2 - D arrays have 2 subscripts –3 - D arrays have 3 subscripts –... –n - D arrays have n subscripts Only available memory limits the number of dimensions available Complexity increases exponentially with each added dimension

17 How do multi - dimensional arrays work? int x[4][3] causes the compiler to set aside memory:

18 Multi-dimensional arrays 2 - D arrays can be thought of as squares 3 - D arrays can be thought of as cubes 4 - D arrays can be though of as a 4 dimensional object (hyper-cubes) –(insert Twilight Zone theme here) 5 - D … (and so on) In all cases, the data is simply stored in adjacent memory locations

19 Initializing multi-dimensional arrays All values can be directly initialized: –int x[4][3] = {1 2 3 4 5 6 7 8 9 10 11 12} Braces can be used to clarify each ‘row’: –int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; –The idea of an ‘array of arrays’ extends from this form of initialization

20 Using for loops to initialize arrays int x[1000]; for (i = 0; i < 1000; i++) x[i] = 1; What does x look like after the loop executes?

21 Using for loops to initialize arrays counter = 0; int x[4][3]; for (i = 0; i < 4; i++) for (j = 0; j < 3; j++) { x[i][j] = counter; counter += 1; } What does x look like after the loop executes?


Download ppt "CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a."

Similar presentations


Ads by Google