Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void transform(float** arr, float* v, int m, int n);
  5. void matInput(float **mas, int m, int n);
  6. void matOutput(float **mas, int m, int n);
  7. void swap(float *a, float *b);
  8. void cleanMas(float **mas, int m, int n);
  9. void matSort(float **mas, int m, int n);
  10. void reverse(float** mat, int m, int n);
  11.  
  12. int main(){
  13. int n, m; //размерность
  14. printf("Enter N and M: "); //ввод размерности и выделение памяти
  15.  
  16. do{
  17. scanf("%d%d", &n, &m);
  18. if (n < 0 || m < 0 )
  19. printf("wrong input! Try again: ");
  20. } while (n < 0 || m < 0);
  21.  
  22. float* v = new float [n * m];
  23. float** mas = new float* [m]; //выделение памяти под массив
  24. transform(mas, v, m, n);
  25.  
  26. matInput(mas, m, n);
  27. printf("\nYour matrix:\n");
  28. matOutput(mas, m, n);
  29. matSort(mas, m, n);
  30. reverse(mas, m, n);
  31. printf("New matrix:\n");
  32. matOutput(mas, m, n);
  33. cleanMas(mas, m, n); //освобождение памяти
  34. getch();
  35. return 0;
  36. }
  37.  
  38. void transform(float** arr, float* v, int m, int n)
  39. {
  40. for (int i = 0; i < m; i++)
  41. arr[i] = v + i*n;
  42. }
  43.  
  44. void matInput(float **mas, int n, int m)
  45. {
  46. printf("Enter your matrix:\n");
  47. for (int i = 0; i<n; i++)
  48. for (int j = 0; j<m; j++)
  49. scanf("%f", &mas[i][j]);
  50. }
  51.  
  52. void matOutput(float **mas, int m, int n)
  53. {
  54. for (int i = 0; i<m; i++)
  55. for (int j = 0; j<n; j++)
  56. if ( j == n-1)
  57. printf("%6.2f\n", mas[i][j]);
  58. else
  59. printf("%6.2f ", mas[i][j]);
  60. printf("\n");
  61. }
  62.  
  63. void swap(float *a, float *b)
  64. {
  65. float temp = *a;
  66. *a = *b;
  67. *b = temp;
  68. }
  69.  
  70. void cleanMas(float **mas, int m, int n)
  71. {
  72. delete []*mas;
  73. delete []mas;
  74. }
  75.  
  76. void matSort(float **mas, int m, int n)
  77. {
  78. for (int i = 0; i < m*n-1; i++)
  79. for (int j = i+1; j < m*n; j++)
  80. if (mas[0][i] > mas[0][j])
  81. swap(&mas[0][i], &mas[0][j]);
  82. }
  83.  
  84. void reverse(float** mas, int m, int n)
  85. {
  86. for (int i = 1; i < m; i+=2)
  87. for (int j = 0; j < n/2; j++)
  88. swap(&mas[i][j], &mas[i][n - j - 1]);
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement