© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to 2-D Arrays.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
Arrays.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Multiple-Subscripted Array
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Chapter 8 Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional 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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Lesson 12 Arrays Objectives: About the Fibonacci sequence One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study:
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.
 2007 Pearson Education, Inc. All rights reserved C 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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
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’,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Engineering Computing I Chapter 5 Pointers and Arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
Windows Programming Lecture 03. Pointers and Arrays.
Dr Tripty Singh Arrays.
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to 2-D Arrays

© Janice Regan, CMPT 102, Sept Matrices  A matrix or two-dimensional array is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement.  type array_name[num_rows][num_columns]  type array_name[length_column][length_row]  int matrix[3][4];  double mice[7][9];  char courselist[4][32];

© Janice Regan, CMPT 102, Sept Matrices  int matrix[3][4]; row[0] row[1] row[2] in memory row0row1row2 matrix[0][0]matrix[0][1]matrix[0][2] matrix[0][3] row0

© Janice Regan, CMPT 102, Sept Matrices  int matrix[3][4]; in memory Row[0][0] Row[1][0] Row[2][0] Row[0][1] Row[1][1] Row[2][1] Row[0][2] Row[1][2] Row[2][2] Row[0][3] Row[1][3] Row[2][3] Row[1][0] Row[2][0] Row[1][1] Row[2][1] Row[0][2] Row[1][2] Row[2][2] Row[0][3]Row[1][3] Row[2][3] Row[0][1] Row[0][0]

© Janice Regan, CMPT 102, Sept Initializing 2-D arrays  double myarray [3][5] = { { 1.0, 2.3, 3.5, 4.2,5.1}, { 0.1, 1.2, 2.3, 3.4, 4.2}, { 9.9, 8.8, 7.7, 6.6, 5.5}}; int yourarray[2][3] = { 1,2,3,4,5,6};  Allocates enough space for a 2-D array myarray with 3 rows and 5 columns.  Allocate enough space for a 2-D array yourarray with 2 rows and 3 columns  Think of myarray an array of 3 arrays of length 5

© Janice Regan, CMPT 102, Sept Initializing 2-D arrays int myarray [5][4] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[6] [2] = { 1,4, 9,7, 4,1, 0,0 };  Allocates enough space for myarray a 2-D array with 5 rows and 4 columns, and for thisarray a 2-D array with 6 rows and 2 columns  The initial values given for myarray will fill the first two rows of myarray, the remainder of the array will be filled with zeros  The intial values given for thisarray will fill the first four rows of thisarray, the remainder of the array will be filled with zeros

© Janice Regan, CMPT 102, Sept Initializing 2-D arrays int myarray [ ][4] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[ ] [2] = { 1,4, 9,7, 4,1, 0,0 };  Allocates enough space for myarray a 2-D array with 2 rows and 4 columns, and for thisarray a 2-D array with 4 rows and 2 columns  Think of myarray as an array of 2 arrays of length 4, and thisarray as an array of 4 arrays of length 2  When the number of rows is not given in the declaration, the number of rows is determined by the number of elements initialized in the declaration

© Janice Regan, CMPT 102, Sept INVALID: Initializing 2-D arrays int myarray [ ][ ] = { { 1, 3, 5, 4 }, { 7, 4, 3, 0} }; int thisarray[ 3][ ] = { {1,4}, {9,7}, {4,1}, {0,0} };  Think of the 2-D array as an array of 1-D arrays (rows)  Must know the length of each 1-D array (row) to determine where the next 1-D array (row) begins  The second pair of square brackets contains the number of elements in a row  Thus, the 2 nd pair of square brackets cannot be left empty

© Janice Regan, CMPT 102, Sept Initializing 2-DArrays for(i=0; i<NROWS; i++) { for(k=0; k<NCOLS; k++) { myarrayt[ i ][ k ] = 10; }  For efficient initialization or evaluation be sure that elements are initialized or evaluated in the order they occur in memory. Along row 0 then along row1 and so on  The loop over each row is the inner loop, the loop over each column is the outer loop.

© Janice Regan, CMPT 102, Sept Accessing Array Elements  int matrix[3][4];  matrix has 12 integer elements  matrix[0][0]element in first row, first column  matrix[2][3]element in last row, last column  matrix is the address of the first element  &matrix[0][0] is the address of the first element  matrix[1] is the address of the second row

© Janice Regan, CMPT 102, Sept Choosing array sizes: parameters  When calling a function with a 2-D array as an argument the choice of a variable should be compatible with the argument  If the call is of the form double funct1 ( myarray[NCOLS][NROWS] ); double arrayInMain[NCOLS][NROWS]; answer = funct1(arrayInMain);  pass in an array as an argument that has the same maximum row length (NCOLS) as the parameter of the function  Pass in an array as an argument that has the same maximum column length (NROWS)

© Janice Regan, CMPT 102, Sept DArrays as Function Parameters void addconst(int b[NROWS][NCOLS], int bt[NROWS][NCOLS], int usedRows, int usedCols, int const) { /* Declare Variables. */ int i, k; for(i=0; i<usedRows; i++) { for(k=0; k<usedCols; j++) { bt[ i ][ k ] += 1; } return; }

© Janice Regan, CMPT 102, Sept Choosing array sizes: parameters  When calling a function with a 2-D array as an argument the choice of a variable should be compatible with the argument  If the call is of the form double funct1 ( myarray[ ][NCOLS] ); double arrayInMain[NROWS][NCOLS]; answer = funct1(arrayInMain);  pass in an array as an argument that has the same maximum row length (NCOLS) as the parameter of the function  The argument can have any maximum column length (NROWS) and the function will still work properly

© Janice Regan, CMPT 102, Sept DArrays as Function Parameters void addconst(int b[ ][NCOLS], int bt[ ][NCOLS], int usedRows, int const) { /* Declare Variables. */ int i, k; for(i=0; i<UsedRows; i++) { for(k=0; k<usedCols; j++) { bt[ i ][ k ] += 1; } return; }