D-Gj

Podreduvanje po abeceda

Mar 3rd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. // Da se podredat iminja po abeceden redosled.
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char names[20][500];
  11.  
  12.     int length, temp;
  13.  
  14.     cout << "Vnesi dolzhina na nizata (max = 20): ";
  15.     cin >> length;
  16.     cout << endl;
  17.  
  18.     if (length > 20) length = 20;
  19.  
  20.     temp = length;
  21.  
  22.     for (int i = 0; i < length; ++i)
  23.     {
  24.         cout << "Vnesi ime: ";
  25.         cin >> names[i];
  26.     }
  27.  
  28.     do
  29.     {
  30.         int new_length = 0;
  31.  
  32.         for (int i = 1; i < length; ++i)
  33.         {
  34.             if (strcmp(names[i - 1], names[i]) > 0)
  35.             {
  36.                 swap(names[i - 1], names[i]);
  37.                 new_length = i;
  38.             }
  39.         }
  40.  
  41.         length = new_length;
  42.  
  43.     } while (length != 0);
  44.  
  45.     for (int i = 0; i < temp; ++i) cout << names[i] << endl;
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment