Help with a sorting function

Alright, so I was asked to write a program that will ask the user for ten (10) numbers, and store the data in an array. Use the Bubble Sort (as a function) to sort the values in descending order. Display the sorted values.

Here is my 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;



void Sorter(float numb[], int SIZE);
void Swapper(float& x, float& y);


int main ()
{
    //Local constants
    const int SIZE = 10;

    //Local variables
    int num;
    float numbers[SIZE];

/******************** Begin main Function Executables *************************/
    //Get input
    for (num = 0; num <= (SIZE - 1); num++)
    {
        cout<< "Enter number " << (num + 1) << "-> ";
        cin >> numbers[num];
    }

    //Sort numbers in descending order
    Sorter(numbers, SIZE);

    //Clear the screen
    system("cls");

    //Display
    for (num = 0; num <= (SIZE - 1); num++)
        cout<< numbers[num] << setw (15) << endl;

    //Hold execution on screen
    system("pause");

    //Indicate to OS successful termination of program
    return 0;

}   //End main


void Sorter (float numb[], int SIZE)
{
    //Local Variables
    int pass;
    int element;

    //Sort the numbers in descending order
    for (element = 0; element <= (4 - pass); element ++)
        {
        for (element = 0; element <= ((SIZE - 1) - pass); element++)
            if (numb[element] < numb[element + 1])
                Swapper(numb[element], numb[element + 1]);
        }


void Swapper(float& x, float& y)
{
    //Local Variables
    int temp; //Temporary holder

    //Swapping
        temp = x;      //Store old x in temp
        x    = y;      //Store old y in x
        y    = temp;   //Store old x (temp) in y
}


i just can get it to work as wanted. Please help me again!
Hmmm... well there's two glaring errors in this code,
(1) line 67, that should be a float, storing a float in an int results in a cast, and can cause some loss of precision
(2) line 52, you never assign a value to pass, meaning it will have some crazy value like -234823489, so using this as the limit in a loop is not a good idea.

Other than that, your Sorter function does not seem to implement bubble sort correctly. You definitely need to go back to the drawing board for that one
Topic archived. No new replies allowed.