Notes on Two Dimensional Arrays | Grade 12 > Computer Science > Programming in C | KULLABS.COM

• Note
• Things to remember

In the mathematics, we sometimes study set of numbers laid out in rows and columns. Consider the following set of numbers in four rows and three columns. The rows are thought of as numbered one through four from top to bottom, and the columns are numbered , one through three from left to right. If we represent these numbers by t, then these numbers are: t2,3 will represent -43, which is in row 2 and column 3. We need two subscripts.

The row subscript is always written first and the column subscript second. In the two-dimensional array written above, t2,3 =-43, t4,1 =27, and t3,2 = 62. In BASIC these subscripts must be lifted and placed in parentheses as T(2,3), but we still say “T sub two three”.

The two-dimensional array declared in a similar manner to one-dimensional array. The declaration of an array would look like:

Int a ;

Just as with the one-dimensional arrays, the two-dimensional arrays must be placed in the square brackets where first square bracket represents the number of rows and value within second square bracket represents columns. Thus, a maximum number of rows and columns can be declared as needed in the program.

If you are declaring the three-dimensional array, you will need three square brackets.

Int a  ;

The total number of elements in this array would be, 120.

Two-dimensional array could be defined as:

int values   = {1,2,3,4,5,6,7,8,9,10,11,12};

The first number within the square bracket indicates the number of rows and the second value in the square bracket represents the number of columns. Thus, a total number of values or set of arrays is 12. There is a modification in above declaration.

Int values  ;

{

{1,2,3};

{5,6,7,8};

{9,10,11,12};

};

#### Accessing Two Dimensional Arrays

While inputting and accessing the two dimensional array from the keyboard, we use the following convention.

&arrayname[ expression1] [expression];

Here,

&arrayname is the name of the array

[expression1] is the row position

[expression2] is the column position

# Write a program to sort numbers in a array in ascending order.

 /* Sorting in ascending order */ #include #define SIZE 100 void sorting (int n, int x[]); main () { int i, n, x[SIZE]; /*read in a value for n*/ printf(“\n How many number you want to enter ?”); scanf(“%d”,&n); printf(“\n”); /*read the keyed numbers form the keyboard */ for (i=0; i

# Write a program to sort array elements in descending order.

 /* sorting in descending order */ #include #define SIZE 100 void sorting (int n, int x[]); main () { int I, n, X[SIZE}; /*read in a value for n */ printf(“\n How many number you want to enter ?”); scanf(“%d”,&n); printf(“\n”); /* read the keyed numbers form the keyboard */ for (i=; i x[item]) { /*interchange two elements */ temp =x[item]; x[temp] =x[i] x[i] =temp; } return; }

# Write a program to insert an element in the series of elements.

 #include #include void main () { int i, j, k, n, pos; float a, item; printf(“\n Total number of elements (less than 50)”); scanf(“%d”, &n); printf(“\n Enter the elements:\n”); for (i=0; i=pos; k--) /*insertai sort */ a[k] =a [k-1]; a[--post]=item; /*item inserted at this position */ printf(“\n Elements after insertion:\n”); for (i=0; i

# Write a program to define a two dimensional array and print them and their positions.

 #include #include void main () { int j, k; int num  = {1,2,3,4,5,6,7,8,9,10,11,12}; clrscr (); printf(“\n Data stored in the variables are: \n”); for (j=0; j<3; j++) { for(k=0; k<4; k++) { printf(“num [%d, %d] =%d \n”, j, k, num [j] [k]); } printf(“\n”); } }

# Write a program to demonstrate Matrix Addition.

 #include main () { a , b , c  , I, j, m, n, p, q; printf(“\n Input row and column of matrix A. \n”); scanf(“%d %d”, &n, &m); printf(“\n Input row and column for matrix B. \n”); scanf(“%d %d”, &p, %q); /* check whether matrices can be added */ if ((n==p) && (m==q)) { printf(“\n Matrices can be added \n”); printf(“\n Input A-Matrix.\n”); for(i=0; i\n”); for (i=0; i

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 224-230.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

1. An array holds elements that have the same data type, array elements are stored in subsequent memory locations are the characteristics of the array.
2. Array name represents the address of the starting element and Array size should be mentioned in the declaration and Array size must be a constant expression and not a variable.
3. Two-dimensional array are commonly known as matrix  and the array of string is known as two-dimensional array .
.

0%

## ASK ANY QUESTION ON Two Dimensional Arrays

No discussion on this note yet. Be first to comment on this note