Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 2261 Unit 10 Two-dimensional Arrays

Similar presentations


Presentation on theme: "EGR 2261 Unit 10 Two-dimensional Arrays"— Presentation transcript:

1 EGR 2261 Unit 10 Two-dimensional Arrays
Read Malik, pages in Chapter 8. Homework #10 and Lab #10 due next week. Quiz next week (covers Units 9 and 10). Handouts: Quiz #9

2 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 two dimensions. 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. 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.)
-Do practice question 1. 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 initialize any 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: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

11 Example Program: Initializing and Printing a 2-D Array
Demo using week10TwoDInitialization.cpp, and show what happens if you leave out the last elements in a row or the last row. -Do practice question 2.

12 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…

13 Making It More Flexible: Example Program
Run it (week10TwoDInitializationmproved.cpp).

14 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: -Do practice question 3. 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 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: Demo using week10TwoDInitializationImproved.cpp, and show what happens if you remove the cout << endl. Also remind that you can’t just do cout << board. (This will print the array’s base address, not the elements in the 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 next 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. -Do practice questions 4a, 4b, 4c.

19 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:
-Do practice questions 5a, 5b. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

22 Summing by Column Example: To find the sum of each individual column:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition

23 Finding the Largest Element in Each Row and Each Column
Example: To find the largest element in each row: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

24 Computer Memory Computer memory is organized as a sequence of billions of memory cells. Each memory cell has an address (or location) and holds a byte-sized unit of information.

25 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.

26 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….

27 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. Demo using week10Memory.

28 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. Demo using week10Memory.

29 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 31F964. The Windows calculator (set to Programmer view) is handy for converting between decimal and hex. Demo using week10Memory.

30 Forcing Decimal Display of Addresses
In the previous program I used int() to force num1’s address to be displayed in decimal. Without the int(), the address would have been displayed in hex. Demo using week10Memory.

31 Most Variables Occupy Several Bytes
How many memory cells 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; -Do practice question 6.

32 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; -Do practice question 7.

33 Most Variables Occupy Several Bytes (cont’d.)
In an example above, we saw that num1 was stored in memory at address Since num1 is an int variable, it needs four memory cells, and actually occupies the cells at addresses , , , and -Do practice question 8.

34 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).

35 Arrays Use Contiguous Memory Addresses: Example
Demo using week10Memory1DArray.cpp. Each element’s address is the previous element’s address plus 4.

36 Computing the Address of an Element in an 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 , element 37 starts at address , because: * 37 = This is why when you pass an array to a function, what’s really passed is the array’s base address. -Do practice question 9.

37 Two-Dimensional Arrays Are Stored in Row Order
With a 2-D array, the elements are all stored in contiguous memory addresses. But they could be arranged in two different ways: either All of the row-0 elements, then all of the row-1 elements, then all of the row-2 elements, and so on; or… All of the column-0 elements, then all of the column-1 elements, then all of the column-2 elements, and so on. C++ always does it the first way.

38 Two-Dimensional Arrays Are Stored in Row Order: Example
Demo using week10Memory2DArray.cpp. -Then do practice question 10. First come the elements in row 0, then the elements in row 1.

39 Passing a 2-D Array as a Parameter to a Function
When a 2-D 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

40 Passing a 2-D Array as a Parameter to a Function: Example
Demo using week102DArrayParameter.cpp.

41 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


Download ppt "EGR 2261 Unit 10 Two-dimensional Arrays"

Similar presentations


Ads by Google