Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int ary[10] = {10,5,16,28,55,100,90,14,15,31};// Declare array of 10
- int end = 9; // Variable for last position in array
- int biggest; // Variable to hold value of largest #
- int pos = 0; // Variable to hold position where largest value is found
- for (int i = 0; i < end; i++) // For n - 1 passes
- {
- biggest = ary[0]; // Set first array value to the variable that holds biggest
- for (i = 0; i <= end; i++) // For all elements in array
- {
- if (ary[i] > biggest) // If biggest element is larger than second element
- {
- biggest = ary[i]; // Set that value to 'biggest'
- pos = i; // Hold this position #
- }
- else if (ary[1] < ary[0])// I know this is crappy, but I can't figure out how to do it better.
- {
- int temp = ary[i+1]; // Sweap the first two elements if second is less than first
- ary[i+1] = ary[i];
- ary[i] = temp;
- }
- }
- ary[pos] = ary[end]; // Once we found biggest # in array, swap end value with
- ary[end] = biggest; // biggest position's value
- end--; // Stop including last # in search
- i = 0; // Now we start over again to find the next largest #
- }
- // Now the list we compare is one shorter
- end = 9; // Reset end back to 9
- for (int p = 0; p <= end; p++) // For all elements in array
- cout << ary[p] << endl; // Print elements
- }
Advertisement
Add Comment
Please, Sign In to add comment