hey guys,been browsing this site for a few weeks,but this is my first post.
As you might guess by my problem I'm a C++ noob.

I'm tryin to make a program in which the user inputs an array of double, I then use bubble sort to to get it into descending order, and output it in the correct order.

the issue I'm having is that after I sort the array, only the first number comes out as a double, the rest are all integers. How can I fix it so that all the numbers come out at the end as double??


here's my code:

#include <iostream>

using namespace std;

int main()
{
    double set[50];//sets max number of values to 50
    int size, a, b, t, i;

    cout << "How many values would you like to enter??\n";
    cin >> size;

    while (size>50)
    {
        cout << "That is above 50" << endl;
        cout << "How many values would you like to enter??\n";
        cin >> size;
    }
    
    cout << "Please enter the values:\n";
    for (i=0; i<size; i++) 
    cin >> set[i];

    for (a=1; a<size; a++) 
        for (b=size-1; b>=a; b--) {
            if(set[b-1] < set[b]) {

                t=set[b-1];
                set[b-1] = set[b];
                set[b] = t;
                }
            }

    cout << "here are your values in order:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

Recommended Answers

All 11 Replies

You are using the int variable "t" as your temporary value when you do your swap.

When you try to store a double to an int it truncates the floating point part of the number.

C++ arrays start at element 0, NOT element 1. The reason your first element is still a double is because your algorithm starts at element 1 instead of element 0. I'd be willing to bet that your first value is not only still a double but also not in its proper sorted position.

I think it's also worth while to note that you don't have a swap flag. You really should think about adding one to speed up completion a little.

Yeah you're right about the starting at 1. Cheers. I'll change that to 0 now. I feckin knew that already and all...

I've tried changing the "t" to be a double, but then an error occurs on line 36. it just says; "error: invalid type 'double [50] [double]' for array subscript"
I'm using a program called Codeblocks btw.

any ideas?

It all depends on what line 36 looks like with your change.

>>I feckin knew that already and all...
>>Location: Dublin, Ireland

I understand this is a cultural thing and all ('ave some Irish in me m'self :P), but it's really not necessary here. Besides, you asked and I have no way of knowing you "knew that already and all...".

>>I've tried changing the "t" to be a double, but then an error occurs on line 36.
This is because you are using your variables for more than one unrelated purpose. That's something you really shouldn't do, for this very reason.

Are you familiar with declaring indexes locally to a for loop? See the declarations of 'i' and 'j' in the example Bubble Sort implementation below for an example (don't try to c/p it, it won't work in your program):

//Bubble sort
template <typename T> void DataSet<T>::BubbleSort() {
  for (int i = 0; i < m_DataSet.size(); ++i) {        //establish "surface" index
    bool swap = false;                                //declare flag variable
    for (int j = (m_DataSet.size()-1); j > i; --j) {  //establish "bubble" index
      if (m_DataSet[j] > m_DataSet[j-1]) {            //compare "bubble" with adjacent value
        std::swap(m_DataSet[j], m_DataSet[j-1]);      //perform the swap, if necessary
        swap = true;                                  //set the flag, if necessary
      }
    } //end for (index j)
    if (!swap)                                        //check the flag
      break;                                          //end the sort if flag not set
  } //end for (index i)
} //end DataSet<T>::BubbleSort()

what I've tried now is simply using a new double "temp" instead of "t" inside my bubblesort. it has everything coming out as doubles which is great, except that the highest number is omitted and in it's place is "1.88615e - 307" as the final character...??

I've learned a bit about declaring locally, but not about swap flags.How would I go about declaring the "temp" variable within the loop??

here's my adjusted code:

#include <iostream>

using namespace std;

int main()
{
    double temp, set[50];
    int size, t, i;

    cout << "How many values would you like to enter?\n";
    cin >> size;

    while (size>50)
    {
        cout << "That was above 50!" << endl;
        cout << "How many values would you like to enter??\n";
        cin >> size;
    }

    cout << "Please enter the values:\n";
    for (value=0; value<size; value++)
    cin >> set[value];

    for (int a=0; a<size; a++) 
        for (int b=size-1; b>=a; b--) {
            if(set[b-1] < set[b]) {

                temp=set[b-1];
                set[b-1] = set[b];
                set[b] = temp;
                }
            }

    cout << "Here are your values in order:\n";
    for (t=0; t<size; t++)
    cout << set[t] << "\n";

    return 0;
}

>>what I've tried now is simply using a new double "temp" instead of "t" inside my bubblesort
Good.

>>...the highest number is omitted and in it's place is "1.88615e - 307" as the final character...??
This line is your problem:

for (int b=size-1; b>=a; b--) {

What happens when a == 0?

>>I've learned a bit about declaring locally, but not about swap flags
It's just a locally declared variable that you use to keep track of your program's status. While the flag is in-scope, you make a decision based on the value of the flag.

>>How would I go about declaring the "temp" variable within the loop?
Just like you did with your for loop indexes, just do it inside your if's statement block:

if (condition) {
  double temp = someValue;
  /* ... */
}

I'm surprised your compiler isn't flagging this as an implicit declaration:

for (value=0; value<size; value++)
cin >> set[value];

You need to create a proper declaration for "value", your program doesn't have one. I had to fix this before I could compile and test your code.

for (int a=0; a<size; a++) 
        for (int b=size-1; b>=a; b--) {
            if(set[b-1] < set[b]) {

                temp=set[b-1];

What happens to temp when b == a in this loop structure?

ah yeah,sorry,in my actual program I'd changed the "i" int at the beginning to "value". just a typo that.

also now have "temp" variable as an actual temporary variable. cheers for that. I'd tried it that way earlier,but for some reason didn't work.probably another typo by myself :/

and my main problem is solved by simply using

1.      for (int b=size-1; b=a; b--) {

instead of

1.      for (int b=size-1; b>=a; b--) {

embarrasingly simple change to be made...
thanks a million anyways!
will be back!

for (int b=size-1; b=a; b--) {

Actually, that should be:

for (int b=size-1; b>a; b--) {

Or is that another typo?

yeps... should really double check my posts...

thanks again

yeps... should really double check my posts...

thanks again

Generally, it works best if you do a Copy/Paste from your IDE to your post. Less chance for transcription/typographic errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.