Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. void minmax1(int tab[], int n, int &MIN, int &MAX)
  9. {
  10. int i = 2;
  11. if(n>=2)
  12. {
  13. if(tab[0]>tab[1])
  14. {
  15. MIN = tab[1];
  16. MAX = tab[0];
  17. }
  18. else
  19. {
  20. MIN = tab[0];
  21. MAX = tab[1];
  22. }
  23. while(i+2<=n)
  24. {
  25. if(tab[i]>tab[i+1])
  26. {
  27. if(tab[i]>MAX)
  28. MAX = tab[i];
  29. if(tab[i+1]<MIN)
  30. MIN = tab[i+1];
  31. }
  32. else
  33. {
  34. if(tab[i+1]>MAX)
  35. MAX = tab[i+1];
  36. if(tab[i]<MIN)
  37. MIN = tab[i];
  38. }
  39. i+=2;
  40. }
  41. if(n%2==1)
  42. {
  43. if(tab[i] > MAX)
  44. MAX = tab[i];
  45. if(tab[i] < MIN)
  46. MIN = tab[i];
  47. }
  48. }
  49. else
  50. {
  51. MIN = MAX = tab[0];
  52. }
  53. }
  54.  
  55. void minmax2(int tabx[], int nx, int &MINx, int &MAXx)
  56. {
  57. int i = 2;
  58. if(nx>=2)
  59. {
  60. if(tabx[0]>tabx[1])
  61. {
  62. MINx = tabx[1];
  63. MAXx = tabx[0];
  64. }
  65. else
  66. {
  67. MINx = tabx[0];
  68. MAXx = tabx[1];
  69. }
  70. while(i+2<=nx)
  71. {
  72. if(tabx[i]>tabx[i+1])
  73. {
  74. if(tabx[i]>MAXx)
  75. MAXx = tabx[i];
  76. if(tabx[i+1]<MINx)
  77. MINx = tabx[i+1];
  78. }
  79. else
  80. {
  81. if(tabx[i+1]>MAXx)
  82. MAXx = tabx[i+1];
  83. if(tabx[i]<MINx)
  84. MINx = tabx[i];
  85. }
  86. i+=2;
  87. }
  88. if(nx%2==1)
  89. {
  90. if(tabx[i] > MAXx)
  91. MAXx = tabx[i];
  92. if(tabx[i] < MINx)
  93. MINx = tabx[i];
  94. }
  95. }
  96. else
  97. {
  98. MINx = MAXx = tabx[0];
  99. }
  100. }
  101.  
  102.  
  103. int main()
  104. {
  105. int MIN, MAX, tab[100];
  106. int n = 100;
  107.  
  108. fstream plik1;
  109. plik1.open("pliki/plik.txt", ios::in) ;
  110. if (plik1.good() )
  111. {
  112. int x = 0;
  113.  
  114. while (!plik1.eof())
  115. {
  116. plik1 >> tab[x];
  117. cout << tab[x] << " ";
  118. x++;
  119. }
  120. }
  121.  
  122. cout << "----------------------------------------------------------------" <<endl;
  123.  
  124.  
  125.  
  126. minmax1(tab, n, MIN, MAX);
  127. cout << "Wartosc MIN: " << MIN << endl;
  128. cout << "Wartosc MAX: " << MAX << endl;
  129.  
  130. cout << " ------------------------------------------- " <<endl;
  131.  
  132. int MINx, MAXx, tabx[100];
  133. int nx = 100;
  134.  
  135. fstream plik2;
  136. plik2.open("pliki/plik2.txt", ios::in) ;
  137. if (plik2.good() )
  138. {
  139. int c = 0;
  140.  
  141. while (!plik2.eof())
  142. {
  143. plik2 >> tabx[c];
  144. cout << tab[c] << " ";
  145. c++;
  146. }
  147. }
  148.  
  149. cout << "----------------------------------------------------------------" <<endl;
  150.  
  151.  
  152.  
  153. minmax2(tabx, nx, MINx, MAXx);
  154. cout << "Wartosc MIN: " << MINx << endl;
  155. cout << "Wartosc MAX: " << MAXx << endl;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement