Relation between Pointers and Arrays

Q: Briefly discuss the relation between pointers and arrays, giving suitable example. Write a program in C, to print transpose of a 2D matrix entered by a user. Also give comments  

Relation Between Pointers and Arrays in C:

In C, arrays and pointers are closely related because the name of an array represents the address of the first element of the array. An array's name can be used as a pointer to the first element, and you can access array elements using pointer arithmetic.

Key Points:

  1. Array Name as Pointer: The name of an array is a constant pointer to the first element.
    • arr in int arr[3] is equivalent to &arr[0].
  2. Pointer Arithmetic: Using pointers, you can traverse arrays by incrementing or decrementing the pointer.
    • *(arr + i) is equivalent to arr[i].
  3. Accessing Array Elements: Both array indexing and pointer dereferencing can be used to access the elements of the array.
Example:
#include 
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr; // Pointer to the first element of arr

printf("Using pointer to access array elements:\n");

// Access elements using pointer arithmetic
for (int i = 0; i < 3; i++) {
printf("Element %d: %d\n", i, *(ptr + i)); // *(ptr + i) is equivalent to arr[i]
}
return 0;
}

Output:

Using pointer to access array elements: Element 0: 10 Element 1: 20 Element 2: 30

In the above code:

  • ptr is a pointer to the first element of arr.
  • We access each element of the array using pointer arithmetic, i.e., *(ptr + i).

C Program to Print the Transpose of a 2D Matrix:

Transpose of a Matrix: The transpose of a matrix is obtained by swapping its rows and columns. For example, the element at position [i][j] in the original matrix becomes the element at position [j][i] in the transposed matrix.

Code to Print Transpose of a 2D Matrix:

#include 
#define MAX 10  // Maximum size of the matrix
// Function to print the matrix
void printMatrix(int matrix[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows> for (int j = 0; j < cols> printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
// Function to compute the transpose of the matrix
void transposeMatrix(int matrix[MAX][MAX], int transpose[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows> for (int j = 0; j < cols> transpose[j][i] = matrix[i][j]; // Swap rows and columns
}
}
}
int main() {
int matrix[MAX][MAX], transpose[MAX][MAX];
int rows, cols;

// Input the matrix dimensions
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

// Input the matrix elements
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows> for (int j = 0; j < cols> scanf("%d", &matrix[i][j]);
}
}

// Display the original matrix
printf("\nOriginal Matrix:\n");
printMatrix(matrix, rows, cols);

// Compute the transpose of the matrix
transposeMatrix(matrix, transpose, rows, cols);

// Display the transposed matrix
printf("\nTranspose of the Matrix:\n");
printMatrix(transpose, cols, rows); // Transposed matrix has flipped dimensions

return 0;
}
Explanation:
  1. Input the Matrix: The user is prompted to enter the dimensions (rows and columns) of the matrix, followed by the matrix elements.
  2. Transpose Calculation: The transposeMatrix() function swaps rows and columns of the input matrix.
    • The element at position [i][j] in the original matrix is moved to [j][i] in the transposed matrix.
  3. Printing the Matrices: The printMatrix() function is used to print both the original and transposed matrices.
Example:

Input:

Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of the matrix: 1 2 3 4 5 6

Output:

Original Matrix: 1 2 3 4 5 6 Transpose of the Matrix: 1 4 2 5 3 6

Summary:

  • This program demonstrates the use of arrays to store 2D matrices.
  • The transpose of the matrix is achieved by swapping the rows and columns.
  • The program makes use of nested loops to handle both input and the transpose operation.

  • To Share this Blog, Choose your plateform


Write your Testimonial

Your review is very precious for us.


Rating: