multi-dimensional arrays

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

11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle.
Building Java Programs Chapter 7.5
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
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.
Nested LOOPS.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
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’,
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
ARRAYS.
Decision making If.. else statement.
EGR 2261 Unit 10 Two-dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
Programming application CC213
Arrays 1. One dimensional arrays - Review 2. Using arrays
Microsoft Visual Basic 2005: Reloaded Second Edition
Two Dimensional Arrays
Two-dimensional arrays
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
EKT120 : Computer Programming
MULTI-DIMENSIONAL ARRAY
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
2-D Lists Taken from notes by Dr. Neil Moore
Multi-dimensional Array
Chapter 5: Arrays: Lists and Tables
Arrays Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement!
ㅎㅎ Fourth step for Learning C++ Programming Two functions
One-Dimensional Array Introduction Lesson xx
Nested Loop Review and Two-Dimensional Arrays
Functions.
EKT150 : Computer Programming
EKT120: Computer Programming
Multidimensional Arrays
Functions with arrays.
Announcements & review
Decision making If statement.
Chapter 8: Arrays Problem Solving and Program Design in C 5th Edition
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Calendar like the Periodic Table
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
CIS16 Application Development and Programming using Visual Basic.net
Multi-Dimensional Arrays
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
Functions Extra Examples.
Beginning C Lecture 5 Lecturer: Dr. Zhao Qinpei
Two dimensional arrays.
Multidimensional Arrays Section 6.4
C++ Array 1.
EECE.2160 ECE Application Programming
2-D Lists Taken from notes by Dr. Neil Moore
EECE.2160 ECE Application Programming
Dr. Khizar Hayat Associate Prof. of Computer Science
Two dimensional arrays.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Presentation transcript:

multi-dimensional arrays

Two-dimensional arrays are used to represent a table of data. 1. declaration Two-dimensional arrays are used to represent a table of data. The following statement declares a tic-tac-toe board with 3 rows and 3 columns: char tictac[3][3]; The total number of elements in such array is 3 x 3 = 9 elements. Col 0 Col 1 Col 2 Row 0 X O Row 1 Row 2 Dr. Soha S. Zaghloul 2

The table below shows how to refer to each of the elements of tic-tac: 2. Referencing The table below shows how to refer to each of the elements of tic-tac: In a two-dimensional array, each element is referenced by two subscripts: the number of the row the number of the column The number of the row is used first. For example, tictac [2][0] refers to the element in row 2, and column 0. This is shown in the figure. Compare this with the element tic-tac[0][2] Col 0 Col 1 Col 2 Row 0 0,0 0,1 0,2 Row 1 1,0 1,1 1,2 Row 2 2,0 2,1 2,2 Dr. Soha S. Zaghloul 3

Values are grouped by rows. For example: 3. initialization Values are grouped by rows. For example: char tic-tac[3][3] = { {‘ ‘, ‘ ‘, ‘ ‘}, {‘ ‘, ‘ ‘, ‘ ‘}, {‘ ‘, ‘ ‘, ‘ ‘} }; The blue braces represent the first row, the green ones correspond to the second, and the red one represent the third row. Two-dimensional arrays may also be initialized using a nested loop as follows: char tictac[3][3]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 3; col++) tictac[row][col] = ‘ ‘; Dr. Soha S. Zaghloul 4

The following code displays the contents of the array tictac: 4. Displaying an array The following code displays the contents of the array tictac: char tictac[3][3]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 3; col++) printf (“ tictac[%d][%d] = %c”, row, col, tictac[row][col]); Dr. Soha S. Zaghloul 5

5. Arrays with several dimensions In the C language, we may have several dimensions of the array. Declaration is performed in the same way as two- dimensional. However, we’ll have a third number in the declaration. Example: int enroll[100][5][4]; Initialization will be performed in a nested loop of three dimensions: int enroll[100][5][4]; int i, j, k; for (i=0; i<100; i++) for (j=0; j<5; j++) for (k=0; k< 4; k++) enroll[i][j][k] = 0; Dr. Soha S. Zaghloul 6

6. Arrays with several dimensions - example An example of a 3-dimensional array may be that representing a calendar: #define month 12 #define week 5 #define day 7 char year[month][week][day]; Therefore, the element year[10][3][5] represents month 11 (November), the fourth week in the month, and the sixth day in the week (Friday if we assumed Sun is the first day of the week). Dr. Soha S. Zaghloul 7