Two-Dimension Arrays Computer Programming 2.

Slides:



Advertisements
Similar presentations
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Advertisements

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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
C++ for Engineers and Scientists Third Edition
Building Java Programs Chapter 7.5
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
A First Book of ANSI C Fourth Edition
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
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.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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’,
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
C# Arrays.
Computer Programming BCT 1113
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
Today’s Material Arrays Definition Declaration Initialization
Array Array Array Dimension 3. One dinensional array
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Matrix Operations SpringSemester 2017.
Counted Loops.
Arrays, For loop While loop Do while loop
Arrays An Array is an ordered collection of variables
Yong Choi School of Business CSU, Bakersfield
Nested Loop Review and Two-Dimensional Arrays
Can store many of the same kind of data together
EKT150 : Computer Programming
Can store many of the same kind of data together
Multidimensional Arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
Module8 Multi-dimensional Array
Multidimensional array
Multidimensional Arrays
2D Arrays Just another data structure Typically use a nested loop
CIS16 Application Development and Programming using Visual Basic.net
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Matrix Operations SpringSemester 2017.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
C++ Array 1.
Arrays and Matrices Prof. Abdul Hameed.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Two-Dimension Arrays Computer Programming 2

Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Essential Indicator 3.01 Apply procedures to construct two-dimensional arrays. (6%)

Two-Dimensional Arrays Two-Dimensional Array = Rectangular Array A two-dimensional array contains rows and columns. This type of array requires two indices, one for the row and one for the column

Two-Dimensional Arrays Declaring a two-dimensional array dataType [ , ] name = new dataType [r, c]; where r = the number of rows and c = the number of columns. Example int [ , ] intArray = new int[3, 3]; This would declare a 3 by 3 array. 1 2 1 2

Declaring & Initializing a Two-Dimensional Array Example 1 Declare with an initializer list int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = new string[2, 2] { {"Mike","Anna"}, {"Mary",“John"} }; Example 2 You can omit the size of the array with an initializer list. int[,] numbers = new int[ , ] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = new string[ , ] { {"Mike","Anna"}, {"Mary",“John"} }; Example 3 You can omit the size of the array and the new operator if you provide the initializer list. int[,] numbers = int[ , ] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = string[ , ] { {"Mike","Anna"}, {"Mary",“John"} };

Adding Values Individually You can add values into your array as follows. intArray[r, c] = value; Example: intArray[1, 1] = 3; 1 2 3 1 2

Accessing Values in a Two-Dimensional Array Nested For Loops give you more control. Use GetLength() to return how many elements are in that “Dimension”. O is Dimension 1 (rows) 1 is Dimension 2 (columns) int[,] intArr= new int[ , ] {{9, 99}, {3, 33}, {5, 55}}; for (int r = 0; r < intArr.GetLength(0); r++) { for (int c = 0; c < intArr.GetLength(1); c++) { Statements; } }

Creating a Grid In XNA you can create a grid of rectangles that you can use to mimic a gameboard. Declaring the two-dimensional array of rectangles. Rectangle [ , ] grid = new Rectangle[2, 2];

Load the Rectangles into the Grid for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) grid[i, j] = new Rectangle(x, y, 50, 50); x += 50; //change the x location } y += 50; //move y location down x = 0; //reset the x

Nested For Loops The Nested For Loop can be used in: The Initialize method to load your images picArray[i, j] = Content.Load<Texture2D>(“image"); The Draw method to “draw” your images SpriteBatch.Draw(picArray[i,j], grid[i,j], Color.White);

Check Your Understanding Declare an array called nums that will hold 5 integers. int[] nums = new int[5]; What are the index numbers of the num array? 0, 1, 2, 3, 4 Given the nums array just declared, how would you write the for loop to traverse it? for (int i = 0; i < 5; i++)

Check Your Understanding Declare the table array represented below. int [,] table = new int {{8,7}, {6,5}, {4,3}}; Given the table array, what would be the result of the following statement? Table[3,2] = 9; Code won’t compiler because the index is out of bounds. 8 7 6 5 4 3

Check Your Understanding Write the for loop to iterate through the 2D array called matrix. for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) What code would be inside the above nested loop if you wanted to create a running total? sum += matrix[i, j];

More Information For more information about the Array class: http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx