Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- void Taomang(int **&arr, int &n, int &m)
- {
- srand(time(NULL));
- n = rand() % 9 + 1;
- m = rand() % 9 + 1;
- arr = (int **)malloc(n * sizeof(int *));
- for (int i = 0; i < n; i++)
- arr[i] = (int *)malloc(m * sizeof(int));
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- arr[i][j] = rand() % 99 + 1;
- }
- }
- }
- void InMang(int **arr, int n, int m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- printf("%d\t", arr[i][j]);
- }
- printf("\n");
- }
- printf("\n%d Hang\t%d Cot\n", n, m);
- }
- //sap xep trong dong
- void Swap(int *a, int *b)
- {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- void Sort(int *arr, int n)
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (arr[i] > arr[j])
- Swap(&arr[i], &arr[j]);
- }
- }
- }
- void Sort1(int **arr, int n, int index)
- {
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (arr[i][index] > arr[j][index])
- Swap(&arr[i][index], &arr[j][index]);
- }
- }
- }
- void InMang_1(int *arr, int n)
- {
- for (int i = 0; i < n; i++)
- printf("%d\t", arr[i]);
- printf("\n");
- }
- void InCot(int **arr, int n, int index)
- {
- for (int i = 0; i < n; i++)
- {
- printf("%d\t", arr[i][index]);
- }
- printf("\n");
- }
- void Sort2(int **arr, int n, int m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- for (int h = 0; h < n; h++)
- {
- for (int k = 0; k < m; k++)
- {
- if (arr[i][j] < arr[h][k])
- Swap(arr[i][j], arr[h][k]);
- }
- }
- }
- }
- }
- void main()
- {
- int **arr = NULL;
- int n, m;
- Taomang(arr, n, m);
- InMang(arr, n, m);
- //Sap xep theo hang
- //for (int i = 0; i < n; i++)
- //{
- // Sort(arr[i], m);
- // InMang_1(arr[i], m);
- //}
- //Sap xep theo cot
- //Sort1(arr, n, 0);
- //InCot(arr, n, 0);
- //Sap xep mang 2 chieu tang dan
- Sort2(arr, n, m);
- InMang(arr, n, m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement