Chapter 9 OneDimensional Numeric Arrays - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chapter 9 OneDimensional Numeric Arrays

Description:

Examples: int c[32]; int c[-25], b[43.5]; Lesson 9.1. Valid ... Keyword enum. Makes code more readable. Example: enum {sun = 1, mon, tues, wed, thur, fri, sat} ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 27
Provided by: RalphFTo5
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9 OneDimensional Numeric Arrays


1
Chapter 9 One-Dimensional Numeric Arrays
2
Array
  • Data structure
  • Grouping of like-type data
  • Indicated with brackets containing positive
    integer constant or expression following
    identifier
  • Subscript or index
  • Loops commonly used for manipulation

Lesson 9.1
3
One-Dimensional Arrays
  • Declaration indicates name and reserves space for
    all elements
  • Values assigned to array elements using
    assignment statements
  • Array names classified as identifiers
  • Programmer sets size of array explicitly

Lesson 9.1
4
Array Length
  • Determined by expression or value enclosed in
    brackets in declaration
  • General form for declaration type
    namevalue
  • Value in brackets can be constant variable,
    expression, or literal

const int N 26 double bN
int a25 int b52
Lesson 9.1
5
Array Length
  • Must be integer constant greater than 0
  • Only integer type variables (with modifiers)
  • int, char
  • signed, unsigned, short, long
  • Always within brackets following identifier
  • Examples int c32
    int c-25, b43.5

Valid
Lesson 9.1
6
Array Subscripts
  • First index or subscript is 0
  • int a2
  • Data type of elements is int
  • Name of array is a
  • Number of elements is 2
  • Valid subscripts are 0 and 1
  • Address as a0 and a1

Lesson 9.1
7
Printing Array Elements
  • Use cout
  • Treat like single variable
  • Print one element at a time
  • Example cout ltlt "a0 " ltlt a0 ltlt endl

Lesson 9.1
8
Initialization
  • In declarations enclosed in curly braces

int a5 11,22
Declares array a and initializes first two
elements and all remaining set to zero
int b 1,2,8,9,5
Declares array b and initializes all elementsand
sets the length of the array to 5
Lesson 9.2
9
Working With Arrays
  • Remember subscripts must calculate to an integral
    value
  • Normally use loop to control array processing
  • Common loop is for loop
  • Start at zero
  • Continue while loop control variable lt N
  • N is the size of the array

Lesson 9.2
10
Using An Array
  • Declare int test3
  • sets aside 3 storage locations
  • use index to reference the variables
  • test0 86
  • test1 92
  • test2 90

86
92
90
Lesson 9.2
11
Example
  • Declare and fill an array which will hold 5
    double numbers double
    num5 for (int a 0 a lt 5 a) cin
    gtgt numa

Initialize by readingfrom the keyboard
Lesson 9.2
12
Input/Output
  • Typically use files
  • Reading from file, number of elements ?
  • Guard against reading past end of file
  • Use function eof( ) which returns 1 when end
  • Reading from file, number of elements known

Lesson 9.3
13
Loop to Read Data Into an Array
Number of elements unknown!
for (j 0 !infile1.eof ( ) j)
infile1 gtgt aj
Example of for loop
Lesson 9.3
14
Loop to Read Data Into an Array
Number of elements unknown!
length 0 infile gtgt data while ((length lt
max_array_size) ! infile.eof ( ))
alength data length infile gtgt
data
Example of while loop
Lesson 9.3
15
File Input with Known Number of Elements
infile1 gtgt num_elem for (j 0 j lt num_elem
j) infile1 gtgt aj
First line of file contains number of array
elements.
16
File Input Sentinel Value
  • Particular predefined value contained in file
    that indicates end of data group
  • Loop and read data value by value until sentinel
    is read

for (j 0 aj ! -1 j)
infile1 gtgt aj
17
Loop to Print Data From Array(scores is array of
20 test scores)
for (int j 0 j lt 20 j)
cout ltlt scores j ltlt endl
Lesson 9.3
18
Arrays and Functions
  • Pass address of array to function instead of
    element values
  • Function declaration
  • Data type and empty brackets int
  • Function call
  • Array name with no brackets x
  • Function header
  • Data type, name, and empty brackets int x

Lesson 9.4
19
Passing Array Information
  • Three pieces of information
  • Size of single array element
  • Indicated by data type
  • Address of array
  • Indicated by array name in function call
  • Location to store array address
  • Indicated by identifier followed by brackets in
    function header

Lesson 9.4
20
General Form
rtype function (type , int) function (array,
num) rtype function (type b , int num_elem)
Declaration
Call
Header
Lesson 9.4
21
Classes with Array Data Members
  • Automatically made accessible to public class
    member functions
  • Use enumeration to size data member arrays
  • Comma-separated list of identifiers
  • Identifier automatically assigned constant
    integer value
  • Value assigned depends on order in enumeration
    list

Lesson 9.5
22
Enumeration
  • Keyword enum
  • Makes code more readable
  • Example enum sun 1, mon, tues, wed, thur,
    fri, sat
  • Assigns integer constant 1 to sun, 2 to mon, etc.
  • Example of usage int day
    day wed

Same as day 4
Lesson 9.5
23
Find Smallest Value in Array
small scores 0 for (int j 1 j lt 20 j)
if (scores j lt small)
small scores j
Lesson 9.5
24
Arrays of Objects
  • Declared using class name, object name, and
    brackets enclosing integer constant
    Vehicle truck3
  • Three objects (truck0, truck1, truck2 of
    class Vehicle
  • Call member function
  • object, dot operator, and function name
  • truck0.set_data (50, 2, 3)

Lesson 9.6
25
Arrays of Objects
  • Good when multiple pieces of information are
    linked
  • Use assignment statement to copy one object array
    element to another
  • truck1 truck0

Lesson 9.6
26
Summary
Learned how to
  • Create and manipulate arrays
  • Trace and debug loops that manipulate arrays
  • Reserve memory during program execution
  • Use arrays to solve problems
Write a Comment
User Comments (0)
About PowerShow.com