0

I have written the following program intended on comparing float's in C++. Originally this was written trying to compare double's but I soon realized how bad a problem that is. In general what is supposed to happen is that the program is to compare the two numbers of the given slot array and swap them as necessary.

#include<iostream>

using namespace std;

int swap(float[] , int, int);

int main() {
    float slot[10] = {8.25, 3.26, 1.20, 5.15, 7.99, 10.59, 4.36, 9.76, 6.29, 2.09};
    int n=10, i;
    int lower, upper, sortflag, sml, scan;
    lower = 0;
    upper = n-1;
    sortflag = 1;

    float temp;

    while( (lower < upper) && (sortflag == 1)) {
        sml = lower;
        sortflag = 0;
        scan = lower + 1;
        while(scan <= upper - lower) {
            if (slot[scan] > slot[scan + 1]) {
                swap(slot, scan, scan + 1);

                sortflag = 1;
                if(slot[scan] < slot[sml]) sml = scan;
            }
            scan++;
        }

    swap(slot, lower, sml);

    upper = upper - 1;
    lower = lower + 1;
    }

    cout << "AFTER SORT: " << endl;
    for (i= 0; i < n; i++) cout << slot[i] << " ";

    cout << endl;

    return 0;

}

void swap(float data[], int i, int j) {

    float temp;

    temp = data[i];
    data[j] = data[i];
    data[j] = temp;

}

When I ran this program with double instead of float, the program ran infinitely until I had to invoke Ctrl+C to break it. After switching to float I instead get the following output:

AFTER SORT:
8.25 8.25 3.26 5.15 7.99 10.59 10.59 10.59 10.59 10.59
0 0 1 3 4 5 5 5 5 5

--------------------------------
Process exited after 0.06651 seconds with return value 0
Press any key to continue . . .

Where is the logic going wrong?

EDIT: So after some consideration, I went ahead and rewrote the program to make it compare int array values instead.

int slot[10] = {8, 3, 1, 5, 7, 10, 4, 9, 6, 2};

And adjusted all the appropriate functions as necessary: // Declaration of function: void swap(int[] , int, int);

void swap(int data[], int i, int j) {

int temp;

temp = data[i];
data[i] = data[j];
data[j] = temp;

}

And the function is now coming up correct with the correct input. There is no problems with going out of bounds here.

AFTER SORT:
1 2 3 4 5 6 7 8 9 10


--------------------------------
Process exited after 0.05111 seconds with return value 0
Press any key to continue . . .

Here's the new modified program:

int main() {
    int slot[10] = {8, 3, 1, 5, 7, 10, 4, 9, 6, 2};

    int n=10, i;
    int lower, upper, sortflag, sml, scan;
    lower = 0;
    upper = n-1;
    sortflag = 1;

    while( (lower < upper) && (sortflag == 1)) {
        sml = lower;
        sortflag = 0;
        scan = lower + 1;
        while(scan <= (upper-lower)) {
            if (slot[scan] > slot[scan + 1]) {
                swap(slot, scan, scan + 1);

                sortflag = 1;
                if(slot[scan] < slot[sml]) sml = scan;
            }
            scan++;
        }

    swap(slot, lower, sml);

    upper = upper - 1;
    lower = lower + 1;
    }

    cout << "AFTER SORT: " << endl;
    for (i= 0; i < n; i++) cout << slot[i] << " ";
    cout << endl;
    //for (i= 0; i < n; i++) cout << index[i] << " ";

    cout << endl;

    return 0;

}

void swap(int data[], int i, int j) {

    int temp;

    temp = data[i];
    data[i] = data[j];
    data[j] = temp;

}

So now the question is why does the int version work without problem but neither the double nor float versions do?

Paul Williams
  • 1,554
  • 7
  • 40
  • 75

1 Answers1

2

Your swap function is wrong. data[j] = data[i]; is useless when followed by another write data[j] = temp;. It should be like this

int swap(float data[], int i, int j) {

    float temp;

    temp = data[i];
    data[i] = data[j]; // reverse this line
    data[j] = temp;

    return 0;
}

And there's no point making the function returning int if you don't use the result. Just declare it void

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Although this fixes one bug, it doesn't fix the entire program. Going beyond the edge of the array bounds is also a problem. Should be noted that both swap functions suffer the same problem. – Michael Petch Sep 20 '15 at 17:25