4
CS111 Lab arrays Instructor: Michael Gordon

Arrays

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Arrays

CS111 Lab arrays

Instructor: Michael Gordon

Page 2: Arrays

Why Use Arrays

Arrays are used for dealing with sets of

data elements that are of the same type.

You can have an array of ints, doubles,

chars, strings, etc.

Arrays mean not having to create

separate variables/names for each

element.

Arrays are contiguous spaces in memory.

Page 3: Arrays

How do we use Arrays

int grades[5];

Declares an array of size 5

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

Assigns values to each of the elements

grades[0] = 100;

Assigns value of 100 to the first element.

grades[4] = 10;

Assigns value of 10 to the last element.

Page 4: Arrays

Limits and Advice

Arrays must have a variable type.

The first element of an array is number 0.

The last is: size-1;

For now, we cannot create an array with

a variable size. (ex: int array[n];)

Unassigned elements should not be

assumed to be zero.

Use a separate for loop for each task.