COMP102 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

COMP102

Description:

TA: Jackie Lo. Lecture Review. Arrays. A collection of data elements with the same type ... Each data in the collection is called. An element. Lecture Review ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 13
Provided by: jack52
Category:

less

Transcript and Presenter's Notes

Title: COMP102


1
COMP102 Programming Fundamentals I
  • LA2B (Mon 5-7pm)
  • LA2E (Fri 3-5pm)
  • LA2F (Fri 5-7pm)
  • TA Jackie Lo

2
Lecture Review
  • Arrays
  • A collection of data elements with the same type
  • E.g.
  • Collection of integers
  • Integer array
  • Collection of characters
  • Character array
  • Each data in the collection is called
  • An element

3
Lecture Review
  • Array Declaration
  • Syntax
  • lttypegt ltarrayNamegtltarray_sizegt
  • E.g.
  • int array10
  • Array elements
  • Are values of the type
  • lttypegt
  • E.g.
  • All element stored in int array10 are integer

4
Lecture Review
  • Size of the array
  • Represent the number of elements in the array
  • Indicated by ltarray_sizegt
  • E.g.
  • int array10
  • Can use to store 10 integers
  • ltarray_sizegt must be
  • An int constant
  • A constant expression
  • Note
  • An array can have multiple dimensions
  • E.g.
  • int array1020

5
Lecture Review
  • E.g.
  • // array of 10 integer
  • // which is uninitialized
  • int array10

6
Lecture Review
  • Subscripting
  • To access an individual element
  • Must apply a subscript to the corresponding array
  • A subscript
  • Use bracketed expression
  • E.g.
  • arrayx
  • The expression in the brackets
  • Known as the index
  • E.g.
  • x stated in arrayx

7
Lecture Review
  • Note
  • First element of array
  • Has index 0
  • array0
  • Second element of array
  • Has index 1, and so on
  • array1, array2, array3,
  • Last element
  • Has an index array_size 1
  • arrayarray_size 1
  • E.g.
  • If an array is declared as int array10
  • Last element is array9

8
Subscripting
  • E.g.
  • // array of 10 uninitialized ints
  • int array10
  • array3 1
  • int x array3

9
Lecture Review
  • Use for loop to access/process all the elements
    of an array
  • E.g.

for (i0 iltarray_size i) // assign value to
an element of array arrayi x // assign
value of an element to a variable x
arrayi
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 array10 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
11
SUMMARY
  • By the end of this lab, you should be able to
  • Declare and manipulate both
  • 1-D arrays

12
Lab10
  • Download the template from website
Write a Comment
User Comments (0)
About PowerShow.com