Title: Lecture 21:Arrays and Strings(cont.)
1Lecture 21Arrays and Strings(cont.)
- Introduction to Computer Science
- Spring 2006
2Arrays
- Array - a collection of a fixed number of
elements - All the elements of an array must be the same
data type - The elements of an array are stored sequentially
in memory. - One-dimensional array - an array in which the
components are arranged in a list form - The syntax for declaring a one-dimensional array
is - dataType arrayNameintExp
- where intExp is any expression that
evaluates to a positive integer - Example int num5
3include ltiostreamgt include ltiomanipgt using
namespace std const int arraySize 10 void
fill_Array(double list, int listSize)
for(int i0 iltlistSize i)
cingtgtlisti void print_Array(double list,
int listSize) for(int i0 iltlistSize i)
coutltltsetw(5)ltltlisti void
copy_Array(const double listOne, double
listTwo, int listOneSize) for(int i0
iltlistOneSize i) listTwoi
listOnei void square_Array(double list,
int listSize) int k for (int i 0 i lt
listSize i) k i 1 / i runs from 0
to 9 / / k runs from 1 to 10 / listi
kk cout ltlt "The square of " ltlt k ltlt " is " ltlt
listi ltlt endl int main() double
listAarraySize0 double listBarraySize
fill_Array(listA, arraySize) print_Array(listA,
arraySize) copy_Array(listA, listB,
arraySize) square_Array(listA, arraySize)
4Two-Dimensional Arrays
- Two-dimensional Array a collection of a fixed
number of components arranged in two dimensions - The syntax for declaring a two-dimensional array
is - dataType arrayNameintexp1intexp2
- where intexp1 and intexp2 are expressions
yielding positive integer values - The two expressions intexp1 and intexp2 specify
the number of rows and the number of columns,
respectively, in the array - Two-dimensional arrays are sometimes called
matrixes or tables - Two-dimensional arrays are stored in row order
- The first row is stored first, followed by the
second row, followed by the third row and so on
5Two-Dimensional Arrays(cont.)
6Accessing Array Components
- The syntax to access a component of a
two-dimensional array is - arrayNameindexexp1indexexp2
- where indexexp1 and indexexp2 are expressions
yielding nonnegative integer values - indexexp1 specifies the row position and
indexexp2 specifies the column position - Example sales5325.75
7Accessing Array Components
- A common way to access the elements of a
two-dimensional arrays is with nested for
loops.const int MAXI 50const int MAXJ
75int main() -
- int i int j float valuesMAXIMAXJ for
(i 0 i lt MAXI i) Â Â Â Â for (j 0 j lt
MAXJ j) Â Â Â Â Â Â Â Â valuesij
whatever     -
8Initialization
- Like one-dimensional arrays
- Two-dimensional arrays can be initialized when
they are declared - Example
- int board43 2, 3, 1,
- 15, 25, 13,
- 20, 4, 7,
- 11, 18, 14
- To initialize a two-dimensional array when it is
declared - Elements of each row are enclosed within braces
and separated by commas - All rows are enclosed within braces
- For number arrays, if all components of a row
are not specified, the unspecified components are
initialized to zero
9Processing Two-Dimensional Arrays
- A two-dimensional array can be processed in three
different ways - Process the entire array
- Process a particular row of the array, called row
processing - Process a particular column of the array, called
column processing
10Processing Two-Dimensional Arrays Row
processing and Column processing
- We can process a particular row or column of a
two-dimensional array as a one-dimensional array - use algorithms similar to processing
one-dimensional arrays - For example
- the following for loop initializes row four to
zero - row 4
- for(col 0 col lt columns col)
- matrixrowcol 0
- The following for loop inputs data in row 4 of
matrix - row 4
- for(col 0 col lt columns col)
- cingtgtmatrixrowcol
11Processing Two-Dimensional Arrays Entire Array
- We can process the entire array by using nested
for loop - Example
- The following nested for loop initialize the
entire matrix - for(row 0 row lt rows row)
-
- for(col 0 col lt columns col)
-
- matrixrowcol 0
-
-
12Print
- The following nested for loops print the
components of matrix, one row per line - for(row 0 row lt rows row)
-
- for(col 0 col lt columns col)
-
- coutltltsetw(5)ltltmatrixrowcolltlt" "
-
- coutltltendl
-
13Input
- The following nested loop input data in each
component of the matrix -
- for(row 0 row lt rows row)
- for(col 0 col lt columns col)
- cingtgtmatrixrowcol
14Passing Two-Dimensional Arrays as Parameters to
Functions
- Two-dimensional arrays can be passed as
parameters to a function - By default, arrays are passed by reference
- The base address, that is, the address of the
first component of the actual parameter is passed
to the formal parameter - When declaring a two-dimensional array as a
formal parameter - Can omit size of first dimension, but not the
second - Number of columns must be specified
15include ltiostreamgt include ltiomanipgt using
namespace std const int MAXI 10 const int
MAXJ 15 void init_matrix(double
matrixMAXIMAXJ) for(int i0 iltMAXI
i) for(int j0 jltMAXJ
j) matrixijij void
print_matrix(double matrixMAXIMAXJ) for(int
i0 iltMAXI i) for(int
j0 jltMAXJ j) coutltltsetw(5)ltltmatrixij
coutltltendl double
sum_matrix(double matrixMAXIMAXJ) double
sum0.0 for(int i0 iltMAXI i)
for(int j0 jltMAXJ j) sum sum
matrixij return sum int
main() double matrixMAXIMAXJ double
sum init_matrix(matrix) print_matrix(matrix)
sum sum_matrix(matrix) coutltlt"The sum of
all the elements of matrix is "ltltsumltltendl
16End of lecture 21