Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // Wczytac n liczb do tablicy a o rozmiarze n. Zamienic wartosciami liczbe najwieksza z najmniejsza.
  2. // Uporzadkowac wartosci w ciag niemalejacy. Wczytac liczbe x i wstawic ja do tablicy zachowujac uporzadkowanie
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. const int n = 5;
  9.  
  10. int main(void){
  11.  
  12. int tab[n];
  13.  
  14. for(int i = 0; i < n; i++){
  15.  
  16. cin >> tab[i];
  17. }
  18.  
  19. int max = tab[0];
  20. int min = tab[0];
  21.  
  22. int imin = 0;
  23. int imax = 0;
  24.  
  25. for(int i = 0; i < n; i++){
  26.  
  27. if(tab[i] > max){
  28.  
  29. max = tab[i];
  30. imax = i;
  31. } else if(tab[i] < min){
  32.  
  33. min = tab[i];
  34. imin = i;
  35. }
  36. }
  37.  
  38. for(int i = 0; i < n; i++){
  39.  
  40. cout << tab[i] << '\t';
  41. }
  42.  
  43. cout << endl << endl;
  44.  
  45. int temp = tab[imin];
  46. tab[imin] = tab[imax];
  47. tab[imax] = temp;
  48.  
  49. for(int i = 0; i < n; i++){
  50.  
  51. cout << tab[i] << '\t';
  52. }
  53.  
  54. cout << endl << endl;
  55.  
  56. for(int j = 0; j < n-1; j++){
  57.  
  58. for(int i = 0; i < n-1-j; i++){
  59.  
  60. if(tab[i] > tab[i+1]){
  61.  
  62. int tmp = tab[i];
  63. tab[i] = tab[i+1];
  64. tab[i+1] = tmp;
  65. }
  66. }
  67. }
  68.  
  69. for(int i = 0; i < n; i++){
  70.  
  71. cout << tab[i] << '\t';
  72. }
  73.  
  74. int x;
  75.  
  76. cout << endl << "Podaj wartosc x" << endl;
  77.  
  78. cin >> x;
  79.  
  80. int idx = 0;
  81.  
  82. for(int i = 0; i < n-1; i++){
  83.  
  84. if(x >= tab[i] && x < tab[i+1]){
  85.  
  86. idx = i+1;
  87. break;
  88.  
  89. } else if(x > tab[n-1]){
  90.  
  91. idx = n-1;
  92. }
  93. }
  94.  
  95. for(int i = n-1; i > idx; i--){
  96.  
  97. tab[i] = tab[i-1];
  98. }
  99.  
  100. tab[idx] = x;
  101.  
  102. cout << endl << endl;
  103.  
  104. for(int i = 0; i < n; i++){
  105.  
  106. cout << tab[i] << '\t';
  107. }
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement