Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the concept of one and two dimensional array processing using C language
Arrays in C programming are collections of elements of the same data type stored in contiguous memory locations. Array processing involves operations like reading, writing, and manipulating array elements. Let us explore one-dimensional and two-dimensional array processing.
Syntax
/* 1D Array Declaration */ datatype array_name[size]; /* 2D Array Declaration */ datatype array_name[rows][columns];
1D Array Processing
One-dimensional arrays store elements in a linear sequence. Here's how to read and write elements −
Reading Elements into 1D Array
int num[5];
int i;
for(i=0; i<5; i++){
scanf("%d", &num[i]);
}
Writing Elements from 1D Array
int num[5];
int i;
for(i=0; i<5; i++){
printf("%d ", num[i]);
}
Example: Reverse Order Display
The following program reads elements into an array and displays them in reverse order −
#include <stdio.h>
int main() {
int array[5], i;
printf("Enter 5 elements: ");
for(i = 0; i < 5; i++) {
scanf("%d", &array[i]);
}
printf("Elements in reverse order: ");
for(i = 4; i >= 0; i--) {
printf("%d ", array[i]);
}
printf("<br>");
return 0;
}
Enter 5 elements: 1 2 3 4 5 Elements in reverse order: 5 4 3 2 1
2D Array Processing
Two-dimensional arrays store elements in a matrix format with rows and columns −
Reading Elements into 2D Array
int a[4][3];
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
Writing Elements from 2D Array
int a[4][3];
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
}
printf("<br>");
}
Example: Matrix Addition and Multiplication
The following program demonstrates addition and element-wise multiplication of two matrices −
#include <stdio.h>
int main() {
int A[2][3] = {{2, 3, 4}, {1, 2, 3}};
int B[2][3] = {{4, 5, 3}, {2, 1, 2}};
int sum[2][3], product[2][3];
int i, j;
/* Calculate sum and product */
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
sum[i][j] = A[i][j] + B[i][j];
product[i][j] = A[i][j] * B[i][j];
}
}
printf("Matrix A:<br>");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", A[i][j]);
}
printf("<br>");
}
printf("\nMatrix B:<br>");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", B[i][j]);
}
printf("<br>");
}
printf("\nSum Matrix:<br>");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("<br>");
}
printf("\nProduct Matrix:<br>");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", product[i][j]);
}
printf("<br>");
}
return 0;
}
Matrix A: 2 3 4 1 2 3 Matrix B: 4 5 3 2 1 2 Sum Matrix: 6 8 7 3 3 5 Product Matrix: 8 15 12 2 2 6
Conclusion
Array processing in C involves systematic reading, writing, and manipulation of elements using loops. One-dimensional arrays handle linear data, while two-dimensional arrays manage matrix-like data structures efficiently.
