In general, I already thought it was all working, but it turned out wrong. Need to re-code

Code:

int suma(int **math, int m, int n) { int x1, x2, summa = 0; for (x1 = 0; x1 < m - 1; x1++) { for (x2 = 0; x2 < n - 1; x2++) { if ((x1 % 2 == 0) && (x2 % 2 == 0)) { summa = summa + math[x1][x2]; } } } printf("Summa: %d", summa); } 

The sum of elements, both indices of which are even. But for some reason wrongly believes. I thought the idea is true, since the index looks and if it is divided into two without a trace, then we consider it. For example, [2] [4] we take the element of the matrix in the sum, but the element [1] [2] does not take. Where is the mistake? n-1 took, as C, numbering from scratch. Think so.

Code:

 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 = n - 1; k > 0; k--) { for (x2 = 0; x2 < k; x2++) { if (math[x1][x2] > math[x1][x2 + 1]) { s = math[x1][x2]; math[x1][x2] = math[x1][x2 + 1]; math[x1][x2 + 1] = s; } } } } for (x1 = 0; x1 < m; x1++) { for (x2 = 0; x2 < n; x2++) { printf(" %d", math[x1][x2]); } printf("n"); } } 

Organize strings in a non-descending order of their largest elements. It does not work this way, the elements are sorted in rows , not the lines themselves. Therefore, you need to adjust to sort the lines themselves.

Example

1 2 7 4 3 5 2 0 9

Must issue

4 3 5 //наибольший элСмСнт 5 1 2 7 //наибольший элСмСнт 7 2 0 9 //наибольший элСмСнт 9

And now gives out

1 2 7 3 4 5 0 2 9

Help correct the code.

  • The answer was never received. I asked the question to correct the code, and did not write that it is bad or good, I just study, so I can be understood - wapdimon72ru
  • one
    @ wapdimon72u, did you limit the cycle limits when calculating the amount? And it did not work out? In the sorting rearrange the entire line. Write the function swapstr (int ** a, int N, int M, int str1, str2) - avp
  • Yes, it was true, it was I correct it, everything works as it should, but tell me what is notfin = 1; what is it responsible for? and what w is responsible for, what is it? - wapdimon72ru
  • About notfin is in response, a sign that the array is not yet sorted. w is just an auxiliary variable for exchanging values, because Python a, b = b, a not provided in C / C ++. Similarly, w1. - alexlz
  • Like added. - alexlz

1 answer 1

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.

  • The answer (who made it?) Supplemented - alexlz
  • The sum. The elements of the whole matrix are counted, the synumbering is from zero, therefore it is even considered to count [1] [1] [1] [3] and so on, and about sorting, this code does the same thing as mine, that is, it sorts the lines in the rows, NOT the lines themselves - wapdimon72ru
  • As for even / odd - you just need to change the initial values ​​of x1 = 1 and x2 = 1. As for sorting, now db. sorting lines in non-decreasing maximum email. bubble method. Corrections in the answer - alexlz
  • maxelems = malloc (sizeof (int *) * m); on this line that swears, does not want to compile - wapdimon72ru
  • Does swear or decent words? First, there is a type error. Secondly, if you have c ++ (although in this case malloc is better not to use - non-kosher, better new / delete) and there is no #include <stdlib.h> then it will be cursed at the undeclared (no prototype) function malloc (for c ++ it is better maxelems = new int[m]; and delete [] maxelems; instead of free). - alexlz