Professional Documents
Culture Documents
Chapter 12
Overview
Array variable
int days[ 3 ]; Allocates memory for 3 ints
days 2 3 4
1st element 2nd element 3rd element
4
What arrays look like
• Things to notice
0 1 2 3 4 5 6
myArray
• An array of shorts:
short someArray [50];
• An array of floats:
float myArray [25];
• An array of booleans:
bool list [65];
• An array of chars:
char characters [255];
Modifying an Array
int clientAge;
clientAge = myArray [ 4 ];
0 1 2 3 4
0 0 0 0 0
Line by Line
0 1 2 3 4 0
0 0 0 0 0
Line by Line
0 1 2 3 4 0
0 0 0 0 0
Line by Line
0 1 2 3 4 0
0 0 0 0 0
Line by Line
0 1 2 3 4 0
42 0 0 0 0
Line by Line
0 1 2 3 4 1
42 0 0 0 0
Line by Line
0 1 2 3 4 1
42 0 0 0 0
Line by Line
0 1 2 3 4 1
42 0 0 0 0
Line by Line
0 1 2 3 4 1
42 42 0 0 0
Line by Line
0 1 2 3 4 2
42 42 0 0 0
Line by Line
0 1 2 3 4 2
42 42 0 0 0
Line by Line
0 1 2 3 4 2
42 42 0 0 0
Line by Line
0 1 2 3 4 2
42 42 42 0 0
Line by Line
0 1 2 3 4 3
42 42 42 0 0
Line by Line
0 1 2 3 4 3
42 42 42 0 0
Line by Line
0 1 2 3 4 3
42 42 42 0 0
Line by Line
0 1 2 3 4 3
42 42 42 42 0
Line by Line
0 1 2 3 4 4
42 42 42 42 0
Line by Line
0 1 2 3 4 4
42 42 42 42 0
Line by Line
0 1 2 3 4 4
42 42 42 42 0
Line by Line
0 1 2 3 4 4
42 42 42 42 42
Line by Line
0 1 2 3 4 5
42 42 42 42 42
Line by Line
false
byte myList [5];
for (int counter = 0; counter < 5; counter++) {
myList [counter] = 42;
} counter
0 1 2 3 4 5
42 42 42 42 42
Finding the Smallest Element
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
} // if
} // for
0 1 2 3 4 smallestSoFar
42 17 42 -8 4
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
} // if
} // for
0 1 2 3 4 smallestSoFar
42 17 42 -8 4
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
} // if
} // for
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 42
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 1
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 42
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 1
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 42
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) { Is 42 > 17?
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 1
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 42
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 1
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 1
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 2
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 2
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) { Is 17 > 42?
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 2
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 2
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 3
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 3
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) { Is 17 > -8?
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 3
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 17
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 3
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 3
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 4
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 4
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) { Is -8 > 4?
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 4
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 4
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 5
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 5
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Another Trace
int smallestSoFar;
smallestSoFar = randomArray[0];
for (int counter = 1; counter < 5; counter++) {
if (smallestSoFar > randomArray[counter]) {
smallestSoFar = randomArray[counter];
counter
} // if
} // for 5
0 1 2 3 4 smallestSoFar
42 17 42 -8 4 -8
Passing Arrays to Functions
#include <iostream.h>
void main ( ) {
int myArray [15];
modifyArray (myArray, 15);
}
The Overall Concept
If fewer values are listed than there are indexed variables, remaining
indexed variables are initialized to zero of the array base type
62
Initializing Strings
If string constant is used, null terminator is automatically included
Element Sized automatically to length of
char short_str [ ] = “abc”; string + 1 for the ‘\0’
data type
64
Parallel Arrays
Using two or more arrays to represent relationships
involving different data types
Number of employees
const int NUMEMPS;
Elements int hours [NUMEMPS]; Stores hours worked by each
are integers employee
Elements float payRate [NUMEMPS]; Stores pay rate for each
are floats . . . employee
for ( int index = 0; index < NUMEMPS; index++ )
{
cout << “Hours employee #” << ( index + 1 );
cin >> hours[ index ];
cout << “Pay rate employee #” << ( index + 1 );
cin >> payRate[ index ]; Same index used to
access both arrays
}
65
Printing Array Contents
Use a loop to display the contents of each array
element
Declare and
int testArr [ 5 ] = { 10, 20, 30, 40, 50 }; initialize array
Doesn’t work!
Displays address of the
cout << testArr << endl; array, not the contents
Works!
for ( int ct = 0; ct < 5; ct++ ) Loop displays value
cout << testArr [ ct ] << endl; of each element
66
Array Elements as Function Arguments
67
Arrays as Function Arguments
70
Two-dimensional Arrays
Two-dimensional array is several identical arrays put
together in the form of a table
Exam scores
column 0 column 1 column 2
row 0 score[0][0] score[0][1] score[0][2]
Students row 1 score[1][0] score[1][1] score[1][2]
row 2 score[2][0] score[2][1] score[2][2]
float score [ 3 ] [ 3 ];
Assign value to an element
score [ 1 ] [ 2 ] = 93.2;
Row index
cout << score [ 0 ] [ 2 ];
Column index Display value of an element
71
Two-dimensional Arrays
Nested loops are used to process each element of a
two-dimensional array
float score [ 3 ] [ 3 ];
for ( int std = 0; std < 3; std++ ) Outer loop iterates over rows
{ Inner loop iterates over columns
for ( int exam = 0; exam < 3; exam++ )
{
cout << “Student “ << std + 1 << “ , exam “ << exam + 1 << “: “;
cin >> score [ std ] [ exam ]; Read in an exam score
}
Row Column
cout << endl; index index
Program output:
}
score 0 1 2 Student 1, exam 1: 92.3
Student 1, exam 2: 88.5
0 92.3 88.5 83.6 Student 1, exam 3: 83.6
1 Student 2, exam 1: 79.2
79.2 72.8 ?
Student 2, exam 2: 72.8
2 ? ? ? . . .
72
Two-dimensional Arrays as
Function Arguments
Number of columns is specified in a two-dimensional
array parameter Empty Column index
Number of rows
void showArr ( int Arr [ ] [ 2 ], int rows );
void main ( ) Extra braces that enclose each
{ row’s values are optional
int table [ 3 ] [ 2 ] = { { 8, 5 }, { 7, 9 }, { 6, 3 } };
showArr ( table, 3 );
Row 1 Row 2 Row 3
}
void showArr ( int Arr [ ] [ 2 ], int rows )
{
for ( int r = 0; r < rows; r++ ) Iterates rows Program output:
{
for ( int c = 0; c < 2; c++ ) Iterates columns 8 5
cout << Arr [ r ] [ c ] << “ “; 9 9
cout << endl; 6 3
}
}
73
Arrays Strings
A two-dimensional array of characters can be used
as multiple arrays of strings
char team [ 4 ] [ 9 ] = { “Ned”, “Connie”, “Pat”, “Greg” };
0 N e d \0 Four names,
8 characters long
1 C o n n i e \0
Maximum length of
2 P a t \0 string is 9 – 1
3 G r e g \0 (for null terminator)