2 Dimensional Arrays in C / C++ with Sum of Arrays

2 Dimensional Arrays in C / C++ with Sum of Arrays

Let’s begin with some basic understanding of the 2-dimensional array.

How to declare and use 2-dimensional Arrays?

Example 1: int x[2][3] = {{4, 2, 3}, {9, 6, 7}};

Example 2:int x[3][4] = {{4, 2, 3, 9}, {9, 6, 7, 2}, {3, 4, 7, 8}};

Example of different ways to initialize two dimensional array are mentioned below;

int array[2][3] = {{9, 3, 0}, {7, 5, 9}};

int array[][3] = {{3, 3, 0}, {-1, 5, 9}};

int array[2][3] = {5, 3, 5, 8, 5, 4};

2 Dimensional Arrays Program in C / C++

 

Output:

Element at x[0][0]: 6

Element at x[0][1]: 4

Element at x[1][0]: 8

Element at x[1][1]: 7

Element at x[2][0]: 1

Element at x[2][1]: 9

Sum of two arrays using Two dimensional arrays

Ouput
Enter elements for array1
Enter a11: 42;
Enter a12: 2;
Enter a21: -8;
Enter a22: 4;
Enter elements for array2
Enter b11: 3;
Enter b12: 2;
Enter b21: 2;
Enter b22: 3;

Overall Sum Of full Matrix is:
7            5
10          7

Comparison Between one and two Dimensional Array

1-DIMENSIONAL: Store a single list of elements. Store only similar elements with the same data type.

2-DIMENSIONAL: Store ‘list of lists’ or ‘array of arrays’ or ‘array of 1-dimensional arrays’.

Comparison of Declaration Between one and two Dimensional Array

1-DIMENSIONAL: How to declare in C++? type variable_name[ size ];

2-DIMENSIONAL: How to declare in C++. type variable_name[size1][size2];

Comparison of Receiving parameter Between one and two Dimensional Array

1-DIMENSIONAL: Parameters can be received in the followings;

  • sized array
  • unsized array
  • Pointer

2-DIMENSIONAL: It must define the rightmost dimension of an array.

Comparison of Total Size occupies in Bytes Between one and two Dimensional Array

1-DIMENSIONAL:Total Bytes =sizeof(datatype of array variable)* size of array.

2-DIMENSIONAL: Total Bytes= sizeof(datatype of array variable)* size of the first index*size of the second index.