neogz

NIZOVI, sastavi 2 niza u 1 i sortiraj dobiveni (bubble)

Jan 22nd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6.  
  7. void unos(int niz[], int);
  8. void spremi(int glavni_niz[], int niz1[], int niz2[], int);
  9. void ispis(int niz[], int);
  10. void sortiraj(int niz[], int);
  11.  
  12. int main()
  13. {
  14.        
  15.     const int max = 5;
  16.  
  17.     int niz1[max];
  18.     int niz2[max];
  19.  
  20.     unos(niz1, max);
  21.     unos(niz2, max);
  22.  
  23.     const int max2 = 10;
  24.  
  25.     int niz3[max2];
  26.     spremi(niz3, niz1, niz2, max2);
  27.  
  28.     ispis(niz3, max2);
  29.     sortiraj(niz3,max2);
  30.     ispis(niz3, max2);
  31.  
  32.     system("pause>null");
  33.     return 0;
  34. }
  35.  
  36. void unos(int niz[], int max)
  37. {
  38.     cout << "Unesite vrijednosti niza: " << endl;
  39.     for (int i = 0; i < max; i++)
  40.     {
  41.         cout << i + 1 << " -> ";
  42.         cin >> niz[i];
  43.     }
  44.     cout << endl;
  45. }
  46. void spremi(int glavni_niz[], int niz1[], int niz2[], int max)
  47. {
  48.     for (int i = 0; i < max/2; i++)
  49.     {
  50.         glavni_niz[i] = niz1[i];
  51.         glavni_niz[i + 5] = niz2[i];
  52.     }
  53. }
  54. void ispis(int niz [], int max)
  55. {
  56.     cout << "Vas niz je: " << endl;
  57.     for (int i = 0; i < max; i++)
  58.     {
  59.         cout << setw(3) << niz[i];
  60.     }
  61.     cout << endl;
  62. }
  63. void sortiraj(int niz[], int max)
  64. {
  65.         bool sortirano = true;
  66.         int j = 0;
  67.         int tmp;
  68.         while (sortirano) {
  69.             sortirano= false;
  70.             j++;
  71.             for (int i = 0; i < max - j; i++) {
  72.                 if (niz[i] > niz[i + 1]) {
  73.                     tmp = niz[i];
  74.                     niz[i] = niz[i + 1];
  75.                     niz[i + 1] = tmp;
  76.                     sortirano = true;
  77.                 }
  78.             }
  79.         }
  80.    
  81. }
Advertisement
Add Comment
Please, Sign In to add comment