CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.

Slides:



Advertisements
Similar presentations
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
Advertisements

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 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.
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 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Arrays.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation.
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.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Module 1: Array ITEI222 - Advance Programming Language.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Objectives You should be able to describe: One-Dimensional Arrays
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Introduction to Programming
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
The Ohio State University
Lecture 10 Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Multidimensional Arrays
7 Arrays.
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Presentation transcript:

CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays

CSE202: Lecture 16The Ohio State University2 Two-dimensional Arrays A two-dimensional array consists of both rows and columns of elements. It is essentially a matrix. To declare a two-dimensional array, we merely use two sets of square brackets. –The first contains the number of rows –The second contains the number of columns //Creates a 2D array with 3 rows and 4 columns int vals[3][4];

CSE202: Lecture 16The Ohio State University3 Indices in 2D arrays Assume that the two dimensional array called val is declared and looks like the following: To access the cell containing 6, we reference val[1][3], that is, row 1, column 3. val Col 0Col 1Col 2Col 3 Row Row Row

CSE202: Lecture 16The Ohio State University4 Using 2D arrays Just like 1D arrays, once you have specified the index, you are just working with a single variable of the given data type. Assignments and usage is still the same: sumRow0 = val[0][0] + val[0][1] + val[0][2] + val[0][3]; //assigns 72 to cell at row 2, column 3 val[2][3] = 72;

CSE202: Lecture 16The Ohio State University5 Initializing 2D arrays You can use additional braces to indicate when rows start and end, but you don’t have to do that. int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; Or int val[3][4] = {8,16,9,52, 3,15,27,6, 14,25,2,10}; Or (correct, but not as clear as the first two): int val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};

CSE202: Lecture 16The Ohio State University6 More on 2D arrays Initialization of 2D arrays is done in row order. 2D arrays work well with (for) loops like 1D arrays. However, to access all elements, typically you will need nested loops for 2D arrays. Can you see why?

CSE202: Lecture 16The Ohio State University7 Example Program... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }...

CSE202: Lecture 16The Ohio State University8 Example Program... int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }... > array2DExample.exe

CSE202: Lecture 16The Ohio State University9 Example Program (2)... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the transpose of the array for (int col = 0; col < NUM_COL; col++) { for (int row = 0; row < NUM_ROW; row++) { cout << vals[row][col] << " "; } cout << endl; }...

CSE202: Lecture 16The Ohio State University10 Matrix Addition Algorithm Add two n x m matrices A, B; for each row i of A do –for each column j of A do °C[i][j] = A[i][j] + B[i][j]; Output matrices A, B and C.

CSE202: Lecture 16The Ohio State University11 matrixAdd.cpp... int main() { const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; // C = A + B for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { C[i][j] = A[i][j] + B[i][j]; }...

CSE202: Lecture 16The Ohio State University12 matrixAdd.cpp (2)... // display A cout << "A =" << endl; for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { cout << " " << A[i][j] << " "; } cout << endl; } cout << endl;...

CSE202: Lecture 16The Ohio State University13 Distance Matrix Algorithm Compute a matrix D storing distances to a point (x,y); Input: x, y for each row i of D do –for each column j of D do °D[i][j] = distance from (x,y) to (i,j) Output matrix D.

CSE202: Lecture 16The Ohio State University14 distanceMatrix.cpp... // dimensions of distance matrix const int N_ROWS(8); const int N_COLS(12); double A[N_ROWS][N_COLS]; double x(0.0), y(0.0); cout << "Enter (x,y) coordinates of center: "; cin >> x >> y; for (int i=0; i<N_ROWS; i++) { for (int j=0; j<N_COLS; j++) { double dx = j-x; // Note: x represents columns double dy = i-y; // Note: y represents rows double dist = sqrt(dx*dx+dy*dy); A[i][j] = dist; }...

CSE202: Lecture 16The Ohio State University15 X Matrix Algorithm Compute a character matrix A representing an X; Input: h (height of the X) Initialize the first h columns in the first h rows of A to ‘-’; Set all elements of A on the diagonal from A[0][0] to A[h-1][h-1] to ‘X’; Set all elements of A on the diagonal from A[0][h-1] to A[h-1][0] to ‘X’.

CSE202: Lecture 16The Ohio State University16 X.cpp... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; }...

CSE202: Lecture 16The Ohio State University17 X.cpp (2) // initialize elements of A to '-' for (int i=0; i<height; i++) { for (int j=0; j<height; j++) { A[i][j] = '-'; } // draw diagonal \ in A for (int k=0; k<height; k++) { A[k][k] = 'X'; } // draw diagonal / in A for (int k=0; k<height; k++) { A[k][height-k-1] = 'X'; }

CSE202: Lecture 16The Ohio State University18 X.cpp... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0.0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; }...

CSE202: Lecture 16The Ohio State University19 Two Dimensional Arrays as Parameters

CSE202: Lecture 16The Ohio State University20 Matrix Addition Algorithm Add two n x m matrices A, B; for each row i of A do –for each column j of A do °C[i][j] = A[i][j] + B[i][j]; Output matrices A, B and C.

CSE202: Lecture 16The Ohio State University21 matrixAdd2.cpp... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; }...

CSE202: Lecture 16The Ohio State University22 matrixAdd2.cpp... // GLOBAL CONSTANTS const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Function prototypes void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]); void display(const double M[NUM_ROWS][NUM_COLS]); int main() { // Note: A, B and C have the same dimensions // Matrices double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS];...

CSE202: Lecture 16The Ohio State University23 Function add() // Add two matrices // M1[][] = First matrix // M2[][] = Second matrix // M3[][] = M1[][] + M2[][] // Note: M1, M2 and M3 must have the same dimensions void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]) { // M3 = M1 + M2 for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { M3[i][j] = M1[i][j] + M2[i][j]; }

CSE202: Lecture 16The Ohio State University24 Function display() // Output matrix // M[][] = Matrix void display(const double M[NUM_ROWS][NUM_COLS]) { for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j < NUM_COLS; j++) { cout << " " << setw(3) << M[i][j]; } cout << endl; } cout << endl; }

CSE202: Lecture 16The Ohio State University25 matrixAdd2.cpp... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; }...

CSE202: Lecture 16The Ohio State University26 Passing 2D Arrays into Functions 2D arrays are always passed by reference. To declare a 2D array parameter, –Define global constants NUM_ROWS and NUM_COLS; –Parameter is: type array2D[NUM_ROWS][NUM_COLS] For instance, void f(int array2D[NUM_ROWS][NUM_COLS]) No way to declare a variable length 2D array parameter. For instance, void f(int array2D[][]) is illegal and will generate a syntax error.

CSE202: Lecture 16The Ohio State University27 Multi-Dimensional Arrays Arrays can have higher dimensions. Definitions, intialization, indexing, function parameters are similar to 2D arrays. Note multidimensional arrays can have quite a few elements so you must be mindful of your memory capacity: int big[1000][1000][1000]; //a billion ints

CSE202: Lecture 16The Ohio State University28 Common Programming Errors Addressing indices that are out of bounds of the array range. This will run-time crash or at the very least a logical error. Be careful especially when using expressions to compute the indices. –Remember, indexing starts with 0, not 1!!! Forgetting to declare the array (either altogether or forgetting the [] ) Assuming the array is initialized (to zero.)