2-d Arrays.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Dynamic Memory Allocation
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
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.
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,
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
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.
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:
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
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.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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.
Arrays. Arrays are objects that help us organize large amounts of information.
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)
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Programming and Data Structures
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Hassan Khosravi / Geoffrey Tien
Numeric Arrays Numeric Arrays Chapter 4.
Programming and Data Structure
Array 9/8/2018.
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
Pointers.
Pointers.
Basic notes on pointers in C
C Passing arrays to a Function
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Lecture 10 Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
CS111 Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Initializing variables
Multidimensional array
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Dr Tripty Singh Arrays.
CHAPTER 2 Arrays and Vectors.
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Arrays.
READING AND PRINTING MATRICES (with functions)
C++ Array 1.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ARRAYS ..
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays and Pointers.
Presentation transcript:

2-d Arrays

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 Subject 1 Subject 2 Subject 3 Subject 4 Subject 5 75 82 90 65 76 68 80 70 72 88 74 85 50 40 Student 1 Student 2 Student 3 Student 4

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

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

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 1 2 3 4 5 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]);

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 col0 col1 col2 col3 col4

How is a 2-d array is 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 0 Row 1 Row 2

Array Addresses Output 3221224480 3221224484 3221224488 3221224492 3221224496 3221224500 3221224504 3221224508 3221224512 3221224516 3221224520 3221224524 3221224528 3221224532 3221224536 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;

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; } Output a = 3221224480 &a[0][0] = 3221224480 &a[2][3] = 3221224532 a[2]+3 = 3221224532 *(a+2)+3 = 3221224532 *(a+2) = 3221224520 a[2] = 3221224520 &a[2][0] = 3221224520 (a+2) = 3221224520 &a[2] = 3221224520

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

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 printf (“%f”, a[i][j]); The elements are all printed on the same line

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

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]); 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]; { printf (“\n”); printf (“%d ”, c[p][q]); } return 0;

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

Example Usage void add (int x[][25], int y[][25], int rows, int cols) { : } int main() { int a[15][25], b[15]25]; : add (a, b, 15, 25); } We can also write int x[15][25], y[15][25]; But at least 2nd dimension must be given

Dynamic Allocation of 2-d Arrays Recall that address of [i][j]-th element is found by first finding the address of first element of i-th row, then adding j to it Now think of a 2-d array of dimension [M][N] as M 1-d arrays, each with N elements, such that the starting address of the M arrays are contiguous (so the starting address of k-th row can be found by adding 1 to the starting address of (k-1)-th row) This is done by allocating an array p of M pointers, the pointer p[k] to store the starting address of the k-th row

Contd. Now, allocate the M arrays, each of N elements, with p[k] holding the pointer for the k-th row array Now p can be subscripted and used as a 2-d array Address of p[i][j] = *(p+i) + j (note that *(p+i) is a pointer itself, and p is a pointer to a pointer)

Dynamic Allocation of 2-d Arrays int **allocate (int h, int w) { int **p; int i, j; p = (int **) malloc(h*sizeof (int *) ); for (i=0;i<h;i++) p[i] = (int *) malloc(w * sizeof (int)); return(p); } void read_data (int **p, int h, int w) { int i, j; for (i=0;i<h;i++) for (j=0;j<w;j++) scanf ("%d", &p[i][j]); } Elements accessed like 2-D array elements. Allocate array of pointers Allocate array of integers for each row

Contd. void print_data (int **p, int h, int w) { int i, j; for (i=0;i<h;i++) for (j=0;j<w;j++) printf ("%5d ", p[i][j]); printf ("\n"); } int main() { int **p; int M, N; printf ("Give M and N \n"); scanf ("%d%d", &M, &N); p = allocate (M, N); read_data (p, M, N); printf ("\nThe array read as \n"); print_data (p, M, N); return 0; }

Contd. void print_data (int **p, int h, int w) { int i, j; for (i=0;i<h;i++) for (j=0;j<w;j++) printf ("%5d ", p[i][j]); printf ("\n"); } int main() { int **p; int M, N; printf ("Give M and N \n"); scanf ("%d%d", &M, &N); p = allocate (M, N); read_data (p, M, N); printf ("\nThe array read as \n"); print_data (p, M, N); return 0; } Give M and N 3 3 1 2 3 4 5 6 7 8 9 The array read as 1 2 3 4 5 6 7 8 9

Memory Layout in Dynamic Allocation int **allocate (int h, int w) { int **p; int i, j; p = (int **)malloc(h*sizeof (int *)); for (i=0; i<h; i++) printf(“%10d”, &p[i]); printf(“\n\n”); for (i=0;i<h;i++) p[i] = (int *)malloc(w*sizeof(int)); return(p); } int main() { int **p; int M, N; printf ("Give M and N \n"); scanf ("%d%d", &M, &N); p = allocate (M, N); for (i=0;i<M;i++) { for (j=0;j<N;j++) printf ("%10d", &p[i][j]); printf(“\n”); } return 0;

Output Starting address of each row, contiguous (pointers are 8 bytes long) 3 3 31535120 31535128 31535136 31535152 31535156 31535160 31535184 31535188 31535192 31535216 31535220 31535224 Elements in each row are contiguous