BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
2D ARRAY TUTORIAL
1. 2 DIMENSIONAL
ARRAY
GROUP A ‘EVEREST’
Amit Budachhetri
Anil Pokhrel
Ajit Pudasaini
Rikriti Koirala
Roji shrestha
Puja Neupane 1
2. CONTENTS
• Basic array
• Difference between 1D &2D
• 2 Dimension Array
• Syntax of 2 D
• Processing Two-Dimensional Arrays
• Declaration of 2 D
• Examples of 2D
• Conclusion
2
3. Array
• Consecutive group of memory location
• All of which have the same type
• We have 1D,2D 3D types of array
• Position number used to refer to a specific
location/element
• Place in square brackets
• Must be positive integer or integer expression
• First element has index zero
3
4. Difference between 1D &2D
• 1d array contains single row and multiple
columns
• 2d array contains multiple row and multiple
columns
• 2d array is a collection of 1d array placed one
below
• 1d has only the length, but 2d has both the
length and width
4
6. 2D Array
• Two-dimensional Array: a collection of a fixed
number of components arranged in two dimensions
• All components are of the same type
• The two expressions row size and column size specify
the number of rows and the number of columns,
respectively, in the array
• Two-dimensional arrays are sometimes called
matrices or tables
6
7. Syntax
• The syntax for declaring a two-dimensional
array is:
Data Type Array Name[rowsize][columnsize];
where row size and column size are
expressions yielding positive integer values
Example:int data[3][4];
―Data is a TWO Dimensional array of size 3*4
elements
7
9. Processing Two-Dimensional Arrays:
• A two-dimensional array can be processed in
three different ways:
1. Process the entire array
2. Process a particular row of the array, called row
processing
3. Process a particular column of the array, called
column processing
9
10. Declaration
• A 2-D array within a function is declared as
follows:
• #define ROW 3
• define COL 5
• ..... what(....)
• {
• int a[ROW][COL] .... ;
• ..........................
• }
10
12. 1.Program to transpose a matrix.
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("nEnter matrix elements :n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++) 12
13. {
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
/*Transpose a matrix */
printf("nTranspose Matrix is :");
for(i=0;i< c;i++)
{
for(j=0;j< r;j++) {
printf("%dt",matrix[j][i]); /*print elements*/
}
printf("n"); /*after each row print new line*/
}
return 0;
} 13
14. OUTPUT
• Enter number of Rows :2
• Enter number of Cols :3
• Enter matrix elements :
• Enter element [1,1] : 1
• Enter element [1,2] : 2
• Enter element [1,3] : 3
• Enter element [2,1] : 4
• Enter element [2,2] : 5
• Enter element [2,3] : 6
• Transpose Matrix is : 1 4
2 5
3 6 14
18. Conclusion
• Two-dimensional array are those type of array, which
has finite number of rows and finite number of
columns
• The rule for giving the array name is same as the
ordinary variable
• The row size and column size should be an individual
constant.
• during declaration the maximum number of rows
and maximum number of column should be specified
for processing all array elements 18