2D ARRAY IN C
2D Array consisted with number of rows and columns, Therefore it store data in matrix form.
2D ARRAY SYNTAX
int a[2][3];
Explanation: In the above example 'a' is name of 2D Array, [2] is a number of rows and [3] is number of columns.
2D ARRAYP ROGRAM IN C
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][3], i, j;
printf("Enter values in 2D array\n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}}
// Loop to print 2D array in matrix form
for(i=0; i<2; i++)
{
printf("\n\n\n\n");
for(j=0; j<3; j++)
{
printf("\t%d", a[i][j]);
}}
getch();
}
----------------------------
OUTPUT
----------------------------
Enter values in 2D array
2
4
3
7
5
6
2D values in matrix form
2 4 3
7 5 6PROGRAM TO CALCULATE SUM OF ROWS AND COLUMNS VALUES IN 2D ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], i, j, sum=0;
printf("Enter values in 2D array\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}}
// Loop to print 2D array in matrix form and calculate sum
for(i=0; i<2; i++)
{
printf("\n\n\n\n");
for(j=0; j<3; j++)
{
printf("\t%d", a[i][j]);
sum=sum+a[i][j];
}
}
printf("\nSum of 2D array is: %d",sum);
getch();
}
----------------------------
OUTPUT
----------------------------
Enter values in 2D array
2
4
3
7
5
6
8
9
1
2D values in matrix form
2 4 3
7 5 6
0 Comments