Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. void input(int *n, int *m, int (*a)[20]) {
  7. int i, j;
  8.  
  9. cout << "Write n:\n";
  10. cin >> *n;
  11.  
  12. cout << "Write m:\n";
  13. cin >> *m;
  14.  
  15. cout << "Write array a[][]:\n";
  16. for (i = 0; i < *n; i++)
  17. for (j = 0; j < *m; j++)
  18. cin >> a[i][j];
  19.  
  20. return;
  21. }
  22.  
  23.  
  24. void func(int n, int m, int (*a)[20], int *u1, int *u2) {
  25. int i, j, t;
  26.  
  27. for (*u1 = *u2 = -1, i = 0; i < n; i++)
  28. for (j = 0; j < m; j++)
  29. if (a[i][j] % 2)
  30. if (*u1 == -1)
  31. *u1 = i * 20 + j;
  32. else if (*u2 == -1)
  33. *u2 = i * 20 + j;
  34. else {
  35. t = *u1;
  36. *u1 = i * 20 + j;
  37. *u2 = t;
  38. }
  39. return;
  40. }
  41.  
  42.  
  43. void output(int n, int m, int a[20][20], int u1, int u2) {
  44. int i, j, c;
  45.  
  46. c = a[u1 / 20][u1 % 20];
  47. a[u1 / 20][u1 % 20] = a[u2 / 20][u2 % 20];
  48. a[u2 / 20][u2 % 20] = c;
  49.  
  50. cout << "New array:\n";
  51. for (i = 0; i < n; i++) {
  52. for (j = 0; j < m; j++)
  53. cout << a[i][j] << ' ';
  54. cout << endl;
  55. }
  56.  
  57. }
  58. int main() {
  59. int a[20][20], u1, u2, t, n, m, c, i, j;
  60.  
  61. input(&n, &m, a);
  62.  
  63. func(n, m, a, &u1, &u2);
  64.  
  65. if(u2 == -1 || u1 == u2)
  66. cout << "No swap\n";
  67. else
  68. output(n, m, a, u1, u2);
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement