Advertisement
Guest User

wrwerewrew

a guest
Jan 27th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int czytaj()
  8. {
  9. int n;
  10. do
  11. {
  12. cout<<"Wrowadz rozmiar tablicy z zakresu od 100 do 1000"<<endl;
  13. cin>>n;
  14. if(n<100 || n>1000)
  15. {
  16. cout<<"Nieodpowiedni rozmiar tablicy"<<endl;
  17. }
  18. }
  19. while(n<100 || n>1000);
  20.  
  21. return n;
  22. }
  23.  
  24. void losuj(int t[] , int n)
  25. {
  26. srand(time(NULL));
  27.  
  28. for(int i=0 ; i<n ; i++)
  29. {
  30. t[i]=rand()%21-10;
  31. }
  32. }
  33.  
  34. void pisz(int t[] , int n)
  35. {
  36. cout<<"Pierwsza wylosowana tablica"<<endl;
  37. for(int i=0 ; i<n ; i++)
  38. {
  39. cout<<t[i]<<" ";
  40. }
  41. cout<<" "<<endl;
  42. }
  43.  
  44. void sortowanie( int t[], int n )
  45. {
  46. int zamiana ;
  47. int porownanie ;
  48. for( int i = 0; i < n; i++ )
  49. {
  50. for( int a = 0; a < n - 1; a++ )
  51. {
  52. porownanie++;
  53. if( t[ a ] > t[ a + 1 ] )
  54. {
  55. swap( t[ a ], t[ a + 1 ] );
  56. zamiana++;
  57. }
  58. }
  59. }
  60.  
  61. cout<<"Tablica po sortowaniu"<<endl;
  62.  
  63. for(int i=0 ; i<n ; i++)
  64. {
  65. cout<<t[i]<<" ";
  66. }
  67. cout<<" "<<endl;
  68. cout<<"Ilosc wykonanych zamian: "<<zamiana<<endl;
  69. cout<<"Ilosc wykonanych porownan: "<<porownanie<<endl;
  70. }
  71.  
  72. int main()
  73. {
  74.  
  75. int n=czytaj();
  76. int t[n];
  77. losuj(t ,n);
  78. pisz(t ,n);
  79. sortowanie(t, n);
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement