Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #define row 2
- #define col 3
- int main()
- {
- int *matr, i, j, n, m, tmp;
- matr = malloc(row*sizeof(int*)); // Выделение памяти под указатели
- if(matr == NULL){
- printf("Memory is not allocated"); //проверка того выделилась ли память
- exit(0);
- }
- for(i = 0; i < row; i++){
- matr[i] = calloc(col, sizeof(int)); //выделение памяти под строки
- if(matr+i == NULL){
- printf("Memory is not allocated"); //проверка того выделилась ли память
- for(j = 0; j < i; j++) free(matr+j);
- free(matr);
- exit(0);
- }
- }
- for (i = 0; i < row; i++){ // Ввод элементов массива
- for(j = 0; j < col; j++){
- printf("matr[%d][%d] = ", i, j);
- if(scanf("%d", (matr + i*col + j)) != 1){
- printf("Error, enter digits\n");
- j--;
- fflush(stdin);
- }
- }
- }
- printf("Your matrix:\n");
- for(i = 0; i < row; i++){ //Вывод элементов массива
- for(j = 0; j < col; j++){
- printf("%5d ", *(matr + i*col + j));
- }
- printf("\n");
- }
- for(int k = 0; k < row; k++){
- for(i = 0; i < row; i++){
- for(j = 0; j < row; j++){
- if(*(matr+i*col+j) < *(matr+i*col+(j+1))){
- tmp = *(matr+i*col+j);
- *(matr+i*col+j) = *(matr+i*col+(j+1));
- *(matr+i*col+(j+1)) = tmp;
- }
- }
- }
- }
- printf("\nYour sorted matrix: \n");
- for(i = 0; i < row; i++){
- for(j = 0; j < col; j++){
- printf("%5d", *(matr + i*col + j));
- }
- printf("\n");
- }
- for(i = 0; i < row; i++){
- free(matr+i);
- }
- free(matr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement