Docsity

Introduction to Computing-Computer Fundamentals-Lecture Slides, Slides of Computer Fundamentals

Partial preview of the text

Download Introduction to Computing-Computer Fundamentals-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

Introduction to Computing

Arrays

  • Comments
  • What are arrays?
  • How to declare and initialize arrays?
  • Accessing arrays / Using arrays
  • One dimensional and Two dimensional arrays
  • Arrays and constants

Arrays

  • Array
    • Group of consecutive memory locations
    • Same name and type
  • To refer to an element, specify
    • Array name
    • Position number
  • Format: arrayname [ position number ]
    • First element at position 0
    • n element array named c :
      • c[ 0 ] , c[ 1 ] ... c[ n โ€“ 1 ]

Name of array (Note that all elements of this array have the same name, c )

Position number of the element within array c

c[6]

- 6 **0 72 1543

0 62

1 6453 78**

c[0] c[1] c[2] c[3]

c[11]

c[10]

c[9]

c[8]

c[7]

c[5]

c[4]

5

6.3 Declaring Arrays

  • When declaring arrays, specify
    • Name
    • Type of array
    • Number of elements arrayType arrayName[ numberOfElements ];
    • Examples: int c[ 10 ]; float myArray[ 3284 ];
  • Declaring multiple arrays of same type
    • Format similar to regular variables
    • Example: int b[ 100 ], x[ 27 ];

7

Using Arrays

  • Initializers

int n[ 5 ] = { 1, 2, 3, 4, 5 };

  • If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 }
  • All elements 0
  • If too many a syntax error is produced syntax error
  • C arrays have no bounds checking
  • If size omitted, initializers determine it

int n[ ] = { 1, 2, 3, 4, 5 };

  • 5 initializers, therefore 5 element array

Accessing Array Components

  • The general form (syntax) of accessing an array component is:

arrayName[indexExp]

where indexExp, called index , is any expression whose value

is a nonnegative integer

  • Index value specifies the position of the component in the array
  • The [] operator is called the array subscripting operator
  • The array index always starts at 0

Array Initialization

  • As with simple variables
    • Arrays can be initialized while they are being declared
  • When initializing arrays while declaring them
    • Not necessary to specify the size of the array
  • Size of array is determined by the number of initial values in the

braces

  • For example:

double sales[] = {12.25, 32.50, 16.90, 23, 45.68};

Partial Initialization

  • The statement

int list[10] = {0};

declares list to be an array of 10 components and initializes all components to zero

  • The statement

int list[10] = {8, 5, 12};

declares list to be an array of 10 components, initializes list[0] to 8 , list[1] to 5 , list[2] to 12 and all other components are initialized to 0

Restrictions on Array Processing

Assignment does not work with arrays

In order to copy one array into another array we must copy component-wise

Restrictions on Array Processing

(continued)