Created
November 27, 2024 09:08
-
-
Save vlaleli/de8d0afc22cbbc2b2cd3f06647911000 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| const int size = 15; | |
| int arr[size]; | |
| srand(time(0)); | |
| for (int i = 0; i < size; ++i) { | |
| arr[i] = rand() % 61 - 30; | |
| } | |
| cout << "Initial array: "; | |
| for (int i = 0; i < size; ++i) { | |
| cout << arr[i] << " "; | |
| } | |
| cout << endl; | |
| bool sorted = true; | |
| for (int i = 0; i < size - 1; ++i) { | |
| if (arr[i] < arr[i + 1]) { | |
| sorted = false; | |
| break; | |
| } | |
| } | |
| if (!sorted) { | |
| for (int i = 0; i < size - 1; ++i) { | |
| for (int j = 0; j < size - i - 1; ++j) { | |
| if (arr[j] < arr[j + 1]) { | |
| swap(arr[j], arr[j + 1]); | |
| } | |
| } | |
| } | |
| cout << "Sorted array: "; | |
| for (int i = 0; i < size; ++i) { | |
| cout << arr[i] << " "; | |
| } | |
| cout << endl; | |
| } else { | |
| cout << "The array is already sorted." << endl; | |
| } | |
| int maxElement = arr[0]; | |
| int minElement = arr[size - 1]; | |
| cout << "Maximum element: " << maxElement << endl; | |
| cout << "Minimum element: " << minElement << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment