Broad Network


Two Dimensional Array in the C Computer Language

Part 16 of Complete C Course

Foreword: Two Dimensional Array in the C Computer Language

By: Chrysanthus Date Published: 3 Jun 2024

Introduction

Consider the following table, consisting of letters of the alphabet:

    'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O',
    'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y',
    'Z', ' ', ' ', ' ', ' '

There are 6 rows and 5 columns. The last row has extra four single space characters, for completion of the 4 remaining column cells.

A Two-Dimensional or 2D array is declared as follows:

    Type arrayName[rows][columns]

where “rows” is the number of rows and “columns” is the number of columns. The above table can be defined as a 2D array as follows:

        int TwoDArr[6][5] = {{'A', 'B', 'C', 'D', 'E'},
                         {'F', 'G', 'H', 'I', 'J'},
                         {'K', 'L', 'M', 'N', 'O'},
                         {'P', 'Q', 'R', 'S', 'T'},
                         {'U', 'V', 'W', 'X', 'Y'},
                         {'Z', ' ', ' ', ' ', ' '}};

With a 2D array, the number of rows and columns should be included in the square brackets. Note that the 2D array list, consists of a list of one-dimensional arrays.  Note the position of the commas, separating the 1D arrays. Note the presence of the extreme opening curly bracket, and the extreme closing curly bracket. Alternatively, the 2D array can be declared as follows:

    int twoDArr[6][5] ;

And then the data fed in one-by-one, as follows:

        twoDArr[0][0]='A'; twoDArr[0][1]='B'; twoDArr[0][2]='C'; twoDArr[0][3]='D'; twoDArr[0][4]='E';
        twoDArr[1][0]='F'; twoDArr[1][1]='G'; twoDArr[1][2]='H'; twoDArr[1][3]='I'; twoDArr[1][4]='J';
    - - - - etc. - - - -

Each short statement ends with a semicolon: there can be more than one simple statement in one line, as long as each statement ends with a semicolon.

The following program declares the above table, and then prints out all the elements, in the intended layout:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int TwoDArr[6][5] = {{'A', 'B', 'C', 'D', 'E'},
                         {'F', 'G', 'H', 'I', 'J'},
                         {'K', 'L', 'M', 'N', 'O'},
                         {'P', 'Q', 'R', 'S', 'T'},
                         {'U', 'V', 'W', 'X', 'Y'},
                         {'Z', ' ', ' ', ' ', ' '}};
                        
        for (int i=0; i<6; i++) {
            for (int j=0; j<5; j++) {
                printf("%c ", TwoDArr[i][j]);
            }
            printf("\n");
        }

        return 0;
    }

Read through the program. The output is:

    A B C D E
    F G H I J
    K L M N O
    P Q R S T
    U V W X Y
    Z    

In the code, a for-loop has another nested for-loop. This combination displays the 2D data. The outer for-loop is for the rows, and the inner for-loop is for the columns. The index variable, i is for the rows, and the index variable, j is for the columns.



Related Links

More Related Links

Cousins

BACK NEXT

Comments