-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.c
131 lines (115 loc) · 3.04 KB
/
01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void choosepath(int pathweight[][10], int row, int column, int arr[10][10]);
int main() {
int i, j, row, column,element,index=0,x;
int arr[10][10] = { 0 };
int pathweight[10][10]= { 0 };
printf("Bengisu Sahin || 152120191064 || [email protected]\n");
printf("First Semester || Final\n\n");
printf("Enter number of rows (0 to exit): ");
scanf_s("%d", &row);
printf("Enter number columns: ");
scanf_s("%d", &column);
if (row == 0 || column== 0) {
printf("You exit the program.\n");
}
else if (row < 0 || column < 0) {
printf("You entered inappropriate value.Your process is end.\n");
}
else{
srand(time(NULL));
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++) {
element = rand() % 10;
arr[i][j] = element;
}
}
for (j = 0; j < column; ++j) {
pathweight[0][j] = arr[0][j];
}
for (i = 1; i < row; i++) {
for (j = 0; j <= column-1; j++) {
index=j;
if(j!=0 && j != column-1 ){
if (pathweight[i - 1][j - 1] < pathweight[i - 1][index]) {
index = j - 1;
}
if (pathweight[i - 1][j + 1] < pathweight[i - 1][index]) {
index = j + 1;
}
}
if (j == 0) {
if (pathweight[i - 1][j] < pathweight[i - 1][j + 1])
{
index = j;
}
else {
index = j + 1;
}
}
if (j == column - 1) {
if (pathweight[i - 1][j] < pathweight[i - 1][j - 1])
{
index = j;
}
else {
index = j - 1;
}
}
x= arr[i][j] + pathweight[i - 1][index];
pathweight[i][j] = x;
}
}
choosepath(pathweight, row, column,arr);
}
system("pause");
}
void choosepath(int pathweight[][10], int row, int column, int arr[10][10]) {
void display(int temp[10][10], int arr[10][10]);
int i,k=row,j,nomcol=0,temp[10][10];
row = row - 1;
for (j = 0; j < column - 1; j++) {
if (pathweight[row][j+1] < pathweight[row][nomcol])
nomcol = j+1;
}
temp[row][nomcol] = 99;
for (row-1; row > 0; row--)
{
if(nomcol!=0 && nomcol!=column-1) {
if (pathweight[row][nomcol] == arr[row][nomcol] + pathweight[row-1][nomcol + 1])
nomcol = nomcol + 1;
if (pathweight[row][nomcol] == arr[row][nomcol] + pathweight[row-1][nomcol - 1] )
nomcol = nomcol - 1;
}
if (nomcol == 0) {
if (arr[row][nomcol] + pathweight[row-1][nomcol + 1] == pathweight[row][nomcol])
nomcol = nomcol + 1;
}
if (nomcol == column-1) {
if (arr[row][nomcol] + pathweight[row-1][nomcol - 1] == pathweight[row][nomcol])
nomcol = nomcol - 1;
}
temp[row-1][nomcol] = 99;
}
display(temp, arr,k,column);
}void display(int temp[10][10], int arr[10][10],int k,int column) {
int i, j,sum=0;
for (i = 0; i <= k-1; i++) {
for (j = 0; j < column; j++) {
if (temp[i][j] == 99) {
printf("[");
printf(" %d ", arr[i][j]);
printf("]");
sum += arr[i][j];
}
else {
printf(" %d ", arr[i][j]);
}
}
printf("\n");
}
printf("\n");
printf("Sum of weights: %d\n", sum);
}