EGR 2261 Unit 10 Two-dimensional Arrays

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
Chapter VII: Arrays.
Arrays.
EGR 2261 Unit 11 Pointers and Dynamic Variables
EGR 2261 Unit 9 One-dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
EGR 2261 Unit 4 Control Structures I: Selection
Arrays, For loop While loop Do while loop
Engineering Problem Solving with C++, Etter/Ingber
7 Arrays.
EKT150 : Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Lecture 18 Arrays and Pointer Arithmetic
Introduction To Programming Information Technology , 1’st Semester
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Review of Everything Arrays
Objectives You should be able to describe: Addresses and Pointers
Arrays Week 2.
Multidimensional Arrays
7 Arrays.
Overloading functions
Programming Logic and Design Fifth Edition, Comprehensive
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
C++ Array 1.
Dr. Khizar Hayat Associate Prof. of Computer Science
Introduction to Pointers
Presentation transcript:

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

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

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

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

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

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

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

Accessing Array Elements (cont’d.) -Do practice question 1. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

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

Two-Dimensional Array Initialization During Declaration (cont’d.) Example: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

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.

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 Run it (week10TwoDInitializationmproved.cpp).

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

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

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

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

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.

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

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

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

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

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

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.

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.

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

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.

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.

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.

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.

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.

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

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 Demo using week10Memory1DArray.cpp. Each element’s address is the previous element’s address plus 4.

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 1560000, element 37 starts at address 1560148, because: 1560000 + 4 * 37 = 1560148 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.

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.

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.

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

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

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