21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Chapter Matrices Matrix Arithmetic
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
ECON 1150 Matrix Operations Special Matrices
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Arrays- Part 2 Spring 2013Programming and Data Structure1.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Section 3.5 Revised ©2012 |
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Sec 4.1 Matrices.
Arrays and Pointers.
Assembly - Arrays תרגול 7 מערכים.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
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.
Windows Programming Lecture 03. Pointers and Arrays.
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.
12-1 Organizing Data Using Matrices
Dynamic Allocation Review Structure and list processing
Java Array Object Chuen-Liang Chen Department of Computer Science
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.
Today’s Material Arrays Definition Declaration Initialization
Matrix Operations.
C Passing arrays to a Function
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
CS111 Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Multidimensional Arrays
prepared by Senem Kumova Metin modified by İlker Korkmaz
Section 2.4 Matrices.
Multidimensional Arrays
Dr Tripty Singh 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.
Arrays, Pointers, and Strings
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur2 Multidimensional Arrays double a[100]; int b[4][6]; char c[5][4][9]; A k-dimensional array has a size for each dimensions. Let s i be the size of the ith dimension. If array elements are of type T and v=sizeof(T), the array declaration will allocate space for s 1 *s 2 *...*s k elements which is s 1 *s 2 *...*s k *v bytes.

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur3 2-dimensional Arrays zIt is convenient to think of a 2-d array as a rectangular collection of elements. zint a[3][5] a[0][0]a[0][1]a[0][2]a[0][3]a[0][4]row0 a[1][0]a[1][1]a[1][2]a[1][3]a[1][4]row1 a[2][0]a[2][1]a[2][2]a[2][3]a[2][4]row2 a[3][0]a[3][1]a[3][2]a[3][3]a[3][4]row3 col0col1col2col3col4

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur4 Pointers and multi-d arrays zThere are numerous ways to access elements of a 2-d array. za[i][j] is equivalent to: y*(a[i]+j) y(*(a+i)[j]) y*((*(a+i))+j) y*(&a[0][0] + 5*i + j)

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur5 Pointers and multi-d arrays zWe can think of a[i] as the ith row of a. zWe can think of a[i][j] as the element in the ith row, jth column. zThe array name, a (&a[0]) is a pointer to an array of 5 integers. zThe base address of the array is &a[0][0]. zStarting at the base address the compiler allocates contiguous space for 15 ints.

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur6 The storage mapping function z(The mapping between pointer values and array indices.) zint a[M][N]; yThe storage mapping function : a[i][j] is equivalent to *(&a[0][0] + N*i + j)

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur7 Formal parameter declarations zWhen a multi-dimensional array is a formal parameter in a function definition, all sizes except the first must be specified so that the compiler can determine the correct storage mapping function. int sum ( int a[][5] ) { int i, j, sum=0; for (i=0; i<3; i++) for (j=0; j<5; j++) sum += a[i][j]; return sum; } In the header of the function definition, the following 3 parameter declarations are equivalent: int a[][5] int a[3][5] int (*a)[5]

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur8 3-dimensional arrays zint a[X][Y][Z]; zThe compiler will allocate X*Y*Z contiguous ints. zThe base address of the array is &a[0][0][0] zStorage mapping function : a[i][j][k]  y*(&a[0][0][0] + Y*Z*i +Z*j + k) zIn the header of the function definition, the following 3 parameter declarations are equivalent: yint a[][Y][Z], int a[X][Y][Z], int (*a)[Y][Z]

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur9 Initialization : multi-d arrays zint a[2][3] = {1,2,3,4,5,6}; zint a[2][3] = {{1,2,3}, {4,5,6}}; zint a[][3] = {{1,2,3}, {4,5,6}};

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur10 The use of typedef #define N 4 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; or typedef vector matrix[N];

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur11 void add (vector x, vector y, vector z) { int i; for (i=0; i<N; i++) x[i] = y[i]+z[i]; } scalar dot_product (vector x, vector y) { int i; scalar sum = 0.0; for (i=0; i<N; i++) sum += x[i]*y[i]; return sum; }

21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur12 void multiply (matrix x, matrix y, matrix z) { int i, j, k; for (i=0; i<N; i++) { for (j=0; j<N; j++) { x[i][j] = 0.0; for (k=0; k<N; k++) { x[i][j] += y[i][k]*z[k][j]; }