Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Program to Transpose a 2D Array.

 

#include <stdio.h>

// Function to transpose a 2D array
void transposeArray(int arr[][3], int rows, int cols) {
int transposed[cols][rows];

// Performing the transpose operation
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = arr[i][j];
}
}

// Printing the transposed array
printf(“Transposed array:n”);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf(“%d “, transposed[i][j]);
}
printf(“n”);
}
}

int main() {
int rows, cols;

// Taking input for the number of rows and columns
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);

printf(“Enter the number of columns: “);
scanf(“%d”, &cols);

int array[rows][cols];

// Taking input for array elements
printf(“Enter %d x %d elements:n”, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf(“%d”, &array[i][j]);
}
}

// Calling the function to transpose the array and printing the result
transposeArray(array, rows, cols);

return 0;
}