Reputation: 23
There are double numbers in the output
344.6
563.4
455.6
10.7
10.6
1
the last two shows
[0] 10.000000000000000 double
[1] 10.000000000000000 double
in the locals variable in visual studio express 2015, and
repRate[0]==repRate[1]
true
why? I tried use (double) to make sure. The output gives me two different number but repRate[0]==repRate[1] return true.
this is the input.
5
1 10 3456
2 10 5644
3 10 4566
4 20 234
5 20 232
five means you have 5 items, 1 is id code, 10 is the initial number, 3456 is the final number. the reproduce rate is (3456-10)/10 and I need to sort the array using reproduce rate as index.
#include <iostream>
using namespace std;
int main() {
int n;
int a[110][4];
double repRate[110];
//input
cin >> n;
for (int i = 0; i < n;i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
//rep rate
for(int i = 0;i < n;i++){
repRate[i] = (double) ((double)a[i][2]- (double)a[i][1]) / (double)a[i][1];
cout << repRate[i] << endl;
}
//sort
for (int i = 0;i < n;i++) {
for (int j = 0;j < n - i-1;j++) {
int temp[5];
int rtemp;
if (repRate[j]>repRate[j+1]) {
rtemp = repRate[j+1];
repRate[j + 1] = repRate[j];
repRate[j] = rtemp;
for (int k = 0;k < 4;k++) {
temp[k] = a[j+1][k];
}
for (int k = 0;k < 4;k++) {
a[j+1][k] = a[j][k];
}
for (int k = 0;k < 4;k++) {
a[j][k] = temp[k];
}
}
}
}
int test;
test = repRate[0] == repRate[1];
cout << test;
return 0;
}
Upvotes: 1
Views: 116
Reputation: 73394
Try setting rtemp
to double, since this line:
rtemp = repRate[j+1];
will cut off some accuracy. That should be a warning on your compiler, if not enable warnings!
After that the output is like this:
5
1 10 3456
2 10 5644
3 10 4566
4 20 234
5 20 232
344.6
563.4
455.6
10.7
10.6
0
where 0 is what you want.
Upvotes: 2