CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored...

21
CIS 130 Numeric Arrays Chapter 10

Transcript of CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored...

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 common name

• All the pieces of data are the same type

• Each piece of data is called an ‘array element’

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.

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.

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

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:

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;

Accessing array elements

• The first element an array is always subscript 0– the nth 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;

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 0th subscript

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]

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};

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

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

Data Types and Sizes

Type Size in bytes

-----------------------------------

int 2 - 4 (4 in our system)

short 2

long 4

float 4

double 8

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));

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

How do multi - dimensional arrays work?

• int x[4][3] causes the compiler to set aside memory:

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

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

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?

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?