Characteristics of Arrays: Array in C
Characteristics of Arrays: Array in C
An array is defined as the collection of similar data items that are stored under a common name.
Arrays are the derived data type in C programming language which can store the primitive type of
data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers, structure, etc.
Each value refer by its index. Using[ ](subscript) for accessing the index
Characteristics of Arrays
The declaration int a[5] creates 5 variables of integer types in the memory. Instead of declaring 5 variables
for 5 values.
The array elements are stored in continuous memory locations
Advantage of C Array
2) Ease of traversing: By using for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
Types of Array
Syntax
Data type array_name[array_size];
In such case, there is no requirement to define the size. So it may also be written as the following code.
int marks[]={20,30,40,50,60};
Example Program
#include<stdio.h>
int main()
{
int i=0;
int marks[5]={20,30,40,50,60}; //declaration and initialization of array
for(i=0;i<5;i++)
{
printf("%d \t",marks[i]);
}
return 0;
}
Output
20 30 40 50 60
Example 2:
#include <stdio.h>
int main()
{
int number[5];
printf("Enter 5 number: ");
for(int i = 0; i < 5; ++i)
{
scanf("%d", &number[i]);
}
printf("Displaying the number: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", number[i]);
}
return 0;
}
Output
Enter 5 number: 1 2 3 4 5
Displaying the number: 1 2 3 4 5
#include<stdio.h>
int main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23}; (7,9,10,23,101)
for(i = 0; i<5; i++)
{
for(j = i+1; j<5; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<5; i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Output
Printing Sorted Element List ...
101
23
10
9
7
Two Dimensional Array in C
An array with two subscripts is termed as two dimensional array.
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented as the collection of rows and columns.
Example
#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3;i++) 0
{
printf("\n");
for(j=0;j<3;j++) 0, 1, 2
{
printf(" %d \t",arr[i][j]);
}
}
return 0;
}
Output
1 2 3
4 5 6
7 8 9
Example
#include<stdio.h>
int main()
{
int disp[10][10];
int i, j,n;
printf("Enter the row and colum: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
for(j=0;j<n;j++)
{
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<n; i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d \t", disp[i][j]);
}
}
return 0;
}
Output
Enter the row and colum: 3
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Enter value for disp[2][0]:7
Enter value for disp[2][1]:8
Enter value for disp[2][2]:9
Two Dimensional array elements:
1 2 3
4 5 6
7 8 9
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
Output
ENTER VALUES FOR FIRST MATRIX :
1
2
3
4
ENTER VALUES FOR SECOND MATRIX :
1
2
3
4
THE ADDITION OF MATRIX IS :
2 4
6 8