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.
#include <cstdlib>
#include <iostream>
#include <iomanip>
usingnamespace std;
void Sorter(float numb[], int SIZE);
void Swapper(float& x, float& y);
int main ()
{
//Local constants
constint 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