In this chapter we shall learn about another important and widely used topic arrays.
Below are the topics discussed:
1. Introduction to arrays
2. Single dimensional arrays initialization
3. Terminology used in arrays
4. 1D array examples
5. 2D array
1. Introduction to arrays
If you have 10 variables to be declared, instead of declaring 10 variables, you can use arrays.
Definition:
An array is a collection of linear and homogeneous data structure. It means it is a collection of elements of same type. And they are stored in continuous memory locations.
2. Single dimensional arrays initialization
An array can be defined in many ways as shown below.
int arr[5] = {1, 2, 3, 4, 5};
or
int arr[] = {1, 2, 3, 4, 5}; // size will be 5 automatically
or
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
As you can see from above, the array index starts from 0. Hence if an array has 5 elements, you need to start accessing from 0 to 4.
3. Terminology used in arrays
Size: This is the length of the array.
Type: Data type of the array.
Base: It is the address of 0th element.
Index: it is used to refer a single element in the array.
Range: It is the length from lower index to higher index.
Example if arr[100], then the range will be 0 to 99.
4. 1D array examples:
#include<stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; ++i)
{
printf("The value at index %d is %d and address is %u\n", i, arr[i], arr+i );
}
return 0;
}
Example: An example to show different types of arrays and the size occupied by them.
#include<stdio.h>
int main()
{
int iArr[5];
float fArr[5];
char cArr[5];
long lrr[5];
printf("The size of int is %d\n",sizeof(int) );
printf("The size of float is %d\n",sizeof(float) );
printf("The size of char is %d\n",sizeof(char) );
printf("The size of long is %d\n",sizeof(long) );
printf("The size of int array for 5 elements is %d\n",sizeof(iArr) );
printf("The size of float array for 5 elements is %d\n",sizeof(fArr) );
printf("The size of char array for 5 elements is %d\n",sizeof(cArr) );
printf("The size of long array for 5 elements is %d\n",sizeof(lrr) );
return 0;
}
Output:
The size of int is 4
The size of float is 4
The size of char is 1
The size of long is 8
The size of int array for 5 elements is 20
The size of float array for 5 elements is 20
The size of char array for 5 elements is 5
The size of long array for 5 elements is 40
An example to show simple array with their addresses:
Output:
The value at index 0 is 1 and address is 3918781136
The value at index 1 is 2 and address is 3918781140
The value at index 2 is 3 and address is 3918781144
The value at index 3 is 4 and address is 3918781148
The value at index 4 is 5 and address is 3918781152
5. 2D array
Similar to 1D array, there can be 2D array.
They are represented as below
arr[row] [column]
An array of arr[3][2] is represented as shown below:
column 0 column 1
row 0 1 2
row 1 3 4
row 2 5 6
Form the above example we can say that the total elements that can be hold by a 2D array is rows * columns.
Example to show inserting and displaying 2D arrays.
#include<stdio.h>
int main()
{
int array[3][2];
printf("Enter the elements for 2D array\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
scanf("%d", &array[i][j]);
}
}
printf("\nThe elements entered for 2D array\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter the elements for 2D array
1
2
3
4
5
6
The elements entered for 2D array
1 2
3 4
5 6