Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
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.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
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.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
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.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
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’,
Arrays.
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 Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
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.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
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.
Two-Dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Multi-dimensional Array
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays Outline Introduction Arrays Declaring Arrays
Multidimensional array
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Presentation transcript:

Topic 9C – Multiple Dimension Arrays

CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions. Two dimensional arrays are the simplest type of multiple dimension arrays. They can be used to represent tables of data, matrices, and other two dimensional objects. One of the most obvious example of a two dimensional array is the tic-tac-toe board.

CISC105 – Topic 9C Multiple Dimension Arrays Example: Tic-Tac-Toe A tic-tac-toe board has two dimensions, the row and the column. Each dimension is composed of 3 possible indices (there are 3 rows and 3 columns) [0][0][0][1][0][2] [1][0] [2][0] [1][1][1][2] [2][1][2][2]

CISC105 – Topic 9C Two Dimensional Arrays We can see how a two dimensional array looks like a table or matrix. Thus, we can represent a two dimensional array as rows and columns. To declare a two-dimensional array: (data type) array_name[# of rows][# of columns];

CISC105 – Topic 9C Two Dimensional Arrays Declarations Therefore, a two dimensional array of integers, named x, with 29 rows and 33 columns would be declared as: int x[29][33]; A two dimensional array of characters, named c, with 119 rows and 2 columns would be declared as: char c[119][2];

CISC105 – Topic 9C Multiple Dimension Array Declarations We can also declare arrays of more than two dimensions. A six dimensional array of double s could be declared as: double a[2][3][9][4][5][3]; A three dimensional array of integers could be declared as: int x[4][5][4];

CISC105 – Topic 9C Referencing Multiple Dimension Arrays To access an element of a multiple dimension array, we use the same form as for one dimensional arrays, with the extra dimension(s) also present, such as: x[2][7] = x[1][7] + 27; y[9][2][0][4] = 9; printf(“%d”,p[3][4][5][9][0]);

CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments We have seen that when one dimensional arrays are passed into a function, the size of the array is not passed in. This allows us to pass arrays of any size into the function. When passing a multiple dimension array as a function parameter, the size MUST BE specified in the function prototype. Thus, we can only pass in fixed-size multiple dimensional arrays into functions.

CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments However, there is one exception to this rule. The FIRST size (in a two dimensional array, the number of rows) MAY be omitted in a function prototype. Therefore, if we wanted to create a function that takes one parameter, a tic-tac-toe board, we could write the prototype as either: void function_x(char board[][3]); void function_x(char board[3][3]); OR

CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments If the size of a multiple dimension array is known and fixed, such as the size of a tic-tac-toe board, the size of all dimensions should be specified. If this is not fixed, or known, the first dimension can, and should, be omitted.

CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments Thus, the following function prototypes are valid: void function_a(char[][4]); int function_b(int[4][9]); int function_c(char p[3][9][2]); int function_d(int[][9][2][4]); int function_e(int[4][9][2][4]); int function_f(char[][3][9]);

CISC105 – Topic 9C Multiple Dimension Arrays as Function Arguments In contrast, the following function prototypes are NOT valid: void function_a(char[][]); int function_b(int[4][]); int function_c(char p[][][2]); int function_d(int[][9][][]); int function_e(int[4][][4][]); int function_f(char[9][][9]);

CISC105 – Topic 9C Initialization of Multiple Dimension Arrays Multiple dimension arrays can be initialized in much the same way as one dimensional arrays. char board[3][3] = { {‘X’, ‘X’, ‘O’}, {‘O’, ‘X’, ‘O’}, {‘X’, ‘O’, ‘X’} }; Notice how the initialization list is grouped into rows.

CISC105 – Topic 9C Initialization of Multiple Dimension Arrays In addition to grouping the initialization list into rows, the initialization list can simply be provided as a straight list: char board[3][3] = {‘X’,‘X’,‘O’,‘O’,‘X’, ‘O’,‘X’,‘O’,‘X’}; This type of initialization list fills in the first row, left-to-right, then the second row, left-to-right, then the third row, etc…

CISC105 – Topic 9C Initialization of Multiple Dimension Arrays Thus, this declaration will set board[0][0] to ‘X’, board[0][1] to ‘X’, board[0][2] to ‘O’, board[1][0] to ‘O’, board[1][1] to ‘X’, board[1][2] to ‘O’, board[2][0] to ‘X’, board[2][1] to ‘O’, board[2][2] to ‘X’. char board[3][3] = {‘X’,‘X’,‘O’,‘O’,‘X’, ‘O’,‘X’,‘O’,‘X’};

CISC105 – Topic 9C Summary Multiple dimension arrays are very similar to one dimensional arrays, in declarations, initialization, and referencing. When passing a multiple dimension array into a function, the size of the dimensions MUST be specified (in contrast to one dimensional arrays), with the exception of the size of the first array dimension.

CISC105 – Topic 9C An Example Problem #1 Write a prototype for a function named function1 that takes two parameters, a three dimensional integer array with sizes 4, 9, & 10, and a four dimensional integer array with sizes 9, 4, 2, & 2. The first array is for input and the function should not be able to modify it. The second array is for input and output; the function can modify it. The function returns a float data type. float function1( const int array1[4][9][10], int array2[9][4][2][2] );

CISC105 – Topic 9C An Example Problem #2 Write a function that takes one parameter, a tic-tac-toe board. It should then ask the user which box they want to put a ‘X’ in, by specifying the row and column. The function should then set that specified box equal to ‘X’. The prototype for this function is given: void set_an_X(char board[3][3]);