Types Of Array

In this article, we are going to learn about types of array. Arrays are one of the most interesting topics in the coding world. Array is basically the collection of similar data types. We will see types of array and how types of array are different from each other.

Classification Of Array

Based on their dimensions, arrays can be divided into single-dimensional and multidimensional varieties. A two-dimensional array logically represents a mathematical matrix, while a single-dimensional array corresponds to a linear collection of data. A multidimensional array has several dimensions, too.

All elements in the memory are stored in a linear order so that allows us to access the array dimension-wise. It is simply a concept called memory addressing

Single Dimensional Array

One of the most popular types of arrays in C is the one-dimensional array. It is a linear collection of related data types, and all of the single-dimensional array’s allocated memory is allocated in consecutive blocks.

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

An improved single-dimensional array that supports multilevel nesting is a multidimensional array. A different way to look at it is as an array of an array with one less dimension, such as an array of a one-dimensional array is a two-dimensional array or an array of a two-dimensional array is a three-dimensional array.

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. In C programming, it is frequently used to represent a matrix in a data structure across all types of arrays.

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

As a specific type of multidimensional array, three-dimensional arrays are also used to represent three-dimensional coordinates in programming. Another way to put it is that a three-dimensional array is a collection of two-dimensional arrays.

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 this article, we had discussed about types of array in c in detail. Array is one of the most important topics if you see the scenario of interviews. Now, you have to focus and practice more to become an expert in the topic array.

Leave a Reply

Your email address will not be published. Required fields are marked *