In this article, we will discuss what is an array and its types? If we want to store n values, then we need n variables. In general, we declare a variable to store a value. So, we need n variables to store n values. If we want to keep track of the salary of 50 employees we must declare 50 variables, which is not handy for our programming. Our program becomes more complex, and it reduces efficiency and readability. ARRAY can address this problem.
An array is a variable that can store multiple values of the same type.
There is an array of the same element (homogenous).
Array is a group of values.
Arrays are a collection of elements with the same data type and size.
You can use an array to store a list of numbers or a list of names.
A set of variables grouped to form an array.
It is also necessary to declare a subscript when declaring an array.
Advantages of Array
Less code to the access the data.
Multiple data items of the same type using only one name.
You can use it to implement other data structures like linked lists, stacks, queues, trees, graphs, etc.
It is easy to retrieve the elements of an array using the loop.
Using the array, we can access any element at random.
Drawback of an Array
We cannot exceed the size limit we set when we create the array. So, unlike Linked Lists, it doesn’t grow during use.
C does not check the bounds of array indexes.
Since we cannot change the size of an array, developers generally declare large arrays in case their data needs to grow in the future.
Types of Array
Following are the types of Array:
One Dimensional(1-D) Array
A single subscript represents each element of a 1D array.
Memory locations for the elements are consecutive.
Declaration of 1D Array
Syntax data_type array_name[array_size];
Example
int marks[5];
Here, int is the data_type, marks is the array_name and 5 is the array_size
Initialization of 1D Array
An array will allocate the following types of memory blocks.
int a[5]; /* 5 is a subscript */
But at that point, the array contains meaningless (garbage) values.
Initialization refers to assigning values to an array.
The initialization uses curly brackets.
int a[5]={10,20,30,40,50}; //Array of Integers - Integer Array char a[3]={'R','A','M'); //Array of Characters - Character Array
Characters are represented in a single quote otherwise they treat as a variable.
When the array’s size is less than the value assigned to it, zero 0 is assigned to the remaining values.
int a[5]={10,20,30};
10
20
30
0
0
You can omit the subscript or size of an array (not defined).
The compiler in this case provides enough space for all initialized elements.
int a[]={10,20,30};
10
20
30
0
0
char name[] = {'r','a','m','\0'); /* \0 is NULL value */ char name[]="ram";
The term “string” refers to an array of characters.
How to Access the Elements of a 1D Array?
As we know that an array can store more than one element.
It is then difficult to access an element since all elements have the same name.
As a result, each element in an array is assigned a unique position number that is called an “index”.
This index always and always starts from 0 to n-1 where n is the subscript of an array.
Consequently, this index number identifies the element.
int a[5]={10,20,30,40,50);
10
20
30
40
50
a
Index 0 1 2 3 4
N = subscript of an array.
Here n = 5 [So index range is 0 to 4.]
10 can be accessed ->a[0]
20 can be accessed ->a[1]
30 can be accessed ->a[2]
40 can be accessed ->a[3]
50 can be accessed ->a[4]
Scan or Enter a Value in a 1D Array
During runtime, we use scanf( ) to enter values into variables.
Location
scanf( ) Syntax
a[0]
scanf(“%d”,&a[0]);
a[1]
scanf(“%d”,&a[1]);
a[2]
scanf(“%d”,&a[2]);
a[3]
scanf(“%d”,&a[3]);
a[4]
scanf(“%d”,&a[4]);
Here the user will input values one by one, and then store them in array locations.
It is not feasible to write scanf( ) for each element of an array when it has a lot of elements or large size.
Loops are also acceptable here as scanf( ) only changes the index. Since a loop is easier to use here, it can simplify the process.
for(i=0;i<5;i++) { scanf ("%d",&a[i]); }
Example Array Input and Show
include<stdio.h>
include<conio.h>
void main()
{
int arr[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter a number ");
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
Output Enter a number 1
Enter a number 3
Enter a number 5
Enter a number 6
Enter a number 7
1 3 5 6 7
Two Dimensional (2-D) Array
Matrixes in the C language represent two-dimensional arrays as rows and columns. This is also called an array of arrays or a list of arrays.
Declaration of 2D Array
It uses two subscripts.
The first subscript for the row size.
The second subscript is for the column size.
Syntax
data_type array_name [row size][column size];
Numbers of element in 2D are
Number of element = Row * Column
Example
int a[4][3];
Total number = 4*3=12
The following figure shows four rows and three columns in each row…….
00
01
02
10
11
12
20
21
22
30
31
32
As you can see, this Array is a 2-D Array that can store 12(4*3) Values of integer type, also known as a Matrix.
Initiation of the 2D array
Initializing a 2D array requires the following:
Column size is a must.
Row size is optional.
This memory contains the following values:
1
2
3
4
5
6
7
8
9
10
11
12
Memory Allocation
Characters are represented in single quotes otherwise they are treated as variables.
As soon as the array is filled with values less than its dimensions, the remaining will be set to 0.
Element Access for a 2D Array
It’s similar to accessing the elements of a 1-d array, but in a 2-d array, there will be two indexes for each element.
where, First, will indicate row number.
The second will indicate column number.
ExampleSum of Matrix
include<stdio.h>
include<conio.h>
void main()
{
int i,j,a[5][5],n,m,b[5][5];
clrscr();
printf("Enter Number of Row in First Array:");
scanf("%d",&n);
printf("Enter Number of Column in First Array:");
scanf("%d",&m);
printf("Total Number of Value to Enter (Row*Column):%d\n",n*m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
printf("Enter Number of Row in Second Array:");
scanf("%d",&n);
printf("Enter Number of Column in Second Array:");
scanf("%d",&m);
printf("Total Number of Value to Enter (Row*Column):%d\n",n*m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(" %d ",b[i][j]);
}
printf("\n");
}
printf("Sum of Matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(" %d ",a[i][j]+b[i][j]);
}
printf("\n");
}
getch();
}
Output Enter Number of Row in First Array:2
Enter Number of Column in First Array:2
Total Number of Value to Enter (Row*Column):4
5
6
7
8
5 6
7 8
Enter Number of Row in Second Array:2
Enter Number of Column in Second Array:2
Total Number of Value to Enter (Row*Column):4
1
2
3
4
1 2
3 4
Sum of Matrix:
6 8
10 12
Multi-Dimensional Array
We can use the dimension based on our requirements.
In the n-Dimension Array, there is a need for n square brackets and n subscript.