2D Array in C Language
×


2D Array in C Language

103

 In C Language, a 2D array resembles a table, organized into rows and columns, with each cell capable of holding a value.

In 2D Array each row represent one 1D Array.

 This allows you to store and handle in a structured way, such as tables.

 Each cell represents an element of the 2D array.

 The numbers inside the cells represent the values stored in the array.

 Rows are represented horizontally, and columns are represented vertically.

 Rows and columns are indexed starting from 0.

 The array is organized into rows and columns, forming a grid-like structure.

Syntax of 2D Array declaration in C 

 data_type array_name [row_size] [column_size];
  data_type: specifies the type of elements that array will hold, eg(int, float, char).

  array_name: Represents the name of array.

  row_size: Indicates number of rows in the array.

 column_size: Indicates number of column in each row.

Program for 2D Array in C Language 

#include <stdio.h>
int main() {

// Declaration and initialization of a 2D array to store scores
int Marks[3][4] = {
{85, 90, 75, 88},
{78, 82, 80, 85},
{92, 88, 95, 90}

};

// Accessing and printing each score using nested loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("marks of student %d in subject %d is: %d\n", i + 1, j + 1, Marks[i][j]);
}
} return 0;
}

Output:

marks of student 1 in subject 1 is: 85
marks of student 1 in subject 2 is: 90
marks of student 1 in subject 3 is: 75
marks of student 1 in subject 4 is: 88
marks of student 2 in subject 1 is: 78
marks of student 2 in subject 2 is: 82
marks of student 2 in subject 3 is: 80
marks of student 2 in subject 4 is: 85
marks of student 3 in subject 1 is: 92
marks of student 3 in subject 2 is: 88
marks of student 3 in subject 3 is: 95
marks of student 3 in subject 4 is: 90


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments