2D or 2Dimensional Arrays

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

By: gajendra.sunny (26 month(s) ago)

i want to download this ppt. can u give me the link.

Presentation Transcript

2D or 2Dimensional Arrays : 

2D or 2Dimensional Arrays By : Falak Singhal X1 B

Introduction with 2d Arrays… : 

Introduction with 2d Arrays…

What is an array??? : 

What is an array??? “An array is a continued memory allocation that can store many values in memory using one common basic identifier and the individual values are identified by a number of that allocation” Individual elements are identified by integer subscripts which are enclosed in square brackets [] following the array variable. The xi in mathematics is written as x[i] in C++ and most other programming languages. E.g. : int abc[10] //declares an array of of int type variables under common identifier “abc”.

What is an 2d array??? : 

What is an 2d array??? In common language –” Data that is in rows and columns is usually stored in 2-dimensional arrays” Or “ A 2 dimensional array is an array in which there is a collection of one dimensional arrays” e.g. : int abc[3][2] ; //declares an 2d array of three 1d arrays having 2 elements each

Declaration of 2-dimensional - arrays… : 

Declaration of 2-dimensional - arrays…

Slide 6: 

Two-dimensional arrays are declared by specifying the number of rows then the number of columns in the following fashion- <datatype> <identifier> [rows][columns] e.g. : int abc[5][4];

Slide 7: 

The declaration int abc [5][4] will produce the following type of memory allocation : COLUMN ROW

Initializing 2-dimensional - arrays… : 

Initializing 2-dimensional - arrays…

Slide 9: 

Unless specified, all initial values of arrays are garbage. You can specify initial values by enclosing each row in curly braces like this: char ttt[3][3] = {{'x', 'x', 'o'},{'o', 'o', 'x'},{'x', 'o', ' '} }; Note that - If some elements are omitted in the initialization list, they are set to zero.

Slide 10: 

char ttt[3][3] = {{'x', 'x', 'o'},{'o', 'o', 'x'},{'x', 'o', ' '}};

Slide 11: 

int nums[5][4];

Processing 2-dimensional- arrays… : 

Processing 2-dimensional- arrays…

Slide 13: 

To read or process a 2d array, you need to use nested loops One loop processes the row and the other, the columns. If the outer loop is for rows and the inner loop is for columns, then for each row index, all columns are processed and then the same process is repeated for the next row index. Reading the array

Slide 14: 

e.g. : int a[2][3]; //array with 2 rows & 3 columns int i,j; for(i=0 ; i < 2 ; i++) //loop to control rows { for(j=0 ; j < 3 ; j++) //loop to control columns { cout <<“enter element “ ; cin >>a[i][j]; } }

Slide 15: 

The previous code reads values for all elements. It first reads all columns for row 0 and similarly continues… Single memory allocation

Application of 2D arrays : 

Application of 2D arrays

Matrices as 2D arrays : 

Matrices as 2D arrays Matrix is a useful application of 2d arrays Two or more C++ 2d arrays can be used to perform mathematical operations on the arrays visualizing them as separate matrices.

Slide 18: 

E.g.: addition of two matrices #include<iostream.h> #include<conio.h> void accept(int a[3][3]) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<"enter the matrix element "; cin>>a[i][j]; } } }

Slide 19: 

void display(int x[3][3]) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<x[i][j]<<'\t'; }cout<<endl; } }

Slide 20: 

void main() { clrscr(); int a[3][3],b[3][3],sumab[3][3]; accept(a); clrscr(); accept(b); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { sumab[i][j]=a[i][j]+b[i][j]; } } clrscr(); cout<<"I MATRIX"<<endl; display(a); cout<<"II matrix"<<endl; display(b); display(sumab); getch(); }