I just wrote a program that finds the minimum sum path in a matrix by moving down or diagonally. The program works correctly, but I want to specify the summed numbers [] this way when I find the sum. How can I do this? I tried various things but couldn't..You can find the program I wrote below, thank you.
sample output of my program:
9 7 8 3 8
8 6 1 2 3
4 7 5 0 8
Minimum Sum:4
output that I want
9 7 8 [3] 8
8 6 [1] 2 3
4 7 5 [0] 8
Minimum Sum:4
#include <stdio.h>
#include <stdlib.h>
static int m, n;
int main()
{
int i, j;
do {
printf("Enter number of rows (0 to exit): ");
scanf("%d", &m);
if (m == 0) {
}
printf("Enter number of columns: ");
scanf("%d", &n);
int matrix[m][n];
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
matrix[i][j] = rand() % 10;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j], " ");
}
printf("
");
}
printf("Minimum sum:%d", minPath(matrix));
} while (1);
}
int Min(int x, int y)
{
return (x < y) ? x : y;
}
int minPath(int matrix[m][n])
{
int i, j;
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
if (j > 0 && j < n - 1)
matrix[i][j] += Min(matrix[i - 1][j],
Min(matrix[i - 1][j - 1], matrix[i - 1][j + 1]));
else if (j > 0)
matrix[i][j] += Min(matrix[i - 1][j], matrix[i - 1][j - 1]);
else if (j < n - 1)
matrix[i][j] += Min(matrix[i - 1][j], matrix[i - 1][j + 1]);
}
}
int res = 100000000;
for (j = 0; j < n; j++)
res = Min(matrix[m - 1][j], res);
return res;
}
question from:https://stackoverflow.com/questions/65672025/extra-requests-in-a-program-that-finds-its-minimum-sum-path