Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

60 строки
1.4 KiB
C

#include <stdio.h>
#define MAX_ROWS 100
#define MAX_COLS 100
int main() {
int n, m;
int A[MAX_ROWS][MAX_COLS];
// Input matrix dimensions
printf("Enter the number of rows (n): ");
scanf("%d", &n);
printf("Enter the number of columns (m): ");
scanf("%d", &m);
// Input matrix elements
printf("Enter the elements of matrix A (%d x %d):\n", n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &A[i][j]);
}
}
// Replace zero elements
for (int j = 0; j < m; j++) {
int firstNonZero = -1;
// Find the first non-zero element in the column
for (int i = 0; i < n; i++) {
if (A[i][j] != 0) {
firstNonZero = A[i][j];
break;
}
}
// Replace zero elements with the found first non-zero value
if (firstNonZero != -1) { // If a non-zero element was found
for (int i = 0; i < n; i++) {
if (A[i][j] == 0) {
A[i][j] = firstNonZero;
}
}
}
}
// Output modified matrix
printf("Modified matrix A:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("Press any key to exit...\n");
getchar();
getchar();
return 0;
}