Advertisement
deced

Untitled

Sep 5th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int InputN() {
  6. int n = -1;
  7. while ((n < 2) | (n > 15)) {
  8. printf("Введите размер[2..15] матрицы n*n: ");
  9. scanf("%d", &n);
  10. if ((n < 2) || (n > 15)) {
  11. printf("Ошибка!\n");
  12. }
  13. }
  14. return n;
  15. }
  16.  
  17. int CorrectElement(int i, int j) {
  18. int n = -100;
  19. while ((n < -99) || (n > 99)) {
  20. printf("[%d][%d] = ", i, j);
  21. scanf("%d", &n);
  22. }
  23. return n;
  24. }
  25. int* InputMatrix(int n) {
  26. int* arr = NULL;
  27. arr = (int*)malloc(sizeof(int) * n * n);
  28. for (int i = 0; i < n; i++) {
  29. printf("\n");
  30. for (int j = 0; j < n; j++) {
  31. arr[i * n + j] = CorrectElement(i, j);
  32. }
  33. }
  34. return arr;
  35. }
  36.  
  37. void PrintMatrix(int* arr, int n) {
  38. for (int i = 0; i < n; i++) {
  39. printf("\n");
  40. for (int j = 0; j < n; j++) {
  41. printf("%3d", *(arr + i * n + j));
  42. }
  43. }
  44. }
  45.  
  46. int* ChangeMatrix(int* arr, int n) {
  47. int temp;
  48. for (int i = 0; i < n; i++) {
  49. temp = *(arr + i * n + 0);
  50. *(arr + i * n + 0) = *(arr + i * n + i);
  51. *(arr + i * n + i) = temp;
  52. }
  53. return arr;
  54. }
  55.  
  56.  
  57. int main() {
  58. int n;
  59. int* arr;
  60. n = InputN();
  61. arr = InputMatrix(n);
  62. printf("\nИсходная матрица: ");
  63. PrintMatrix(arr, n);
  64. arr = ChangeMatrix(arr, n);
  65. printf("\n");
  66. printf("\nИзменённая матрица: ");
  67. PrintMatrix(arr, n);
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement