Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a two-dimensional array in C language?
An array is a group of related items that store with a common name.
Syntax
The syntax is as follows for declaring an array −
datatype array_name [size];
Types of arrays
Arrays are broadly classified into three types. They are as follows −
- One – dimensional arrays
- Two – dimensional arrays
- Multi – dimensional arrays
Initialization
An array can be initialized in two ways, which are as follows −
- Compile time initialization.
- Runtime initialization.
Two multidimensional arrays
These are used in situations where a table of values have to be stored (or) in matrices applications.
Syntax
The syntax is given below −
datatype array_ name [rowsize] [column size];
For example int a[5] [5];
| a[0][0] 10 |
a[0][1] 20 |
a[0][2] 30 |
| a[1][0] 40 |
a[1][1] 50 |
a[1][2] 60 |
| a[2][0] |
a[2][1] | a[2][2] |
Following is the C Program for compile time initialization −
Example
#include<stdio.h>
main ( ){
int a[3][3] = {10,20,30,40,50,60,70,80,90};
int i,j;
printf ("elements of the array are");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
printf("%d \t", a[i] [j]);
}
printf("
");
}
}
Output
The output is stated below −
elements of the array are: 10 20 30 40 50 60 70 80 90
Following is the C program for runtime initialization −
Example
#include<stdio.h>
main ( ){
int a[3][3] ,i,j;
printf ("enter elements of array");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
scanf("%d", &a[i] [j]);
}
}
printf("elements of the array are");
for ( i=0; i<3; i++){
for (j=0;j<3; j++){
printf("%d\t", a[i] [j]);
}
printf("
");
}
}
Output
The output is stated below −
Enter elements of array : 1 2 3 4 5 6 7 8 9 Elements of the array are 1 2 3 4 5 6 7 8 9
Given below is the C program to calculate sum and product of all elements in an array using run time compilation −
Example
#include<stdio.h>
void main(){
//Declaring the array - run time//
int A[2][3],B[2][3],i,j,sum[i][j],product[i][j];
//Reading elements into the array's A and B using for loop//
printf("Enter elements into the array A:
");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("A[%d][%d] :",i,j);
scanf("%d",&A[i][j]);
}
printf("
");
}
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("B[%d][%d] :",i,j);
scanf("%d",&B[i][j]);
}
printf("
");
}
//Calculating sum and printing output//
printf("Sum array is :
");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
sum[i][j]=A[i][j]+B[i][j];
printf("%d\t",sum[i][j]);
}
printf("
");
}
//Calculating product and printing output//
printf("Product array is :
");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
product[i][j]=A[i][j]*B[i][j];
printf("%d\t",product[i][j]);
}
printf("
");
}
}
Output
The output is stated below −
Enter elements into the array A: A[0][0] :2 A[0][1] :3 A[0][2] :1 A[1][0] :2 A[1][1] :4 A[1][2] :5 B[0][0] :1 B[0][1] :2 B[0][2] :3 B[1][0] :5 B[1][1] :6 B[1][2] :7 Sum array is : 3 5 4 7 10 12 Product array is : 2 6 3 10 24 35
Advertisements