Define array? What are the benefits of using array? Write a program to add two matrices using array.

An array is a group of related data items that share a common name. In the other words, an array is a data structure that stores a number of data items as a single entity (object). The individual data items are called elements and all of them have some data types. An array is used when multiple data items that have common characteristics are required.

The benefit of using arrays are given below

1. In array, We can access the data very easily using index number
2. We can apply searching process in array easily
3. We can represent 2D arrays as matrices
4. We can used to implement other data structure like linked lists, stack, queue, trees, graph etc.

Program to add two matrices using matrix

#include <stdio.h>

int main()
{
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d   ", sum[i][j]);
if (j == c - 1)
{
printf("\n\n");
}
}

return 0;
}


The output of above program is

Enter the number of rows: 2
Enter the number of columns: 3

Enter elements of 1st matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 5
Enter element a23: 6
Enter elements of 2nd matrix:
Enter element b11: 7
Enter element b12: 8
Enter element b13: 9
Enter element b21: 1
Enter element b22: 2
Enter element b23: 3

Sum of two matrices:
8   10   12

5   7   9

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.