You're re-using variable t as an int counter (in your loops) and as a temporary placeholder during your swap (intending it to be a double at that point). It may not affect your answers, but try using variables longer than one-letter, and making sure that each only has one purpose and one type per function.

Other than that, tell us what decimal numbers you're entering (and in what order), and what gets printed out as the sorted array. That might make it more obvious what's going wrong.

Hi,

This is what my code looks like now.

``````#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
//Declaring the variables
int size;
int a,b,t, nums;

cout << "\t\t\tValue Sorter\n" << "\n"; //This is the title.

//this do loop will set the size of the array.
do
{
cout << "Please enter the amount of values you want to sort (max 50): ";
cin >> size;
}while (size > 50 || size < 0);

cout << "\nYou have chosen to sort " << size << " values.\n";

//I will now look for the values to be stored in the array
double *arr = new double[size];
for (int t=0; t < size; t++)
{
if (t < size)
{
cout << "Enter value " << t << ": ";
cin >> arr[t];
}
}

cout << "\nYou entered: " << endl;

for (int t=0; t < size; t++)
{
cout << arr[t] << " ";
}

cout << "\n";

//this is the bubble sort
for (int a=0; a<size; a++)
{
for (int b=size-1; b>=a; b--)
{
if (arr[b-1] > arr [b])
{
/*which means if out of order
exhange the vlaues*/
nums = arr[b-1];
arr[b-1] = arr[b];
arr [b] = nums;
}
}
}

//this will display the sorted array
cout << "\nThe sorted values in ascending order are:\n";
for(nums=0; nums<size; nums++)
{
cout << arr[nums] << " ";
}

cout << "\n";

return 0;
}``````

Do I need to declare a and b as variables? I'm still not getting sorted numbers that have decimal points.

For example,
Input = 1.3, 1.5, 1.1
It will say numbers you input are 1.3, 1.5, 1.1 {so not losing decimals at this step}
{I'm only losing them in the sorting step below. So I assume there's something wrong with my bubble sort?}
Your sorted numbers in ascending order are 1 1 1. Not sure where I'm going wrong.
Thanks for the help guys.

You already -have- declared a and b, you do it inline as part of your `for()` statement, rather than separately, but since you don't use them outside the scope of those loops, it's fine.

The two problems remaining are:
1) After the sort, you use "nums" for your loop counter, instead of going back to t. By itself, it wouldn't be a problem, but why switch? Consistency is a good thing.
2) What's actually causing your problem now, which affects using "nums" for your loop counter, is you've declared nums as an int. So when you use it as a temporary variable in your bubble-sort code, you're throwing out the fractional part of the values as you bubble a value down past them. Since arr[] contains doubles, your temporary value should be a double also. A float or double can be used to control a loop, but should be done carefully. Since array indices are integers, that's the type to use to loop over elements in an array (regardless of the type of values in stored in the array.

Good luck!

Thanks for all the help. Fixed the problems (I think :-D)!

Here's the code for anybody that's interested. It is sorting decimal numbers now and printing them to screen.

``````#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
//Declaring the variables
int size;
double t;

cout << "\t\t\tValue Sorter\n" << "\n"; //This is the title.

//this do loop will set the size of the array.
do
{
cout << "Please enter the amount of values you want to sort (max 50): ";
cin >> size;
}while (size > 50 || size < 0);

cout << "\nYou have chosen to sort " << size << " values.\n";

//I will now look for the values to be stored in the array
double *arr = new double[size];
for (int t=0; t < size; t++)
{
if (t < size)
{
cout << "Enter value " << t << ": ";
cin >> arr[t];
}
}

cout << "\nYou entered: " << endl;

for (int t=0; t < size; t++)
{
cout << arr[t] << " ";
}

cout << "\n";

//this is the bubble sort
for (int a=0; a<size; a++)
{
for (int b=size-1; b>=a; b--)
{
if (arr[b-1] > arr [b])
{
/*which means if out of order
exhange the vlaues*/
t = arr[b-1];
arr[b-1] = arr[b];
arr [b] = t;
}
}
}

//this will display the sorted array
cout << "\nThe sorted values in ascending order are:\n";
for(int t=0; t<size; t++)
{
cout << arr[t] << " ";
}

cout << "\n";

return 0;
}``````

Edited by dctb13: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.