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.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
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.
©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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
 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.
Multiple-Subscripted Array
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
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.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
A First Book of ANSI C Fourth Edition
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
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.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
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.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. Arrays are objects that help us organize large amounts of information.
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.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Programming and Data Structures
Programming and Data Structure
Array 9/8/2018.
Pointers.
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
2-d Arrays.
Chapter 2 Array and String Visit to more Learning Resources.
CHAPTER 2 Arrays and Vectors.
CHAPTER 2 Arrays and Vectors.
Arrays.
READING AND PRINTING MATRICES (with functions)
ARRAYS ..
Presentation transcript:

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 values Student 1 Student 2 Student 3 Student 4 Subject 1Subject 2Subject 3Subject 4Subject 5

3 Contd. The table contains a total of 20 values, five in each line  The table can be regarded as a matrix consisting of four rows and five columns C allows us to define such tables of items by using two-dimensional arrays

4 Declaring 2-D Arrays General form: type array_name [row_size][column_size]; Examples: int marks[4][5]; float sales[12][25]; double matrix[100][100];

5 Initializing 2-d arrays int a[2][3] = {1,2,3,4,5,6}; int a[2][3] = {{1,2,3}, {4,5,6}}; int a[][3] = {{1,2,3}, {4,5,6}}; All of the above will give the 2x3 array

6 Accessing Elements of a 2-d Array Similar to that for 1-d array, but use two indices  First indicates row, second indicates column  Both the indices should be expressions which evaluate to integer values (within range of the sizes mentioned in the array declaration) Examples: x[m][n] = 0; c[i][k] += a[i][j] * b[j][k]; a = sqrt (a[j*3][k]);

7 Example int a[3][5]; A two-dimensional array of 15 elements Can be looked upon as a table of 3 rows and 5 columns 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 col0col1col2col3col4

8

9 How is a 2-d array stored in memory? Starting from a given memory location, the elements are stored row-wise in consecutive memory locations (row-major order) x: starting address of the array in memory c: number of columns k: number of bytes allocated per array element  a[i][j]  is allocated memory location at address x + (i * c + j) * k a[0]0] a[0][1] a[0]2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] Row 0Row 1Row 2

10 Array Addresses int main() { int a[3][5]; int i,j; for (i=0; i<3;i++) { for (j=0; j<5; j++) printf("%u\n", &a[i][j]); printf("\n"); } return 0; } Output

11 More on Array Addresses int main() { int a[3][5]; printf("a = %u\n", a); printf("&a[0][0] = %u\n", &a[0][0]); printf("&a[2][3] = %u\n", &a[2][3]); printf("a[2]+3 = %u\n", a[2]+3); printf("*(a+2)+3 = %u\n", *(a+2)+3); printf("*(a+2) = %u\n", *(a+2)); printf("a[2] = %u\n", a[2]); printf("&a[2][0] = %u\n", &a[2][0]); printf("(a+2) = %u\n", (a+2)); printf("&a[2] = %u\n", &a[2]); return 0; } a = &a[0][0] = &a[2][3] = a[2]+3 = *(a+2)+3 = *(a+2) = a[2] = &a[2][0] = (a+2) = &a[2] = Output

12 How to read the elements of a 2-d array? By reading them one element at a time for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) scanf (“%f”, &a[i][j]); The ampersand (&) is necessary The elements can be entered all in one line or in different lines

13 How to print the elements of a 2-d array? By printing them one element at a time for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) printf (“\n %f”, a[i][j]);  The elements are printed one per line for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) printf (“%f”, a[i][j]);  The elements are all printed on the same line

14 Contd. for (i=0; i<nrow; i++) { printf (“\n”); for (j=0; j<ncol; j++) printf (“%f ”, a[i][j]); }  The elements are printed nicely in matrix form

15 Example: Matrix Addition int main() { int a[100][100], b[100][100], c[100][100], p, q, m, n; scanf (“%d %d”, &m, &n); for (p=0; p<m; p++) for (q=0; q<n; q++) scanf (“%d”, &a[p][q]); for (p=0; p<m; p++) for (q=0; q<n; q++) scanf (“%d”, &b[p][q]); for (p=0; p<m; p++) for (q=0; q<n; q++) c[p][q] = a[p][q] + b[p][q]; for (p=0; p<m; p++) { printf (“\n”); for (q=0; q<n; q++) printf (“%d ”, c[p][q]); } return 0; }

16 Passing 2-d Arrays as Parameters Similar to that for 1-D arrays  The array contents are not copied into the function  Rather, the address of the first element is passed For calculating the address of an element in a 2-d array, we need:  The starting address of the array in memory  Number of bytes per element  Number of columns in the array The above three pieces of information must be known to the function

17 Example Usage int main() { int a[15][25], b[15]25]; : add (a, b, 15, 25); : } void add (int x[15][25], int y[15][25], int rows, int cols) { : } Parameter passing