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?