fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6.  
  7. // Ask for the number of elements
  8. cout << "Enter the number of elements: ";
  9. cin >> n;
  10.  
  11. // Declare an array of size n
  12. int arr[n];
  13.  
  14. // Input the elements of the array
  15. cout << "Enter the elements:" << endl;
  16. for(int i = 0; i < n; i++) {
  17. cin >> arr[i];
  18. }
  19.  
  20. // Bubble Sort Algorithm to sort the array in descending order
  21. for(int i = 0; i < n - 1; i++) {
  22. for(int j = 0; j < n - 1 - i; j++) {
  23. if(arr[j] < arr[j + 1]) {
  24. // Swap the elements if they are in the wrong order (for descending order)
  25. int temp = arr[j];
  26. arr[j] = arr[j + 1];
  27. arr[j + 1] = temp;
  28. }
  29. }
  30. }
  31.  
  32. // Display the sorted array in descending order
  33. cout << "Sorted elements in descending order: ";
  34. for(int i = 0; i < n; i++) {
  35. cout << arr[i] << " ";
  36. }
  37. cout << endl;
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5284KB
stdin
 
stdout
Enter the number of elements: Enter the elements:
Sorted elements in descending order: