Matrix Operations (Dynamic 2D)
Manages a dynamic 2D array (matrix) allowing the user to increase or decrease the number of rows.
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("INSTRUCTIONS: Enter dimensions and elements, then choose to increase/decrease rows.\n");
int **matrix, rows, cols, i, j;
// Initialize the 2D array
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
matrix = (int **)malloc(rows * sizeof(int *));
for (i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Initialize the elements of the matrix
printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Display the original matrix
printf("Original Matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Increase or decrease the number of subarrays
int choice;
printf("\nEnter 1 to increase the number of subarrays, 2 to decrease: ");
scanf("%d", &choice);
if (choice == 1) {
// Increase the number of subarrays
int newRows;
printf("Enter the number of additional rows: ");
scanf("%d", &newRows);
matrix = (int **)realloc(matrix, (rows + newRows) * sizeof(int *));
for (i = rows; i < rows + newRows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
rows += newRows;
} else if (choice == 2) {
// Decrease the number of subarrays
int removeRows;
printf("Enter the number of rows to remove: ");
scanf("%d", &removeRows);
if (removeRows <= rows) {
matrix = (int **)realloc(matrix, (rows - removeRows) * sizeof(int *));
rows -= removeRows;
} else {
printf("Error: Cannot remove more rows than present.\n");
}
} else {
printf("Invalid choice.\n");
}
// Display the modified matrix
printf("\nModified Matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Deallocate memory
for (i = 0; i < rows; i++)
free(
matrix[i]);
free(matrix);
return 0;
}Matrix Operations (Dynamic 2D) — Free C Code Example
Manages a dynamic 2D array (matrix) allowing the user to increase or decrease the number of rows. This free code example is available in C and can be copied and run immediately — no account or signup required. Use the language tabs above to switch between implementations.
How to Use This Code
- Select your language — click the language tab (C, Java, C++, or Python) above the code viewer.
- Copy the code — use the copy button in the top-right corner of the code block.
- Run it — paste into a file with the correct extension (.c, .java, .cpp, .py), compile, and run using your terminal or IDE.
- Follow the on-screen instructions in the terminal to provide any required inputs.
Frequently Asked Questions
What does this program do?
Manages a dynamic 2D array (matrix) allowing the user to increase or decrease the number of rows.
Which languages is it available in?
Available in C. Switch between implementations using the tabs at the top of the code viewer.
How do I run this code?
Copy the code, save it with the right file extension (.c, .java, .cpp, or .py), then compile or run it with your installed compiler or interpreter (GCC, JDK, G++, Python 3).
Is this code free?
Yes — free to view, copy, and use. No account required.