Here
for (x1 = 0; x1 < m - 1; x1++) { for (x2 = 0; x2 < n - 1; x2++) {
Why m-1 and n-1, but not m and n? And the sorting is very strange, some kind of Michurin hybrid of a bubble and (I don't know what is called) the oak itself.
Addition:
int suma(int **math, int m, int n) { int x1, x2, summa = 0; for (x1 = 0; x1 < m; x1 += 2) for (x2 = 0; x2 < n; x2 += 2) summa = summa + math[x1][x2]; printf("Summa: %d", summa); }
Amount. Only considering the elements math [0] [0] (the first element of the first line), math [2] [2] to be even or odd is another matter.
int sortirovka(int **math, int m, int n) { int x1, x2, k, s = 0; printf("n"); printf("tNew Matrix:nn"); for (x1 = 0; x1 < m; x1++) for (k = 0; k < n-1; k++) for (x2 = k+1; x2 < n; x2++) if (math[x1][k] > math[x1][x2]) { s = math[x1][x2]; math[x1][x2] = math[x1][k]; math[x1][k] = s; } for (x1 = 0; x1 < m; x1++) for (x2 = 0; x2 < n; x2++) { printf(" %d", math[x1][x2]); } printf("\n"); }
The sorting is the most oak (I do not remember the name, as I already said). The first element of the line is taken and compared with the rest. If more - mutually exchange. Further, with the second, third, etc. to [n-2]. The number of comparisons does not depend on the initial ordering of the array.
Check these edits - did not check.
Added:
int suma(int **math, int m, int n) { int x1, x2, summa = 0; for (x1 = 1; x1 < m; x1 += 2) for (x2 = 1; x2 < n; x2 += 2) summa = summa + math[x1][x2]; printf("Summa: %d", summa); } int sortirovka(int **math, int m, int n) { int x1, x2, k, s = 0, *maxelems, notfin; printf("n"); // ΠΏΠΎΠΈΡΠΊ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ
ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π² ΡΡΡΠΎΠΊΠ°Ρ
maxelems = (int *) malloc(sizeof(int)*m); // ΠΈΡΠΏΡΠ°Π²Π»Π΅Π½ΠΎ for(x1=0; x1 < m; x1++) { maxelems[x1] = math[x1][0]; for(x2=1; x2 < n; x2++) if(maxelems[x1] < math[x1][x2]) maxelems[x1] = math[x1][x2]; } printf("tNew Matrix:nn"); notfin = 1; // ΠΏΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° Π½Π΅ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Π° while(notfin) { notfin = 0; // ΡΠ±ΡΠΎΡ. ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΡ ΡΡΡΠΎΠΊ Π½Π΅ Π±ΡΠ΄Π΅Ρ, ΡΠΎ ΠΎΡΡΠ°Π½Π΅ΡΡΡ Π½ΡΠ»ΡΠΌ for (x1 = 0; x1 < m-1; x1++) if(maxelems[x1] > maxelems[x1+1]) { int *w = math[x1]; // Π²ΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½Π°Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° ΡΠΊΠ°Π·Π°ΡΠ΅Π»Π΅ΠΉ Π½Π° ΡΡΡΠΎΠΊΠΈ int w1 = maxelems[x1]; // Π²ΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½Π°Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ
Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ Π² ΡΡΡΠΎΠΊΠ°Ρ
. ΠΠΎΠ»ΠΆΠ½Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ°ΡΡΡΡ ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎ // math[x1] <-> math[x1+1] ΠΈ maxelems[x1] <-> maxelems[x1+1] math[x1] = math[x1+1]; math[x1+1] = w; maxelems[x1] = maxelems[x1+1]; maxelems[x1+1] = w1; notfin = 1; // ΠΎΠ±ΠΌΠ΅Π½ Π±ΡΠ». Π±ΡΠ΄Π΅Ρ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΉ ΠΏΡΠΎΡΠΌΠΎΡΡ ΠΌΠ°ΡΡΠΈΠ²Π° } } for (x1 = 0; x1 < m; x1++) for (x2 = 0; x2 < n; x2++) { printf(" %d", math[x1][x2]); } printf("\n"); free(maxelems); // ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΏΠ°ΠΌΡΡΠΈ ΠΌΠ°ΡΡΠΈΠ²Π° ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ
Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΠΏΠΎ ΡΡΡΠΎΠΊΠ°ΠΌ. }
There was an error in the type in the string
maxelems = malloc(sizeof(int*)*m);
d.b.
maxelems = (int *) malloc(sizeof(int)*m);
I can no longer comment, the limit has been reached. notfin - a sign that the sorting is not yet complete. When sorting the bubble, the neighboring values ββare compared and, if necessary, are swapped. After the first pass, the array is not yet sorted. We carry out the second, etc. A sign that the array is sorted is the lack of movement of values ββwhen browsing. Somewhere like that.
a, b = b, anot provided in C / C ++. Similarly, w1. - alexlz