Two Dimensional Arrays

This note is about the Two Dimensional Array found in the C programming Language and has great features of it in C programming Language. It includes the Accessing Two Dimensional Array too.

Summary

This note is about the Two Dimensional Array found in the C programming Language and has great features of it in C programming Language. It includes the Accessing Two Dimensional Array too.

Things to Remember

 

  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 .

MCQs

No MCQs found.

Subjective Questions

No subjective questions found.

Videos

No videos found.

Two Dimensional Arrays

Two Dimensional Arrays

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[3] [4];

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[3] [4] [5];

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

Two-dimensional array could be defined as:

int values [3] [4] = {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 [3] [4];

{

{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 <stdio.h>

#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<n; i++)

{

printf(“i =%d x =”, i+1);

scanf(“%d”, &x[i]]);

}

/* rearrange all the array elements */

sorting (n,x);

/* display the rearranged list of numbers */

printf(“\n \n Rearranged list of numbers */

for(i=0; i<n; i++)

printf(“i=%d x =%d\n”, i+1, x[i]);

}

/* Function that sorts Selection sort*/

void sorting (int n, int x[])

{

int I, item, temp;

for(item =0; item <n-1; ++item)

/* find the smallest of all the remaining elements in array */

for (I =item +1; i<n; ++i)

if (x [i] < x[item])

(/*interchange tow elements */

temp =x[item];

x[item] =x[i];

x[i] =temp;

}

return;

}

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

/* sorting in descending order */

#include <stdio.h>

#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<n; ++i);

{

printf(“i= %d x =”, i+);

scanf(“%d”, &x[i]);

}

/* rearrange all the array elements */

sorting (n,x);

/* display the rearrange list of numbers */

printf(“\n \n Rearranged list of number:\n\n”);

for (i=0; i<n; ++i)

printf(“i=%d x=%d\n”, i+1, x [i]);

}

/* Function that sorts */

void sorting (int n, int x[])

{

int i, temp, temp;

for(item =0; item <n-1; ++item)

/*find the smallest of all the remaining elements in array */

for(i=item+1; i<n; ++i)

if(x[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 <stdio.h>

#include <conio.h>

void main ()

{

int i, j, k, n, pos;

float a[50], item;

printf(“\n Total number of elements (less than 50)”);

scanf(“%d”, &n);

printf(“\n Enter the elements:\n”);

for (i=0; i<n; i++)

scanf(“%f”, &a[i]);

printf(“\n Enter the position to be inserted less than the given numbers:”);

scanf(“%d”,&pos);

/* pushing elements to right to insert the number */

n++;

for (k=n; k>=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<n; i++)

printf(“6.2f”, a[i]);

printf(“\n”);

}

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

#include <stdio.h>

#include <conio.h>

void main ()

{

int j, k;

int num[3] [4] = {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 <stdio.h>

main ()

{

a[10] [10], b[10] [10], c [10] [10], 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;++i)

for (j=0;j<m; ++j)

scanf(“%d, &a[i] [j]);

printf(“\n Enter B-Matrix.\n”);

for (i=0; i<n; ++i)

for (j=0; j<m; ++j)

scanf(“%d”, &[i] [j]);

/* Addition of two matrices */

for (i=0;i<n; ++i)

for (j=0;j<m; ++j)

c[i][j]=a[i][j] +b [i][j];

printf(“\n Sum of A and B matrices>\n”);

for (i=0; i<n; ++i)

for (j=0; j<m; ++j)

{

printf(“] ] ]\n”, a[i][j], b[i][j], c[i][j]);

}

printf(“\n”);

else

printf(“\n Matrices cannot be added”);

}

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

Lesson

Programming in C

Subject

Computer Science

Grade

Grade 12

Recent Notes

No recent notes.

Related Notes

No related notes.