Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. system("color f0");
  7. int count=0;
  8. float numb;
  9. cout << "This will sort your numbers enter 0 to end";
  10. cout << endl << "Are you going to exceed 100 numbers? (y/n) ------> "; // Conserve memory space
  11. char ans,order;
  12. cin >> ans;
  13. cout << "Do you want the numbers sorted in ascending or desending order? (a/d) ----> ";
  14. cin >> order;
  15. char choice;
  16. if (ans=='y')
  17. {
  18.               cout << endl << "How many numbers do you need to sort? ";
  19.               int choice;
  20.               cin >> choice;
  21.               }
  22. else
  23. {
  24.      choice=100;
  25. }
  26. float numbAr[choice];
  27. bool stop=false;
  28. while (stop==false)
  29. {
  30.       cout << endl << "Enter a number ";
  31.       cin >> numb;
  32.       if (numb==0 || numb==0.0)
  33.       {
  34.                   stop=true;
  35.                   }
  36.                   else
  37.                   {
  38.                        numbAr[count]=numb;
  39.                   count++;
  40.                   }
  41.                   }
  42.                   int outer=0,inner=0,swapct=0,sum=0;
  43.                   float temp;
  44.                   bool swapped=true;
  45.                   cout << endl << endl << "Sorted List: " << endl;
  46.                   while (outer<count && swapped!=false)
  47.                   {
  48.                         swapct=0;
  49.                         inner=0;
  50.                         while (inner<=(count-2))
  51.                         {
  52.                               if (numbAr[inner]<numbAr[inner+1])
  53.                               {
  54.                                     temp=numbAr[inner+1];
  55.                                     numbAr[inner+1]=numbAr[inner];
  56.                                     numbAr[inner]=temp;
  57.                                     swapct++;} inner++;
  58.                                     }
  59.                                     if (swapct==0)
  60.                                     {
  61.                                                    swapped=false;
  62.                                     }
  63.                                     sum+=swapct;
  64.                                     outer++;
  65.                                     }
  66.                                     switch (order)
  67.                                     {
  68.                                            case 'a':
  69.                                                 for (int i=(count-1);i>=0;i--)
  70.                                                 {
  71.                                                     cout << " " << numbAr[i];
  72.                                                     }
  73.                                                     cout << endl << endl;
  74.                                                     cout << "Amount of swaps = " << sum << endl << endl;
  75.                                                     break;
  76.                                                     case 'd':
  77.                                                          for (int i=0;i<count;i++)
  78.                                                          {
  79.                                                              cout << " " << numbAr[i];
  80.                                                              }
  81.                                                              cout << endl << endl;
  82.                                                              cout << "Amount of swaps = " << sum << endl << endl;
  83.                                                              break;
  84.                                                              case 'A':
  85.                                                                   for (int i=(count-1);i>=0;i--)
  86.                                                                   {
  87.                                                                       cout << " " << numbAr[i];
  88.                                                                   }
  89.                                                                   cout << endl << endl;
  90.                                                                   cout << "Amount of swaps = " << sum << endl << endl;
  91.                                                                   break;
  92.                                                                   case 'D':
  93.                                                                        for (int i=0;i<count;i++)
  94.                                                                        {
  95.                                                                            cout << " " << numbAr[i];
  96.                                                                            }
  97.                                                                            cout << endl << endl;
  98.                                                                            cout << "Amount of swaps = " << sum << endl << endl;
  99.                                                                            break;
  100.                                                                            default:
  101.                                                                                     cout << endl << "Invalid output parameter" << endl << endl;
  102.                                                                                     };
  103.                                                                                     system("pause");
  104.                                                                                     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement