的最小数我有以下形式的一组数据:找到一组数据
a1 b1 c1 d1
a2 b2 c2 d2
...
an bn cn dn
我的目标是要找到具有为C列的值最小的行。
我做了以下内容:
const int limit=100000;
float Array[limit][4];
int main() {
double a, b, c, d, smallest, ref1, ref2;
ifstream in("data.dat");
int idx=-1, point;
while(!in.eof()) {
idx++;
in >> a >> b >> c >> d;
Array[idx][0]=a; Array[idx][1]=b; Array[idx][2]=c; Array[idx][3]=d;
} \\end of while
in.close();
int count=idx;
for(int i=1; i<count; i++) {
ref1= Array[0][2];
ref2 = Array[i][2];
if(ref2 < ref1) {ref1 = ref2; point=i;} //I thought this will save the smallest value
smallest = ref1; point= i;
} \\end for
cout << "point" << Array[point][0] << Array[point][1] << .. etc.
return 0;
}
但是,输出是在数据的最后一个点。 (至于输入这个问题,我意识到当新行被读取时,ref1将始终是Array [0] [2]。所以现在我完全失去了!)
如何保存一个点作为参考点,以便将其与其余数据进行比较,并且每次将其与较小的点进行比较时,它会变为较小的值?
UPDATE:我通过考虑ref1 = Array [0] [2];出于for循环。
您使用'count'没有被初始化。但是我没有得到你所问的问题,你能再解释一下吗? – 2012-08-13 13:51:52
你是否检查你是否正确读取文件? – jrok 2012-08-13 13:52:28
你在每一个for循环中设置'point = i',不管是否它是最小或不是。删除它 – acraig5075 2012-08-13 13:57:40