Sorting Arrays

for my program i need to sort my int numbers [5] from highest to lowest
but it seems that i made it go from lowest to highest

can some help fix my code please?

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int main()
{
   
    int numbers[5] = {3, -6, 10, 1, 130};
    int counter1, counter2, tempNum;
    
//Highest to Lowest

   for(int counter1=0; counter1<5; counter1++){

           
           for(int counter2=0; counter2<5; counter2++){

  
                   if(numbers[counter2]>numbers[counter2+1]){
                   
        
                    tempNum=numbers[counter2];
                    numbers[counter2]=numbers[counter2+1];
                    numbers[counter2+1]=tempNum;        
                    }
            }         
     } 
     //display the sorted array
     for (int i=0; i<5; i++){
         cout << numbers[i] << endl;
         }

    system("pause");
    return 0;
}


Line 18. Your condition decides when to change order of elements.


Note: the counter2 can have value 4. When it has, counter2+1 is 5. There is no 5 in your array. (Valid indices are 0..n-1)
Topic archived. No new replies allowed.