Computer Programming C: UNIT II: Arrays and Strings

Two Dimensional Array

C Programming

Two dimensional arrays are used in situation where a table of values need to be stored in an array.

TWO DIMENSIONAL ARRAY


1. INTRODUCTION

Two dimensional arrays are used in situation where a table of values need to be stored in an array.

These can be defined in the same fashion as in one dimensional arrays, except a separate pair of square brackets are required for each subscript. Two pairs of square brackets required for two dimensional array and three pairs required for three dimensional arrays and so on.


Example: int a[3][3];

Two dimensional arrays are stored in a row‒column matrix, where the left index indicates the row and the right indicates the column.

Example: int a[3][3];

Where 'a' is the array name and it reserves 3 rows and 3 columns of memory as shown below.


The individual elements are identified by index or subscript of an array, from the above example.



Example: Program to specify two dimension array.

/*Program using two dimension array */

#include <stdio.h>

main()

{

      /* Local definitions */

      int stud[4][2]; /*An array name with 4 rows and 2 columns */

      int i;

      /* Statements */

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

      {

            printf("\n Enter the %d Student roll no and Mark:",i);

            scanf("%d %d", &stud[i][0], &stud[i][1]);

      } /* for */

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

            printf("%d Students roll no %d mark %d", i, stud[i][0], stud[i][1]);

} /* main */

OUTPUT

Enter the 0 student roll no and mark: 3977 80

Enter the 1 student roll no and mark: 17776  95

Enter the 2 student roll no and mark: 6682 82

Enter the 3 student roll no and mark: 6683 85

0 student roll no 3977          mark 80

1 student roll no 17776          mark 95

2 student roll no 6682          mark 82

3 student roll no 6683          mark 85

EXPLANATION: Look at the scanf() statement used in the first for loop in the above example. In stud[i][1] and stud [i][1] the first subscript of the variable stud, is row number which changes for every student. The second subscript tells the column number. The following diagram shows how the data is stored in the memory.

Note that all data stored in continuous memory location.


i.e., 3977 is stored in stud [0][0], 80 is stored in stud [0][1] and so on. In fact two dimensional array is nothing but a collection of a number of one dimensional arrays placed one below the other.

 

2. INITIALISING A TWO DIMENSIONAL ARRAY

Like one dimensional array, the values can be initialised to the two dimensional arrays at the time of declaration

Syntax: data type array_name[row_size][column_size] = {List of values};

Description: {List of variables} specifies the list of elements.

Example

int stud [4][2] =

{

    {6680, 80},

    {6681, 95},

    {6682, 82},

    {6683, 85},

};

Remember that while initialising an array it is necessary to mention the second dimension, where as the first dimensions is optional. So the following will never work;

int arr[2] [] = {1,2,3,4,5,6};

int arr[][] = {1,2,3,4,5,6};

We must mention the column size then only the compiler knows where the first row ends. The row size is optional if we initialise the array in the declaration part itself.

Representation of 2 x 2 array Eg:‒ 2 x 2 Matrix view 

 int a = [2][2] {1,2,3,4}

Example 1: Program to illustrate Matrix Multiplication

* Program for matrix multiplication */

#include <stdio.h>

main()

{

        int a[5][5], b[5][5],c[5][5],r1, r2, c1, c2, i,j,k;

Step 1 :

        printf("\nEnter the size of the matrix A....");

        scanf("%d %d", &rl, & cl);

        printf("\nEnter the size of the matrix B....");

        scanf("%d %d", &r2, & c2);

        if(c1= =r2)

                goto step2;

        else

                printf("\nMultiplication is not possible");

                goto stepl;

Step2 :

        printf("\nEnter matrix A elements...\n");

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

        {

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

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

        } /* for */

        printf("\nEnter matrix B elements...\n");

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

        {

               scanf("%d", &b[i][j]);

        } /* for */

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

        {

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

                {

                c[i][j]=0;

                        for(k=0;k<c1;k++)

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

                } /* for */

                printf("\nThe resultant matrix is ....\n");

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

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

                                printf("%d\t",c[i][j]);

                        printf("\n");

                } /* for */

} /* main */

OUTPUT:

Enter the size of the matrix A....3 3

Enter the size of the matrix B....2 2

Multiplication is not possible

Enter the size of the matrix A....3 3

Enter the size of the matrix B....3 3

Enter matrix A elements...

2 2 2 2 2 2 2 2 2

Enter matrix B elements...

3 3 3 3 3 3 3 3 3

The resultant matrix is

18 18 18

18 18 18

18 18 18

EXPLANATION: The program reads size of the matrix, then checks rows and column are equal or not. If rows and columns are equal further execution starts, else it prints multiplication is not possible. The i and j for loop is used to get both A and B matrix. The another i and j for loop is used to calculate the matrix multiplication and final i and j for loop is used to print the resultant matrix.

 

Computer Programming C: UNIT II: Arrays and Strings : Tag: Computer Science : C Programming - Two Dimensional Array


Computer Programming C: UNIT II: Arrays and Strings



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation