Add Two Arrays and Store Result in Third Array
In C programming, we can add two arrays element-by-element and store the result in a third array. The arrays must be of the same size for addition to work correctly.
In this tutorial, we will learn how to add arrays using three types of arrays: 1D array, 2D array, and 3D array.
1. Addition of Two 1D Arrays
A one-dimensional array stores elements in a single row. To add two 1D arrays, we use a loop to add corresponding elements.
#include <stdio.h>
int main() {
int a[5], b[5], c[5];
printf("Enter 5 elements for first array:\n");
for(int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("Enter 5 elements for second array:\n");
for(int i = 0; i < 5; i++) {
scanf("%d", &b[i]);
}
for(int i = 0; i < 5; i++) {
c[i] = a[i] + b[i];
}
printf("Resultant array:\n");
for(int i = 0; i < 5; i++) {
printf("%d ", c[i]);
}
return 0;
}
This program takes input for two arrays and stores their sum in the third array.
2. Addition of Two 2D Arrays (Matrix Addition)
A two-dimensional array represents rows and columns. To add two 2D arrays (matrices), we add elements at the same row and column positions.
#include <stdio.h>
int main() {
int a[2][2], b[2][2], c[2][2];
printf("Enter elements for first 2x2 matrix:\n");
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
printf("Enter elements for second 2x2 matrix:\n");
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
scanf("%d", &b[i][j]);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
c[i][j] = a[i][j] + b[i][j];
printf("Sum of matrices:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
Both matrices must have the same number of rows and columns for addition.
3. Addition of Two 3D Arrays
A three-dimensional array is used to represent data in three dimensions. To add two 3D arrays, we use three nested loops.
#include <stdio.h>
int main() {
int a[2][2][2], b[2][2][2], c[2][2][2];
printf("Enter elements for first 3D array (8 elements):\n");
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
scanf("%d", &a[i][j][k]);
printf("Enter elements for second 3D array (8 elements):\n");
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
scanf("%d", &b[i][j][k]);
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
c[i][j][k] = a[i][j][k] + b[i][j][k];
printf("Sum of 3D arrays:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++)
printf("%d ", c[i][j][k]);
printf("\n");
}
printf("\n");
}
return 0;
}
The logic remains the same: add corresponding elements and store the result in the third array.
Conclusion
Adding two arrays in C is done by summing corresponding elements and storing the result in a third array.
For 1D arrays, use one loop. For 2D arrays, use nested loops. For 3D arrays, use three nested loops.
The key requirement is that both arrays must have the same size and dimensions.
Codecrown