Two Dimensional Array in C
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure.A two-dimensional array in C is essentially an array of arrays, or a matrix. It is a data structure that allows you to store data in a tabular form, with rows and columns. In C, a two-dimensional array is declared and used as follows:
Here's a breakdown of the components:cdata_type array_name[rows][columns];
1. data_type: This is the type of data that the array will store, such as int, float, char, etc.
2. array_name: This is the name you give to the array.
3. rows: This specifies the number of rows in the 2D array.
4. columns: This specifies the number of columns in the 2D array.
Two-dimensional array example in C
c#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Printing the contents of the 2D array
printf("Contents of the 2D array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
This code declares and initializes a 3x3 integer array named matrix. Then, it uses nested loops to print the contents of the array. When you run this program.Output:
cContents of the 2D array:
1 2 3
4 5 6
7 8 9
Declaration of two dimensional Array in C
To declare a two-dimensional array in C, you use the following syntax:
Here's a breakdown of each component:
1. data_type: This is the data type of the elements you want to store in the array, such as int, float, char, or any other data type.
2. array_name: This is the name you give to the array.
3. rows: This specifies the number of rows in the 2D array.
4. columns: This specifies the number of columns in the 2D array.
Here's an example of how to declare a few different types of two-dimensional arrays:
cdata_type array_name[rows][columns];
Here's a breakdown of each component:
1. data_type: This is the data type of the elements you want to store in the array, such as int, float, char, or any other data type.
2. array_name: This is the name you give to the array.
3. rows: This specifies the number of rows in the 2D array.
4. columns: This specifies the number of columns in the 2D array.
Here's an example of how to declare a few different types of two-dimensional arrays:
cint integerArray[3][4]; // A 3x4 array of integers.
double doubleArray[2][2]; // A 2x2 array of doubles.
char charArray[5][10]; // A 5x10 array of characters.
You can also declare and initialize a two-dimensional array in one step:cint matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
This creates a 2x3 integer array and initializes it with the provided values. You can access and manipulate the elements of the array as shown in my previous responseInitialization of 2D Array in C
- Initializing a 2D array during declaration:
cint matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
This creates a 3x3 integer array named matrix and initializes it with the provided values.- Initializing a 2D array by assigning values to individual elements:
cint matrix[3][3]; // Declare a 3x3 integer array
// Assign values to elements
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
Both methods will give you the same result – a 3x3 array with the same values. The method you choose depends on your specific needs and coding style. The first method is often more convenient for initializing small arrays with known values, while the second method is useful when you need to read data from external sources or when values are generated dynamically.2D array in C example: Storing elements in a matrix and printing it:
c#include <stdio.h>
int main() {
int rows, columns;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);
int matrix[rows][columns];
// Input elements into the matrix
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("Enter element at row %d, column %d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Print the matrix
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Sample Input:
sqlEnter the number of rows: 3
Enter the number of columns: 3
Enter elements of the matrix:
Enter element at row 1, column 1: 1
Enter element at row 1, column 2: 2
Enter element at row 1, column 3: 3
Enter element at row 2, column 1: 4
Enter element at row 2, column 2: 5
Enter element at row 2, column 3: 6
Enter element at row 3, column 1: 7
Enter element at row 3, column 2: 8
Enter element at row 3, column 3: 9
Output:
makefileMatrix:
1 2 3
4 5 6
7 8 9
This program allows you to input the number of rows and columns and then enter the elements of the matrix. Afterward, it prints the matrix in a tabular format as shown in the output above.
0 Comments