Review of Previous Lesson

Slides:



Advertisements
Similar presentations
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
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.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Java Unit 9: Arrays Declaring and Processing Arrays.
08/09/ Arrays Defining, Declaring & Processing.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
18/12/ Arrays 2D Arrays Defining, Declaring & Processing.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Arrays Chapter 7.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Arrays.
Chapter 11 - JavaScript: Arrays
3.1 Fundamentals of algorithms
Lecture 5 of Computer Science II
Outline lecture Revise arrays Entering into an array
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
ECE Application Programming
An Example to Show Command-Line Arguments and Wrapper Class
2-D Lists Taken from notes by Dr. Neil Moore
JavaScript: Functions.
Arrays in C.
Two-Dimensional Arrays
Arrays An Array is an ordered collection of variables
Nested Loop Review and Two-Dimensional Arrays
Chapter 8 Multidimensional Arrays
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays Introducing Arrays
int [] scores = new int [10];
Coding Concepts (Data Structures)
Chapter 8 Multidimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
ARRAYS 1 GCSE COMPUTER SCIENCE.
Chapter 8 Multidimensional Arrays
Topics discussed in this section:
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
2D Arrays Defining, Declaring & Processing
Lecture 13: Two-Dimensional Arrays
Multidimensional array
Lecture 12: 2D Arrays AP Computer Science Principles
Arrays Week 2.
ARRAYS 2 GCSE COMPUTER SCIENCE.
Chapter 8 Multidimensional Arrays
Lets Play with arrays Singh Tripty
Chapter 7 Multidimensional Arrays
Review of Previous Lesson
Review of Previous Lesson
Review of Previous Lesson
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Review of Previous Lesson
Review of Previous Lesson
Arrays & Loops.
Review of Previous Lesson
Arrays & Loops.
2-D Lists Taken from notes by Dr. Neil Moore
Chapter 8 Multidimensional Arrays
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Review of Previous Lesson 24/04/2019 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from 0 - 3.

24/04/2019 Arrays 2-d

Language Features and other Testable Topics 24/04/2019 Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Arrays 2-dimensional rectangular arrays, row-major order of 2-dimensional array elements 8

Language Features and other Testable Topics 24/04/2019 Notes: 8. Students need to understand that 2-dimensional arrays are stored as arrays of arrays. For the purposes of the AP CS A Exam, students should assume that 2-dimensional arrays are rectangular (not ragged) and the elements are indexed in row-major order. For example, given the declaration int[][] m = {{1, 2, 3}, {4, 5, 6}}; m.length is 2 (the number of rows), m[0].length is 3 (the number of columns), m[r][c] represents the element at row r and column c, and m[r] represents row r (e.g., m[0] is of type int[] and references the array {1, 2, 3}). Students are expected to be able to access a row of a 2-dimensional array, assign it to a 1- dimensional array reference, pass it as a parameter, and use loops (including for-each) to traverse the rows. However, students are not expected to analyse or implement code that replaces an entire row in a 2-dimensional array, such as int[] a = {7, 8, 9}; m[0] = a; // Outside the Subset

Multidimensional Arrays 24/04/2019 Does Java have 2D arrays? array2d: If you create an array as below you can think of it as a “matrix” with 3 rows & 4 columns. int[][] array2d = { {1, 0, 12, -1}, {7, -3, 2, 5}, {-5, -2, 2, -9} }; array2d: = reference/address 1 12 -1 7 -3 2 5 -5 -2 2 -9 But in reality, array2d holds an reference to an array of 3 items, where each item is a reference to an array of 4 ints. Note: The declaration & initialisation above can be written on one line, it is written on multiple lines to make it easier to read.

Row-major order e.g. array2dName [ row ][ col ] = …. 24/04/2019 Row-major order a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 array2dName [ row ][ col ] = …. e.g. array2d [0][1] is 0 array2d [2][3] is -9 ? ?

More Complex Way to create/instantiate a 2d array: 24/04/2019 More Complex Way to create/instantiate a 2d array: The previous simple declaration, creation & initialisation and the more complex way to create/instantiate a 2d array have the same issues as described in the previous presentation on 1d arrays. e.g. int[][] array2d = new int [3][4]; 2 nested loops can then be used to populate the 2d array. Note that this can also be done on 2 separate lines: int[][] array2d; ….. array2d = new int [3][4];

Size of a 2d array … .length = the number of rows 24/04/2019 Size of a 2d array … .length = the number of rows e.g. array2d.length = 3 … [0].length = the number of columns in row 0 Note: 2d arrays can be ragged (different number of columns in each row), so this may not give the same number in different rows. However, the AP Computer Science syllabus states “For the purposes of the AP CS A Exam, students should assume that 2-dimensional arrays are rectangular (not ragged) …”. e.g. array2d[0].length = 4

Accessing rows of a 2d array 24/04/2019 Accessing rows of a 2d array e.g. array2d[0] is of type int[] and references the array {1, 0, 12, -1} It is also possible to replace an entire row in a 2d array but the AP Computer Science syllabus states that this will not be expected in the exam: e.g. array2d: 7 8 9 10 -3 2 5 -5 -2 -9 int[][] array2d = { {1, 0, 12, -1}, {7, -3, 2, 5}, {-5, -2, 2, -9} }; int[] array1d = {7, 8, 9, 10}; array2d[0] = array1d; // Outside the Subset

Nested for loop to iterate a non-ragged 2D array: 24/04/2019 for (int row = 0; row < arr2D.length; row++) { // assuming arr2D is not ragged (different number of columns in each row) for (int col = 0; row < arr2D[0].length; col++) { System.out.print(arr2D[row, col]); } System.out.println(); // If you already ‘know’ the number of rows & columns: for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++) { // or use an int literal e.g. 8 for (int colIndex = 0; colIndex < cols - 1; colIndex++) { // or use an int literal e.g. 8 System.out.println(arr2D[rowIndex, colIndex]); ?

Nested For Each loop to iterate a 2D array: 24/04/2019 Nested For Each loop to iterate a 2D array: ? for (int[] arr1D : arr2D) { //Basically pulls out each row. /*Basically now a 1D array (as previously), but in reality, each row of the 2D array above.*/ for (int i : arr1D) { System.out.print(i); } System.out.println();

24/04/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).

Store BIKE IDs 24/04/2019 Super Bikes owns a rectangular parking area with 30 rows; each row has 4 bike spaces. Each bike is always parked in the same space. The array bikeSpace[30,4] stores the bike registrations. Soni uses a flowchart to help him design a module to populate the array with the bike registrations. Input is terminated using the rogue value “BK000”. Write this program and allow the user to see the contents of the array.

Chess Board 1 Liliane wants to write a program to play chess. 24/04/2019 Chess Board 1 Liliane wants to write a program to play chess. She will represent the board of 4 x 4 squares, using a 2-dimensional array. If a chess piece is on a square, it will take a value of 1. Write a program to accept row and column numbers and place a 1 at this position and re-display the board. To display the initially empty board:

24/04/2019 Chess Board 2 Produce a different version of the previous “Chess Board 1” program. Liliane's next task is to indicate that there are pieces occupying the first two rows of the 4 x 4 board. Each square in rows 1 and 2 will be given the value 1. Add initial code that occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows. This is all this version needs to do.

24/04/2019 Chess Board 3 Produce a new version of the previous “Chess Board 2” program that uses While Loops (as you probably used for loops originally). Occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows but with While Loops

Tile Designs 24/04/2019 Ahmed combines white tiles with tiles of one other colour to make a pattern. He draws a design. Here is one example: Ali stores the design in a 2-dimensional array, floorDesign. The length and width of the room will be no more than 9 tiles each. Write a program for Ahmed which: Initially, makes every tile white. Asks the user to enter the size of a design. e.g. 2 tiles by 3 tiles Asks the user to enter the design e.g. Note that with this method the user will enter each row of the design and be expected to know to press the Enter key after each row. If you can think of a better way please do so. Then calculates the number of white tiles and the number of coloured tiles in the design.

4/24/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.