COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

11
COMP102 Lab 08 1 COMP 102 Programming Fundamentals I Presented by : Timture Choi

Transcript of COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

Page 1: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 1

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

Page 2: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 2

Arrays

An array is a collection of data elements That are of the same type

E.g. Collection of integers

Integer array Collection of characters

Character array

Each data in the collection is called An element

Page 3: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 3

Array Declaration

Syntax: <type> <arrayName>[<array_size>];

E.g. int array[10];

Array elements Are values of the type

<type> E.g.

All element stored in “int array[10]” are integer

Page 4: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 4

Array Declaration

Size of the array Represent the number of elements in the array Indicated by <array_size> E.g.

“int array[10]” Can use to store 10 integers

<array_size> must be An int constant A constant expression

Note An array can have multiple dimensions E.g.

int array[10][20];

Page 5: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 5

Array Declaration

E.g. // array of 10 integer // which is uninitialized int array[10];

-- -- ---- array -- -- ---- -- --

4 5 6 3 0 2 8 9 7 1

Page 6: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 6

Subscripting

To access an individual element Must apply a subscript to the corresponding

array

A subscript Use bracketed expression E.g.

array[x]

The expression in the brackets Known as the index E.g.

x stated in array[x]

Page 7: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 7

Subscripting

Note First element of array

Has index 0 array[0]

Second element of array Has index 1, and so on array[1], array[2], array[3], …

Last element Has an index [array_size – 1]

array[array_size – 1] E.g.

If an array is declared as array[10] Last element is array[9]

Page 8: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 8

Subscripting

E.g. // array of 10 uninitialized ints int array[10];

array[3] = 1; int x = array[3];

-- -- 1--array -- -- ---- -- --

4 5 6 3 0 2 8 9 7 1

Page 9: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 9

Subscripting

Use for loop to access/process all the elements of an arrayE.g.

for (i=0; i<array_size; i++){// assign value to an element of array array[i] = x; // assign value of an element to a variable x = array[i]; …}

Page 10: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 10

Array Initialization

Declaration only reserves memory for the elements in the array No values will be stored

To initialize an array Assign value to each of the element

E.g. int array[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

8 7 6 9array 4 3 25 1 0

4 5 6 3 0 2 8 9 7 1

Page 11: COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

COMP102 Lab 08 11

SUMMARY

By the end of this lab, you should be able to: Declare and manipulate both

1-D arrays