COMFUN2 Chapter 11 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CHAPTER 11: ONE-DIMENSIONAL ARRAYS • dataType is the type of data that the array

variables (elements) will store


Arrays • arrayName is name of array (same rules for
• A simple variable (also called a scalar naming an array as for naming a variable)
variable) is unrelated to any other variable • numberOfElements is an integer specifying
in memory the size of the array (enclosed in square
• Sometimes variables are related to each brackets)
other • You may initialize array elements by entering
• Easier and more efficient to treat these as a one or more values, separated by commas,
group in braces
• A group of related variables with the same • Assigning initial values is referred to as
data type is referred to as an array populating the array
• Storing data in an array increases the • The values used to populate an array should
efficiency of a program have the same data type as the array
→ Data can be accessed from internal variables
memory faster than it can be from a → Otherwise, implicit type conversion is
file on a disk performed
→ Once stored in an array, data can be • Most C++ compilers initialize uninitialized
used as many times as necessary numeric array elements to 0.0 or 0
without having to enter it again (depending on data type)
• Variables in an array can be used like any • Automatic initialization is only done if you
other provide at least one value in the initialValues
• Most commonly used arrays in business section
applications are one-dimensional and two- • Be sure to include an appropriate number of
dimensional initial values
→ Providing too many will cause a
One-Dimensional Arrays compiler error or incorrectly assign
• Variables in an array are stored in adjacent memory locations
consecutive locations in computer’s internal
memory Entering Data into a One-Dimensional Array
• Each variable in an array has the same • You can use an assignment statement or the
name and data type extraction operator to enter data into an
• You distinguish one variable in a one- array element
dimensional array from another variable in • Syntax of assignment statement is:
the same array by using a unique integer, arrayName[subscript] = expression;
called a subscript → expression can include any combination
• A subscript indicates a variable’s position in of constants, variables, and operators
the array and is assigned by the computer → Data type of expression must match
when the array is created data type of array; otherwise, implicit
• First variable in a one-dimensional array is type conversion will occur
assigned a subscript of 0, second a
subscript of 1, and so on Displaying the Contents of a One-Dimensional
• You refer to a variable in an array by the Array
array’s name immediately followed by a • To display the contents of an array, you
subscript enclosed in square brackets need to access each of its elements
(e.g., sales[0]) • Use a loop along with a counter variable that
• The last subscript in an array is always one keeps track of each subscript in the array
less than the total number of variables in the
array, since the subscripts begin at 0
Passing a One-Dimensional Array to a
Declaring and Initializing a One-Dimensional Function
Array • You can pass an array to a function by
• Must declare an array before you can use it including the array’s name as the actual
• Also good programming practice to initialize argument
array variables • Unless specified otherwise, scalar variables
• Syntax for declaring and initializing a one- in C++ are passed by value
dimensional array is: • Arrays, however, are passed by reference
dataType arrayName [numberOfElements] = by default, because it is more efficient
{initialValues};
• Passing an array by value would require • Program uses two parallel one-dimensional
copying the entire array, which could be very arrays
large → char array named types: stores the
• Passing an array by reference allows the five membership types
computer to pass the address of only the → int array named fees: stores annual
first array element fee associated with each type
→ Since elements are stored in contiguous • Two arrays are referred to as parallel
memory locations, computer can use this arrays if their elements are related by their
address to locate remaining elements in position in the arrays
the array
• Indicate that you are passing an array by Summary
entering formal parameter’s name and data ❖ An array is a group of variables that have
type, followed by empty square brackets the same name and data type
→ Address-of operator (&) is not needed in ❖ One- and two-dimensional arrays are the
function header or function prototype, most common types
since arrays are passed by reference by ❖ Arrays are often used to store related data
default in internal memory: more efficient to
access than from disk
The Random Numbers Program ❖ Must declare an array before using it
• Program’s main function assigns random ❖ After declaration, you can use an
numbers between 1 and 100 to a five- assignment statement or extraction
element int array named randNums operator to enter data into it
• Calls a program-defined void function to ❖ Each element of a one-dimensional array
display contents of array is assigned a unique number, called a
• Then calls a program-defined value- subscript
returning function to determine highest ❖ First element is assigned a subscript of 0,
number in array second a subscript of 1, and so on
• main function then display’s highest value ❖ Last subscript of a one-dimensional array
is always one number less than the
Sorting the Data Stored in a One-Dimensional number of elements
Array ❖ You refer to an element by the array’s
• You sometimes need to arrange the name followed by the subscript in square
contents of an array in either ascending or brackets
descending order ❖ Parallel arrays are two or more arrays
• Arranging data in a specific order is called whose elements are related by their
sorting position in the arrays
• When a one-dimensional array is sorted in
ascending order, first element contains
smallest value and last element contains
largest value CHAPTER 12: TWO-DIMENSIONAL ARRAYS
• Conversely, when sorted in descending
order, first element contains largest value Using Two-Dimensional Arrays
and last element contains smallest value • Recall that an array is a group of related
• Program’s main function assigns random variables that have the same data type
• Many different types of sorting algorithms • You can visualize a one-dimensional array
• Bubble sort provides a quick and easy way as a column of variables in memory
to sort items stored in an array, as long as • You can visualize a two-dimensional array
the number of items is relatively small (e.g., as a table in that the variables are in rows
fewer than 50) and columns
• Works by comparing adjacent array • You can determine the number of variables
elements and swapping ones that are out of in a two-dimensional array by multiplying the
order number of rows by the number of columns
• Continues comparing and swapping until • Each variable in a two-dimensional array is
data in the array are sorted identified by a unique combination of two
subscripts specifying the variable’s row and
Parallel One-Dimensional Arrays column position
• Program for a motorcycle club displays • Variables located in the first, second, etc.
annual fee associated with membership type row of a two-dimensional array are assigned
entered by user a row subscript of 0, 1, etc.
• Similarly, variables located in the first, • Syntax for extraction operator:
second, etc. column of a two-dimensional cin >> arrayName
array are assigned a column subscript of 0, [rowSubscript][columnSubscript ];
1, etc.
• You refer to each variable in a two- Displaying the Contents of a Two-Dimensional
dimensional array by the array’s name Array
immediately followed by the variable’s row • To display the contents of a two-dimensional
and column subscripts (in that order), each array, you must access its individual
enclosed in its own set of square brackets elements
• As with one-dimensional arrays, the last row • You can use two counter-controlled loops
and column subscripts of a two-dimensional → One to keep track of the row
array will always be one less than the subscript
number of rows and columns (respectively) → One to keep track of the column
in the array, since the subscripts begin at 0 subscript

Accumulating the Values Stored in a Two-


Declaring and Initializing a Two-Dimensional Dimensional Array
Array • Program for the Jenko Booksellers company
• You must declare a two-dimensional array uses a two-dimensional array to store the
before you use it sales made in each of the company’s three
→ As with one-dimensional arrays, it bookstores
is good practice to initialize the • Array contains three rows and two columns
array variables → First column contains sales amount for
• Syntax for declaring a two-dimensional paperback books sold in each of three
array: stores
dataType arrayName [numberOfRows] → Second column contains sales amounts
[numberOfColumns] = for hardcover books in the three stores
{{initialValues}, {initialValues }, • Program calculates total sales by
…{initialValues }}; accumulating amounts stored in array and
• dataType specifies the data type of elements displays total sales
in the array (all elements must have same
data type) Searching a Two-Dimensional Array
• numberOfRows and numberOfColumns • Program for the Wilson Company uses a
specify number of rows and columns in four-row, two-column array to store the
array, respectively company’s four pay codes and their
• You initialize elements in a two-dimensional corresponding pay rates
array by entering a separate initialValues • Pay codes are stored in first column of each
section, enclosed in braces, for each row of row
the array • Pay rate associated with each code is stored
• Maximum number of initialValues sections in the same row as its pay code, in the
you may enter is the number of rows in the second column
array • Program gets a pay code from user and
• Within individual initialValues sections, you searches for pay code in the array’s first
enter one or more values separated by column
commas • If pay code is found, corresponding pay rate
• Maximum number of values you may enter is displayed; otherwise, “Invalid pay code” is
in a row is the number of columns displayed

Entering Data into a Two-Dimensional Array Passing a Two-Dimensional Array to a


• You can use either an assignment statement Function
or the extraction operator to enter data into • Like one-dimensional arrays, two-
the elements of a two-dimensional array dimensional arrays are passed automatically
• Syntax for assignment statement: by reference
arrayName [rowSubscript] • When passing a two-dimensional array to a
[columnSubscript ] = expression; function, function header and prototype
→ expression can include any should contain formal parameter consisting
combination of constants, variables, of name of array followed by two sets of
and operators square brackets containing the number of
• Must match data type of array rows and columns, respectively
• Number of rows is not required (first set of
brackets may be left empty)

Summary
❖ A two-dimensional array resembles a
table in that elements are in rows and
columns
❖ Each element has the same data type
❖ You can determine the number of
elements in a two-dimensional array by
multiplying the number of rows by the
number of columns
❖ Each element in a two-dimensional array
is identified by a unique combination of
two subscripts representing the element’s
row and column, respectively.
❖ Accumulating the Values Stored in a
Two-Dimensional Array
❖ Program for the Jenko Booksellers
company uses a two-dimensional array to
store the sales made in each of the
company’s three bookstores
❖ Array contains three rows and two
columns
❖ First column contains sales amount for
paperback books sold in each of three
stores
❖ Second column contains sales amounts
for hardcover books in the three stores
❖ Program calculates total sales by
accumulating amounts stored in array
and displays total sales
❖ When declaring a two-dimensional array,
you must provide the number of rows as
well as the number of columns
❖ You can use an assignment statement or
the extraction operator to enter data into
an array
❖ You need to use two loops to access
every element in a two-dimensional array
❖ One of the loops keeps track of the row
subscript, and the other keeps track of
the column subscript
❖ To pass a two-dimensional array to a
function, you include the array’s name in
the statement that calls the function
❖ Array’s corresponding formal parameter
in the function header must specify the
formal parameter’s data type and name,
followed by two sets of square brackets
❖ First bracket contains the number of
rows, and the second bracket contains
the number of columns

You might also like