Untitled
#include <stdio.h> #include <stdlib.h> // Function to create a 2D array void CreateArray(int*** arr, int row, int col) { *arr = (int**)malloc(row * sizeof(int*)); for (int i = 0; i < row; i++) { (*arr)[i] = (int*)malloc(col * sizeof(int)); } } // Function to accept input from user void Accept(int** arr, int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf("Enter element [%d][%d]: ", i, j); scanf("%d", &arr[i][j]); } } } // Function to display the elements of the array void Display(int** arr, int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf("%d ", arr[i][j]); } printf("\n"); } } // Function to calculate the sum of all elements void CalculateSum(int** arr, int row, int col) { int sum = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { sum += arr[i][j]; } } printf("Sum of all elements: %d\n", sum); } // Function to calculate the sum of each row void RowWiseSum(int** arr, int row, int col) { for (int i = 0; i < row; i++) { int sum = 0; for (int j = 0; j < col; j++) { sum += arr[i][j]; } printf("Sum of row %d: %d\n", i, sum); } } // Function to find the minimum and maximum elements void MinMax(int** arr, int row, int col) { int min = arr[0][0]; int max = arr[0][0]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (arr[i][j] < min) { min = arr[i][j]; } if (arr[i][j] > max) { max = arr[i][j]; } } } printf("Minimum element: %d\n", min); printf("Maximum element: %d\n", max); } // Function to transpose the array void Transpose(int** arr, int row, int col) { int** transposedArr = (int**)malloc(col * sizeof(int*)); for (int i = 0; i < col; i++) { transposedArr[i] = (int*)malloc(row * sizeof(int)); } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { transposedArr[j][i] = arr[i][j]; } } printf("Transposed array:\n"); for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { printf("%d ", transposedArr[i][j]); } printf("\n"); } } int main() { int row, col; printf("Enter number of rows: "); scanf("%d", &row); printf("Enter number of columns: "); scanf("%d", &col); int** arr; CreateArray(&arr, row, col); Accept(arr, row, col); Display(arr, row, col); CalculateSum(arr, row, col); RowWiseSum(arr, row, col); MinMax(arr, row, col); Transpose(arr, row, col); return 0; }
Leave a Comment