Two-Dimensional Arrays

Slides:



Advertisements
Similar presentations
Spreadsheet Vocabulary
Advertisements

Lecture 20 – Boolean Operators Dr. Patricia J. Riddle.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
1 The Game of Life Supplement 2. 2 Background The Game of Life was devised by the British mathematician John Horton Conway in More sophisticated.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
A First Book of ANSI C Fourth Edition
Vision & Recognition. From a different direction At different times, particularly if it has been modified in the interval In different light A particular.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
Do I Understand Spreadsheets? Assessment Game. Question 1 What is a spreadsheet?
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
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.
Chapter 11 - JavaScript: Arrays
Introduction Abstract
10.4 The Algebra of Matrices
Manipulating MATLAB Matrices Chapter 4
EGR 2261 Unit 10 Two-dimensional Arrays
Arrays.
Two-Dimensional Arrays
Two-Dimensional Arrays
1.5 Matricies.
Simplifying 1 + 2a + b + 1 a + b a + b + 1 2a b + a + b
Introduction to Matrices
A simple way to organize data
Multi-dimensional Array
Week 9 - Monday CS 121.
Statistics for the Social Sciences
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
עקרנות תכנות מונחה עצמים
Two Dimensional Arrays
Board: Objects, Arrays and Pedagogy
Nested Loop Review and Two-Dimensional Arrays
How do you store a bunch of similar stuff?
Topic 26 Two Dimensional Arrays
Objective: Today we will investigate the ‘magic’ in magic squares.
Simplifying 1 + 2a + b + 1 a + b a + b + 1 2a b + a + b
Multidimensional Arrays
MATRICES MATRIX OPERATIONS.
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
2.2 Introduction to Matrices
More 2 D Array.
Magic Squares   10   X.
Data Tables and Arrays.
2D Array and Matrix.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
Computer Architecture and Assembly Language
How do you store a bunch of similar stuff?
CS Software Studio Assignment 1
Arrays of Two-Dimensions
INC 161 , CPE 100 Computer Programming
Functions continued.
An example of how to represent a problem
Addo How to play.
Matrix Operations Chapter 4, Sections 1, 2, 3.
Continue with Life, Magic and Catch -up
© T Madas.
Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei
The Determinant of a Matrix A is denoted by
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
How do you store a bunch of similar stuff?
Computer Architecture and Assembly Language
Computer Science I: Get out your notes.
MATRICES MATRIX OPERATIONS.
Matrices.
AP Java Learning Objectives
Presentation transcript:

Two-Dimensional Arrays

Learning Objectives Introduce two-dimensional arrays Be able to declare a two-dimensional array Be able to analyze the values in each row of a 2D array Understand the implications of an array of arrays. Describe the use of two-dimensional arrays to represent grids of information

Two-Dimensional Arrays Declaration similar to one dimensional arrays Need to specify both the number of rows and columns during allocation Example: Type TwoDArrayType = array[1..3, 1..3] of integer; Var twoDArray:TwoDArrayType;

Program ticytacy; type boardType = array[1. 3, 1 Program ticytacy; type boardType = array[1..3, 1..3] of char; //Char is a single character Var board:boardType; row, col:integer; begin for row := 1 to 3 do for col:= 1 to 3 do board[row,col] := '?'; board[row, row]:='X'; board[col, 4-col] := 'O'; write(board[row,col]); writeln; end; readln; end.

Some 2-D Array Math Calculating the sum for the first row for col:= 1 to 3 do sum:= sum + board[1, col]; Calculating the sum for the first column for row:= 1 to 3 do sum:= sum + board[row, 1]; Calculating the sum of all values sum:= sum + board[row, col];

More 2-D array math Find the sum of the cells that surround the cell board[r, c]. for row:= r-1 to r+1 do for col:= c-1 to c+1 do sum:= sum + board[row, col]; sum:= sum – board[r, c];

Adding Another Dimension You can create arrays of higher dimension than 2. For example, if we were measuring the temperature in a rectangular volume. Type temperatureArrayType = array[1..3, 1..3, 1..3] of integer; var temperatures:temperatureArrayType;

Program: Life The universe is a two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Patterns - Any live cell with fewer than two live neighbors dies, as if caused by under population. - Any live cell with two or three live neighbors lives on to the next generation. - Any live cell with more than three live neighbors dies, as if by overpopulation. - Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Your application of Life Hint: include a boundary around your world. You determine the size of the universe Fill your universe: Examples Each cell has a 1 in N chance of being alive for the first generation. You select the value for N or have the user enter the value for N. Look up potential life patterns. Run the game for several generations. Show the universe after each generation Push: Break the program into procedures. neighbors(), showUniverse(), populate() Push: Determine a way to use records in your solution. Push: Have your universe ‘wrap’

Magic Square A magic square[1] is a n by n square grid (where n is the number of cells on each side) filled with distinct positive integers in the range {1,2,...,n2} such that each cell contains a different integer and the sum of the integers in each row, column and diagonal is equal

Magic Square Program Write a program that will take as input the values for each cell of a 3x3 potential magic square and determine if the square is magic. Push: Write a program to generate 3x3 magic squares. Using an algorithm rather than just hard coding a found solution. Push: Modify this to handle n x n magic squares.