Uploaded by reddfilter

Two-Dimensional Arrays in C: Matrix Addition Tutorial

advertisement
Two-Dimensional Array in C
1. Two Dimensional Array in Detail with Matrix Addition Example
A two-dimensional array (often called a matrix) is an array of arrays. It is used to represent
a table, grid, or matrix where data is stored in rows and columns. Each element is accessed
using two indices: one for the row and one for the column.
Matrix Representation: In a two-dimensional array, the rows and columns define the
structure. For example, a 2x3 matrix would have 2 rows and 3 columns.
Matrix Addition Example:
Let’s say we have two matrices A and B, and we want to add them. The rule for matrix
addition is that the two matrices must have the same dimensions. In matrix addition,
corresponding elements of both matrices are added together.
Matrix A:
A = [1 2 3]
[4 5 6]
Matrix B:
B = [7 8 9]
[10 11 12]
Matrix Addition (A + B):
C = [A[0][0] + B[0][0] A[0][1] + B[0][1] A[0][2] + B[0][2]]
[A[1][0] + B[1][0] A[1][1] + B[1][1] A[1][2] + B[1][2]]
So:
C = [1+7 2+8 3+9]
[4+10 5+11 6+12]
Result:
C = [8 10 12]
[14 16 18]
Code Example (C):
#include <stdio.h>
int main() {
int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
int B[2][3] = {{7, 8, 9}, {10, 11, 12}};
int C[2][3]; // Resultant matrix
// Matrix Addition
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Print the result
printf("Matrix C (A + B):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Matrix C (A + B):
8 10 12
14 16 18
2. Define Two Dimensional Array with an Example
A two-dimensional array is an array of arrays. It represents data in rows and columns, and
it can be thought of as a matrix. Each element in a two-dimensional array is accessed using
two indices: one for the row and one for the column.
Syntax:
data_type array_name[row_size][column_size];
Example:
#include <stdio.h>
int main() {
// Define a 2x3 two-dimensional array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Accessing elements
printf("Element at position (0, 0): %d\n", matrix[0][0]); // 1
printf("Element at position (1, 2): %d\n", matrix[1][2]); // 6
return 0;
}
Output:
Element at position (0, 0): 1
Element at position (1, 2): 6
3. What is Meant by Array? Write a Syntax for One-Dimensional Array
An array is a collection of elements, typically of the same data type, stored in contiguous
memory locations. The array allows efficient storage and retrieval of data. In C, arrays can
be used to store data in a structured way, where each element is identified by its index.
One-Dimensional Array:A one-dimensional array is a linear collection of elements, all of the
same type, stored in consecutive memory locations. The elements can be accessed using a
single index.
Syntax:
data_type array_name[array_size];
Example:
#include <stdio.h>
int main() {
// Define a one-dimensional array of 5 integers
int arr[5] = {1, 2, 3, 4, 5};
// Accessing array elements
printf("First element: %d\n", arr[0]); // 1
printf("Last element: %d\n", arr[4]); // 5
return 0;
}
Output:
First element: 1
Last element: 5
Download