College of Information Technology: CSC 103: Computer Programming For Scientists and Engineers

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

University of Bahrain

College of Information Technology


Department of Computer Science

CSC 103: Computer Programming for


Scientists and Engineers

Dr. Abdul Fattah Salman

Unit 10: Two-dimensional Arrays


CSC 103: Computer Programming for
Scientists and Engineers

These slides are based on slides of Malik


(textbook author) and modified
by Dr. Abdul Fattah Salman
Review: Arrays

• An array is a collection of a fixed number of


elements, all of the same data type.
• For example, an array might contain:
– Five elements, each of which is a double.
– One hundred elements, each of which is an int.
– Seven thousand elements, each of which is a char.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3


Review: Array Dimension
• In a one-dimensional (1-D) array, you can think
of the elements as being arranged in a list.
• In a two-dimensional (2-D) array, you can
think of the elements as being arranged in a
table with rows and columns.
• Higher-dimensional arrays are also possible,
but we'll focus on one-dimensional arrays (last
week) and two-dimensional arrays (this week).

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4


Two-Dimensional Arrays
• So a two-dimensional array (sometimes called
a matrix or a table) is a collection of a fixed
number of elements of the same data type,
arranged in rows and columns.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5


Declaring a Two-Dimensional Array
• The syntax for declaring a 2-D array is:

where intExp1 and intExp2 are constant


expressions with positive integer values specifying
the number of rows and columns in the array,
respectively.
• Example: To declare a 2-D array of double
elements with 10 rows and 5 columns:
double sales[10][5];
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6
Declaring a Two-Dimensional Array
(cont’d.)

• Note that row numbers and column numbers


start at 0, just as with the index of a 1-D array.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
Accessing Array Elements
• To access an element in a two-dimensional
array, the syntax is:

where indexExp1 and indexExp2 are expressions


with positive integer values that specify the row and
column position, respectively.
• Example:
sales[5][3] = 25.75; //See next slide

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8


Accessing Array Elements (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9


Two-Dimensional Array Initialization
During Declaration
• You can initialize a 2-D array when you declare it.
– The elements of each row are enclosed within braces
and separated by commas.
– All rows are enclosed within another set of braces,
with commas separating the rows.
– For arrays of numbers, if you specify initial values for
some of the elements, then unspecified elements are
set to 0.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10


Two-Dimensional Array Initialization
During Declaration (cont’d.)
• Example: These lines breaks
are not required but
help you visualize
the array.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11


Example Program: Initializing and
Printing a 2-D Array
Making It More Flexible

 The program on the previous slide assumes


you have 4 rows and 3 columns. The numbers
4 and 3 are hard-coded into the code at more
than one place.
 Better way: Declare two constants named
NUMBER_OF_ROWS and
NUMBER_OF_COLUMNS at the program’s
start, and use those constants wherever you
need the number of rows or columns. This
makes it easier to revise the code to handle
more rows or columns.
 See next slide…
Making It More Flexible:
Example Program
Two-Dimensional Arrays and
Enumeration Types
• Enumeration types can be used for array indices.
This may be helpful because it lets you use
words instead of integers to refer to elements.
• Example: Keeping track of cars in stock:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15


Two-Dimensional Arrays and
Enumeration Types (cont’d)
• After we’ve assigned values to the array elements:

• To change the number of white Fords:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16


Processing Two-Dimensional Arrays
• Common ways to process a 2-D array:
– Process the entire array (using nested loops). Either:
– Row processing: process a single row at a time.
– Column processing: process a single column at a
time.
• Each row and each column of a 2-D array is itself
a 1-D array.
– To process a single row or a single column, use
algorithms similar to processing 1-D arrays.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
Printing
• Use nested loops to display all of the elements
in a 2-D array:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18


Using row and col as Loop Counter
Variables

 The programs on the previous slide and on the


following slides use row and col as loop counter
variables.

 There’s nothing magical about these names.


You could use i and j instead. But using row
and col can make your programs easier to read.
Initializing
• Examples:
– To initialize the entire 2-D array to 0:

• To initialize the elements in row number 4 to 0:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20


Inputting from the Keyboard
• Examples:
– To input data into each element of a 2-D array:

– To input values into row number 4:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21


Summing by Row
• Example:
– To find the sum of row number 4:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22


Summing by Column
• Example:
– To print the sum of each individual column:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23


Finding the Largest Element in Each
Row or Column
• Example:
– To print the largest element in each row:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24


Passing a 2-D Array as a Parameter to
a Function
• When an array is passed as a parameter to a
function, it is passed by reference (never by value).
– The base address is what’s actually passed.
• As noted earlier, two-dimensional arrays are
stored in row order.
• When declaring a 2-D array as a formal parameter,
you can omit the size of the first dimension, but
not of the second dimension.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25


Passing a 2-D Array as a Parameter to a
Function: Example
Computer Memory

• Computer memory is organized as a


sequence of billions of memory cells.
• Each memory cell has a fixed address (or
location) and holds a byte-sized unit of
information, which can
change with time.
Where in Memory Are Variables
Stored?
• The compiler decides which memory cells
to use for the variables in your program.
These cells will probably be different every
time you compile the program.
• Example:
• The statement
int num1 = 92;
causes the compiler to set aside some cells in
memory for num1 and to load the value 92 into
those cells. You can’t predict where in memory
the compiler will do this.
Why Should You Care?

• Most of the time you don’t care which


cells the compiler chooses, but
sometimes you do.
• Therefore, C++ gives you a way to find
out the address at which the compiler
has stored a variable….
Address-of Operator

• The address-of operator & returns the


address of a variable.

This address will


probably be different
every time you
recompile the program.
A Side Note: Hexadecimal Notation

• When dealing with memory addresses and


some other types of information, computer
engineers and programmers often use a
notation called hexadecimal notation (or
hex notation) when writing numbers.
• Our everyday decimal notation uses ten
digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) to express
numbers.
• Hex notation uses sixteen digits (0, 1, 2, 3,
4, 5, 6, 7, 8, 9, A, B, D, C, E, F) to express
numbers.
A Side Note: Hexadecimal Notation
(Cont’d.)
• If you take a course in Digital Logic you’ll
learn more about hex and how to convert
between decimal notation and hex notation.
• For now, don’t be
surprised if you see an
address expressed as
something like 31F96A.
• The Windows calculator
(set to Programmer
view) is handy for
converting between
decimal and hex.
Forcing Decimal Display of
Addresses

• In the previous program I used int() to


force num1’s address to be displayed in
decimal notation. Without the int(),
the address would have been displayed
in hex notation.
Most Variables Occupy Several Bytes

• How many memory cells (bytes)


does a variable occupy?
It depends on the variable’s
data type.
• For example, on most systems
an int variable occupies four
memory cells (four bytes of
memory).
• The sizeof operator tells you how
many cells the variables of a
particular data type occupy:
cout << sizeof(int) << endl;
Most Variables Occupy Several Bytes
(cont’d.)
• On the previous slide we used the
sizeof operator to find out how many
cells the variables of a particular data
type occupy:
cout << sizeof(int) << endl;
• We can also use it to find out how many
cells a specific variable occupies:
int myNum;
cout << sizeof(myNum) << endl;
Most Variables Occupy Several Bytes
(cont’d.)

• In an example above, we saw that num1


was stored in memory at address
3275108.

• Since num1 is an int variable, it needs


four memory cells, and actually occupies
the cells at addresses 3275108,
3275109, 3275110, and 3275111.
Arrays Use Contiguous Memory
Addresses

• If a program contains two int variables,


you can’t predict whether the compiler
will store them close together or far
apart in memory.
• But for an array, the compiler always
stores the elements contiguously in
memory (i.e., one right after another, in
order from element 0 to the array’s last
element).
Arrays Use Contiguous Memory
Addresses: Example

Each element’s address is the


previous element’s address plus 4.
Computing the Address of an Element
in a 1-D Array
• To compute the address of a particular
element in a 1-D array, you just need to
know three things:
• The array’s base address (i.e., the address of
element 0).
• The data type of the elements.
• The index of the desired element.
• Example: in a 1-D int array whose base
address is 1560000, element 37 starts at
address 1560148, because:
1560000 + 4 * 37 = 1560148
Passing a 1-D Array as a Parameter
to a Function
 Recall that when an array is passed as a
parameter to a function, it is passed by
reference (never by value).
 The base address is what’s actually passed.
 Knowing this base address and the data type of the
array, the function can figure out the address of any
element, just as we did on the previous slide.
How Are Two-Dimensional Arrays
Stored?

• With a 2-D array, the elements are all


stored in contiguous memory addresses.
But we can imagine two different ways of
doing this: either
• Row-order: All of the row-0 elements, then
all of the row-1 elements, then all of the row-
2 elements, and so on; or…
• Column-order: All of the column-0 elements,
then all of the column-1 elements, then all of
the column-2 elements, and so on.
• Which way does C++ actually do it?
How Are Two-Dimensional Arrays
Stored? (Cont’d)

• Consider the declaration:


char myArray[2][2]={{'a','b'}, {'c','d'}};

Using row-order Using column-order

• C++ always uses row-order.


Two-Dimensional Arrays Are Stored in
Row Order: Example

First come the elements in row 0,


then the elements in row 1.
Passing a 2-D Array as a Parameter to
a Function
• When an array is passed as a parameter to a
function, it is passed by reference (never by value).
– The base address is what’s actually passed.
• As noted earlier, two-dimensional arrays are
stored in row order.
• When declaring a 2-D array as a formal parameter,
you can omit the size of the first dimension, but
not of the second dimension.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 44


Passing a 2-D Array as a Parameter to
a Function: Example
Multidimensional Arrays
• n-dimensional array: collection of a fixed
number of elements arranged in n dimensions
(n >= 1)
• Declaration syntax:

• To access an element:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 46

You might also like