chapter 9 – one-dimensional numeric arrays. array u data structure u grouping of like-type data u...

26
Chapter 9 – One- Dimensional Numeric Arrays

Upload: alexis-nolan

Post on 27-Mar-2015

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Chapter 9 – One-Dimensional Numeric Arrays

Page 2: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 3: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 4: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Array Length Determined by expression or value enclosed

in brackets in declaration General form for declaration

type name[value]; Value in brackets can be constant variable,

expression, or literal

Lesson 9.1

const int N = 26;double b[N];

int a[25];int b[5+2];

Page 5: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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 c[32];

int c[-25], b[43.5];

Lesson 9.1

ValidInvalid

Page 6: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Array Subscripts

First index or subscript is 0 int a[2];

– Data type of elements is int– Name of array is a– Number of elements is 2– Valid subscripts are 0 and 1

Address as a[0] and a[1]

Lesson 9.1

Page 7: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Printing Array Elements

Use cout Treat like single variable Print one element at a time Example:

cout << "a[0] = " << a[0] << endl;

Lesson 9.1

Page 8: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Initialization

In declarations enclosed in curly braces

Lesson 9.2

int a[5] = {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

Page 9: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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 < N

N is the size of the array

Lesson 9.2

Page 10: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Using An Array

Declare int test[3];– sets aside 3 storage locations

use index to reference the variables

test[0] = 86;

test[1] = 92;

test[2] = 90;

86 92 90

Lesson 9.2

Page 11: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Example

Declare and fill an array which will hold 5 double numbers

double num[5];for (int a = 0; a < 5; a++)

cin >> num[a];

Initialize by readingfrom the keyboard

Initialize by assigningvalues in loop

num[a] = 0;

Lesson 9.2

Page 12: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 13: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Loop to Read Data Into an Array

for (j = 0; !infile1.eof ( ); j++) { infile1 >> a[j]; }

Example of for loop

Lesson 9.3

Number of elements unknown!

Page 14: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Loop to Read Data Into an Array

length = 0;infile >> data;while ((length < max_array_size) && ! infile.eof ( )){ a[length] = data; ++length; infile >> data;}

Lesson 9.3

Example of while loop

Number of elements unknown!

Page 15: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

File Input with Known Number of Elements

First line of file contains number of array elements.

infile1 >> num_elem;for (j = 0; j < num_elem; j++) { infile1 >> a[j]; }

Page 16: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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; a[j] != -1; j++) { infile1 >> a[j]; }

Where –1 is thesentinel value.

Page 17: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Loop to Print Data From Array(scores is array of 20 test scores)

for (int j = 0; j < 20; ++j) { cout << scores[ j ] << endl; }

Lesson 9.3

Page 18: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 19: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 20: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

General Form

Lesson 9.4

rtype function (type [ ], int);function (array, num);

rtype function (type b[ ], int num_elem)

DeclarationCall

Header

Function return type

Function name

Type of values stored in array

Type of second argumentArray name in calling function

Number of array elements

b is identifier used to representarray within function

num_elem used to represent numberof array elements in function

Page 21: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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

Page 22: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

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;

Lesson 9.5Same as day = 4;

Page 23: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Find Smallest Value in Array

small = scores [0];for (int j = 1; j < 20; ++j) { if (scores[ j ] < small) small = scores [ j ]; }

Lesson 9.5

Page 24: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Arrays of Objects

Declared using class name, object name, and brackets enclosing integer constant Vehicle truck[3];– Three objects (truck[0], truck[1], truck[2] of

class Vehicle Call member function

– object, dot operator, and function name

truck[0].set_data (50, 2, 3);

Lesson 9.6

Page 25: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Arrays of Objects

Good when multiple pieces of information are linked

Use assignment statement to copy one object array element to another

truck[1] = truck[0];

Lesson 9.6

Page 26: Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer

Summary

Create and manipulate arrays Trace and debug loops that manipulate

arrays Reserve memory during program execution Use arrays to solve problems

Learned how to: