Created
October 27, 2018 20:47
-
-
Save cyberlex404/646ed09a5d75df4dc43118115ca4be70 to your computer and use it in GitHub Desktop.
loop matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <conio.h> | |
| #include <stdlib.h> | |
| int isSort(int* line, int m); | |
| int compare(const void * x1, const void * x2); | |
| void main() { | |
| int array[100][100]; | |
| int n, m, i, j, k = 0; | |
| printf("Vvedite kolichestvo strok i stolbcov matrici: "); | |
| scanf_s("%d", &n); | |
| scanf_s("%d", &m); | |
| if (n <= 100 && m <= 100) | |
| { | |
| printf("Vvedite elementy:\n"); | |
| for (i = 0; i < n; i++) | |
| for (j = 0; j < m; j++) | |
| { | |
| printf("Vvedite element [%d,%d]\n", i + 1, j + 1); | |
| //scanf_s("%f", &array[i][j]); | |
| // к чертям рандомное заполнение матрицы. (можно поколдовать и создать "последовательный рандом") | |
| // от число от 1000 до 991; число от 990 до 981 и тд | |
| // заполнять матрицу от руки больче чем 2 на 2 можно до утра через scanf_s() | |
| array[i][j] = rand() % 100; | |
| } | |
| } | |
| // обходим все строки матрицы | |
| for (i = 0; i < n; i++) { | |
| if (!isSort(array[i], m)) { | |
| // строка матрицы не упорядочина по убыванию | |
| printf("stroka %d matricy ne uporyadocina \n", i); | |
| // упорядочиваем одномерный массив | |
| qsort(array[i], m, sizeof(int), compare); | |
| break; // принудительно выходим из цикла после нахождения строки | |
| } | |
| } | |
| _getch(); | |
| } | |
| // вернет 1 если все элементы от 0 до m отсортированы по убыванию. | |
| // Либо вернет 0 если найдет a[i] < a[i+1] | |
| // функция принимает массив и его длину | |
| int isSort(int* line, int m) { | |
| for (int i = 0; i < m; i++) { | |
| if (line[i] < line[i + 1]) { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| // функция сравнения элементов массива | |
| int compare(const void * x1, const void * x2) { | |
| // если результат вычитания равен 0, то числа равны, < 0: x1 < x2; > 0: x1 > x2 | |
| return (*(int*)x2 - *(int*)x1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment