2D Dimensional Array in c programming

09 Dec 2022 Balmiki Mandal 0 C Programming

Mastering 2D Arrays in C: A Step-by-Step Tutorial

A 2D dimensional array in C programming is an array that contains multiple rows and columns. It is a collection of data elements of the same data type arranged in a two-dimensional rectangular grid.

  • The 2D array also called an array of array 
  • Also Called a Collection of 1D Array 
  • Also called Matrix

Syntax : Datatype Variable_name[ r ] [ c ]

  [ r ] = Number of 1D array

  [ C ] = In Each 1D array number of Elements

For example, to declare a 2D array of integers with 3 rows and 4 columns, you would use the following code:

C Programming
int array[3][4];

To initialize a 2D array, syntax:

C Programming
array_name[row_index][column_index] = value;

For example, to initialize the first element of the array declared above to the value 10, you would use the following code:

C Programming
array[0][0] = 10;

To access an element of a 2D array, you use the following syntax:

C Programming
array_name[row_index][column_index]

For example, to access the element at row 1, column 2 of the array declared above, you would use the following code:

C Programming
int element = array[1][2];

 2D arrays can be used to store a variety of data, such as:

  • Images
  • Matrices
  • Spreadsheets
  • Databases
  • Game maps

They can also be used to implement a variety of algorithms, such as sorting and searching.

complete program that demonstrates how to use a 2D array in C:

C Programming
#include <stdio.h>

int main() {
  int array[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

  // Print the elements of the array
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }

  return 0;
}
Output:
1 2 3 4
5 6 7 8
9 10 11 12

Further Reading:


2D Dimensional Array in c programming

Write a C Program to reverse 2D array Elements

Integer 2D array Passing to function in c

String 2D array to a Function in c

Write a C program to short 2D array using bubble sort

c program to allocate dynamic memory for 2D array integers

Using Arrays of Pointers to Represent Two-Dimensional Arrays

How to Calculate of sub element of 2D array and How to print it in c

Write a C Program to scan 5 Elements and print on the screen using 2D array

Assingement of 2D array in c

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.