Advertisement
Lisaveta777

100.1 (2D array)

Nov 20th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define SIZE 2
  4. #define SIZE2 5
  5. //100. PASSING 2D ARRAY INTO FUNCTION - BETTER TO PASS 1D ARRAY //INSTEAD!
  6. //С помощью функции заполнить матрицу случайными числами. Написать функцию,
  7. // выполняющую сортировку строк матрицы по возрастанию.
  8. //Вывести на экран две матрицы: до и после сортировки (используя функцию).
  9.  
  10. void pop_ar(int s,int s2,int a[s][s2])
  11. {
  12.     int i,j;
  13.     for(i=0;i<SIZE;i++)
  14.     {
  15.         for(j=0;j<SIZE2;j++)
  16.         {
  17.             a[i][j] = rand()%20+1;
  18.         }
  19.     }
  20. }
  21. void pr_ar(int s,int s2,int a[s][s2])
  22. {
  23.     int i,j;
  24.     for(i=0;i<SIZE;i++)
  25.     {
  26.         for(j=0;j<SIZE2;j++)
  27.         {
  28.             printf("%d\t",a[i][j] );
  29.         }
  30.         printf("\n");
  31.     }
  32. }
  33. void sort_ar(int s,int s2,int a[s][s2])
  34. {
  35.     int i,j,k,temp;
  36.     for(i=0;i<SIZE;i++)
  37.     {
  38.         //printf("\ni %d\t",i);
  39.         for(j=0;j<SIZE2-1;j++)
  40.         {
  41.             //printf("\nj %d\t",j);
  42.             for(k=j+1;k<SIZE2;k++)
  43.             {
  44.                 //printf("k is %d\t",k);
  45.                 if(a[i][k]<a[i][j])
  46.                 {//swaps them
  47.                     printf("a[i][k] is %d, a[i][j] is %d\n",a[i][k],a[i][j]);
  48.                     temp = a[i][k];
  49.                     a[i][k] = a[i][j];
  50.                     a[i][j] = temp;
  51.  
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     int arr[SIZE][SIZE2];
  61.     pop_ar(SIZE,SIZE2,arr);
  62.     pr_ar(SIZE,SIZE2,arr);
  63.     sort_ar(SIZE,SIZE2,arr);
  64.     pr_ar(SIZE,SIZE2,arr);
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement