Last Updated on April 25, 2023 by Prepbytes
An array is a fundamental data structure that is used to store a collection of elements of the same data type in a contiguous block of memory. Arrays are often used to represent collections of data such as lists, tables, matrices, or multidimensional structures. In this article, we will focus on the types of arrays based on their dimensions.
Types Of Array
Arrays can be classified based on their dimensions, which refer to the number of indices required to access an element in the array. In this section, we will discuss two types of arrays: single-dimensional arrays and multi-dimensional arrays.
Single Dimensional Array
A single-dimensional array is the simplest type of array that stores elements in a linear sequence. It is also known as a one-dimensional array, and each element in the array can be accessed using a single index. The index ranges from 0 to the length of the array minus one.
Syntax for Declaration of Single Dimensional Array
The declaration syntax for the one-dimensional array is listed below.
data_type array_name[array_size];
//Example
int evenNumbers[5];
data_type : is a type of data for each block of an array.
array_name : is the array’s name, which we can use to refer to it.
array_size : is the number of memory array blocks will have.
Initialization of Single Dimensional Array
A list of elements separated by commas and enclosed in curly brackets can be used to initialize an array.
data_type array_name[array_size] = {element1, element2, element3, ......... , elementN};
//Example
int evenNumbers[5] = {0, 2, 4, 6, 8};
Important Point: What happens if we supply less elements than the array can hold?
In this case, the default value—null for a string array, 0 for an integer array, etc.—will be used to fill the remaining memory block. See the example in the section below for further details.
#include <stdio.h> int main() { int arr[5] = {7, 8}; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; }
Output
7 8 0 0 0
Arrangement
Accessing Elements of Single Dimensional Array
By combining the element’s index and array name, we may retrieve an element in a single-dimensional array. The array’s index begins at zero.
arrayName[index];
C Program to Enter 5 Numbers and Print them in Reverse Order.
#include <stdio.h> int main() { int arr[5] = {7, 8, 1, 10, 6}; // one-dimensional array for (int i = 4; i >= 0; i--) { printf("%d ", arr[i]); // prints array element } return 0; }
Output
6 10 1 8 7
Explanation :
First, we’ve generated an array of 5 sizes with some initial values.
On that array, we will later traverse from the last index to the first index, which is 0.
Note: An array’s last index is equal to the array length – 1. Beginners commonly make the error of assuming that the last index is equal to 5 if the array size is 5, so this is a crucial lesson to learn.
We just have one statement inside the loop, and it prints the element which is the ith index.
Multi-Dimensional Array
A multi-dimensional array is an array that stores elements in a two-dimensional or higher-dimensional structure. It is also known as a two-dimensional or higher-dimensional array, and each element in the array can be accessed using multiple indices. Multi-dimensional arrays can be of different dimensions, including two-dimensional, three-dimensional, or higher-dimensional arrays.
Syntax for Multi-Dimensional Array Declaration
The syntax of a multidimensional array differs from a single-dimensional array only marginally.
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension].....[sizeof_nth_dimension];
//Example
int array[5][4][6][7];
Two-Dimensional Array
A multidimensional array with two dimensions is known as a two-dimensional array. A two-dimensional array is an array of arrays, where each element in the array is itself an array. It is also known as a matrix, and can be used to represent tables, grids, or other two-dimensional structures.
Syntax and Declaration of Two Dimensional Array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension];
//Example
int array2d[5][10];
5 rows and 10 columns make up the int type array known as array2d.
Initialization of Two-Dimensional Array
A list of the single-dimensional arrays in the correct syntax can be used to initialize a two-dimensional array,,
int numbers[5][2] = {
{0, 2, 4, 6, 8},
{1, 3, 5, 7, 9}
};
Example of 2-dimensional array
Accessing Individual Elements of Two-Dimensional Array
Accessing a certain element requires both the array’s row and column indices,
arrayName[rowIndex][columnIndex];
Passing the Entire 2D Array
There are various ways to pass various types of arrays to a function in C; a few of them are listed below. These methods can be used to pass a 2D array to a function.,
1. When Row and Column Both Sizes are Globally Available
Note: The row size in the function parameter can also be skipped.
#include <stdio.h> int M = 3; int N = 3; //These M and N can also be declared as macros void print(int arr[M][N]) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) printf("%d ", arr[i][j]); printf("\n"); } } int main() { //Initialization of array int arr[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Call the function by passing an array print(arr); return 0; }
Output
1 2 3
4 5 6
7 8 9
Explanation :
Everything in this example is rather straightforward; for instance, we initialise an array before looping through it to print its elements.
Pay close attention to how we send the array to the function, for example.
The array’s rows and columns are accessible from everywhere, and we are sending the array name to the function call.
2. When Column Size is Globally Available
When receiving the parameter in the function definition, we can omit the Row Size.
#include <stdio.h> int N = 3; //Function to print the array elements void print(int arr[][N], int M) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) printf("%d ", arr[i][j]); printf("\n"); } } int main() { //Initialize Array int arr[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Call function print(arr, 3); return 0; }
Output
1 2 3
4 5 6
7 8 9
Explanation :
While receiving the parameter in the function definition, we can omit the row size, but the column size is required.
To learn about the rows in the array inside the function, we have nevertheless passed the row as a second parameter.
By doing this, we may design a function that eliminates the requirement for globally defining an array’s rows.
3. Using Pointer Approach
During the function call, a pointer from the function definition can be used to access the provided array’s memory.
#include <stdio.h> //Function to print the array elements void print(int * arr, int M, int N) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) printf("%d ", *((arr + i * N) + j)); printf("\n"); } } int main() { //Initialize Array int arr[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //We are doing implicit conversion while calling print((int*)arr, 3, 3); return 0; }
Output
1 2 3
4 5 6
7 8 9
Explanation :
In this instance, we are calling the function while giving the pointer to the array.
The base address of the array will be contained in that pointer, which may be used to access the full array.
We perform certain calculations to determine the correct index inside the printf command. By adding 1 to the base address, we are just finding all addresses.
The ideal technique to pass an array to the function is in this manner. because for defining the function, there is no dependence on the array’s size.
Three-Dimensional Array
A three-dimensional array is a type of multi-dimensional array that stores elements in a three-dimensional structure. It is often used to represent three-dimensional structures such as cubes, rectangular prisms, or other geometric shapes. In a three-dimensional array, each element is accessed using three indices, one for the depth or "layer", one for the row, and one for the column.
Syntax for Declaration of Three Dimensional Array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension];
//Example
int array[5][10][15];
C program to show the concept of a three-dimensional array.
#include <stdio.h> int main() { int array[2][2][2]; int i, j, k; for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { scanf("%d", & array[i][j][k]); } } } printf("Print the Array\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) printf("%d ", array[i][j][k]); printf("\n"); } } return 0; }
Input
1 2 3 4 5 6 7 8
Output
Print the Array
1 2
3 4
5 6
7 8
Representation :
Conclusion
In conclusion, we can classify arrays based on their dimensions, with single-dimensional arrays storing elements in a linear sequence and multi-dimensional arrays storing elements in a two-dimensional or higher-dimensional structure. Understanding the different types of arrays and how to use them effectively is an important skill for any programmer. In this article, we discussed types of arrays in detail. Now, you have to focus and practice more to become an expert in the topic array.
FAQs on Types of Arrays
Here are some frequently asked questions on types of arrays.
Q1: What is the syntax for declaring a three-dimensional array in Java?
Answer: The syntax for declaring a three-dimensional array in Java is:
datatype[][][] array_name = new datatype[depth][row_size][col_size];
Q2: What is the difference between a jagged array and a rectangular array?
Answer: A jagged array is an array of arrays where each sub-array can have a different number of elements, while a rectangular array is a multi-dimensional array where each row has the same number of elements.
Q3: What is the disadvantage of using a multi-dimensional array in programming?
Answer: Multi-dimensional arrays can be more difficult to work with than single-dimensional arrays because they require multiple indices to access elements.
Q4: What is a sparse array in programming?
Answer: A sparse array is an array where most of the elements are empty or zero, and only a few elements contain non-zero values. Sparse arrays are used to optimize memory usage in cases where most of the elements are not needed.
Q5: What are some common applications of arrays in programming?
Answer: Arrays are used in programming for a wide range of applications, such as sorting algorithms, matrix operations, graphics processing, and scientific computing, among others.