An array is an identifier that stores a set of similar data. In another way, arrays can be defined as a data structure that can store a fixed size data in a sequential order where the data in it are of the same type. While arrays can store multiple values, a variable can store only one value at a time. For example, an array can store all the days Monday to Sunday while an array can store only one day, like Monday.
Arrays can be either one dimensional or multi-dimensional. A one-dimensional array would store data of the kind, prime numbers between 1 and 100, all the weekdays, etc. Multidimensional arrays would data the kind, data stored in rows and columns, for an instance a matrix. Let us discuss each of them.
An array fills the memory space in a contiguous manner. Each element in it would be allocated equal space which can be accessed by a value known as the index.
One Dimensional Array
Let us assume an array of the size n as shown in the figure. For an array with n size, it would accommodate n+1 elements. Such arrays are known to be the one-dimensional arrays.
Element[0] Element[1] … … … Element[n-2] Element[n-1]
Declaring Arrays
Arrays are defined like the variables with its size mentioned in square brackets [] as.
Datatype arrayName[size];
Initializing Arrays
It is not possible to assign all the elements to an array at once. So the elements must be initialized as shown below:
int arr[5] = {1, 2, 3, 4, 4};
All the elements are enclosed between the curly braces and separated by commas.
Example 14: Write a program that takes the number of array elements, asks the user to enter the data and find the sum of it.
Sample Code:
[php]
#include <stdio.h>
//arraysum.c by bhutanio.com
void main()
{
int n, i;
int sum=0;
printf("\nEnter the number of elements ");
scanf("%d", &n);
int arr[n];
printf("\nEnter the array elements \n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<n; i++)
sum=sum+arr[i];
printf("\nSum of the elements: %d", sum);
}
[/php]
Two Dimensional Arrays
The simplest form of multi-dimensional arrays is the two dimensional (2-D) arrays. These arrays are like matrices. Like matrices, the entries are in rows and columns. An m by n matrix would look like:
Declaring 2-D Arrays
To declare such arrays, we do like in the one-dimensional array but here we need two pairs of square braces to mention the number of rows and columns, like.
datatype Array_Name[rows] [columns];
For the matrix A that takes character elements (mentioned above):
char A[m][n];
Initializing 2-D Arrays
2-D arrays are initialized by assigning bracketed values for each rows. For example:
int A[3][3] = {{1, 2, 3} , {1, 1, 2}, {1, 2, 3}};
Or equivalently, it can be initialized as:
int A[3][3] = {1, 2, 3, 1, 1, 2, 1, 2, 3};
Accessing 2-D Arrays
The array elements are accessed by specifying the rows and columns of the element to be accessed in two pairs of square brackets. For example, if I am to get an integer number from array A at 0th row and 0th column:
int x = A[0][0];
Now the variable x has the value of the element in A[0][0].
Example 15: Assume matrix
A = [1 0 0, 0 1 0, 0 0 1]
Write a C program that prints all the elements with its row and column indexes, and print the element at A[1][1].
Sample Code
[php]
#include <stdio.h>
//implementing 2d array by bhutanio.com
void main(){
int a[3][3] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
int b = a[1][1];
int i , j;
//for printing all the elements with indexes
for(i = 0; i< 3; i++){
for(j = 0; j< 3; j++){
printf("\na[%d][%d] = %d", i, j, a[i][j]);
}
}
//for printing element at a[1][1]
printf("\nThe element at a[1][1] is %d", b);
}
[/php]
You may also like to check my previous chapters. Check them out at the links below (in the Related Posts Links).
Pingback: Array data structure in python | Understanding Array | Programming