2012-08-13 30 views
2

的最小数我有以下形式的一组数据:找到一组数据

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循环。

+0

您使用'count'没有被初始化。但是我没有得到你所问的问题,你能再解释一下吗? – 2012-08-13 13:51:52

+0

你是否检查你是否正确读取文件? – jrok 2012-08-13 13:52:28

+0

你在每一个for循环中设置'point = i',不管是否它是最小或不是。删除它 – acraig5075 2012-08-13 13:57:40

回答

3

你应该设定一个参考,因为我做外循环和更新双打但是这里最小的,顺便说一句,不要混用彩车是一个示例代码:

#include <iostream> 
#include <fstream> 

using namespace std; 


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 i = 0; 
    int count; 
    smallest = Array[i][2]; 
    for(i=1; i<count; i++) { 
    ref2 = Array[i][2]; 

    if(ref2 < smallest) { 
     smallest = ref2; 
     point=i; 
    } 
    } 

    std::cout << "point" << Array[point][0] << " " 
      << Array[point][1] << " " 
      << Array[point][2] << " " 
      << Array[point][3] << std::endl; 

return 0; 

} 

随着数据文件

1 2 3 4 
2 3 8 9 
1 3 5 2 
1 1 1 1 
2 4 2 4 
3 1 0 1 

HTH

+0

谢谢,这就是我所做的,我得到了正确的结果。 – stupidity 2012-08-13 14:21:06

+0

好吧,你可能会把答案当作“接受”来回答;-) – 2012-08-13 15:06:36

3

要证明你已经发现了一组值的最小值,改变你的for循环以下几点:

int smallest_val = std::numeric_limits<int>::max(); 

for(int i=0; i < idx; i++) 
{ 
    if (Array[i][2] < smallest_val) 
     smallest_val = Array[i][2]; 
} 

基本上通过设置smallest_val以最大的可能值,它开始可以使用std::numeric_limits<int>::max()。现在阵列中的每个值必须至少与smallest_value一样大或更小(没有什么可以更大)。在循环访问数组时,一旦您的值小于smaller_value中的当前值,您将正确地将smaller_value中的值重新分配给该新的较低值。通过以int类型表示的尽可能大的可能性开始,可以避免遇到的问题,即最小值相对于彼此的位置。使用数学归纳法,这种类型的方法对于你在这里要做的事情是不必要的。

+0

谢谢你的回答。我也修复它通过考虑1循环。 – stupidity 2012-08-13 14:19:31

0

这里的问题是,你永远不会比你最小。您可以在ref1和ref2之间分配最低值,而不考虑先前的迭代。此外,ref1始终是数据中的第一个值,所以如果最后一个值大于第一个值,最小值始终是最后一个值。将循环更改为Jason发布的内容可以解决您的问题。

1

在不同的音符,输入回路控制不正确:

while(!in.eof()) { 

直到读取失败,才会触发eof()。在实践中,这意味着输入循环会执行一次额外的时间,并且在上次通过时会得到无意义的值。

正确的测试是

while(in >> a >> b >> c >> d) { 

如果有任何提取的失败,(希望因为in是在输入的结束,while循环将结束。

+0

我想,在这种情况下,做一个虽然是更好的选择... ...做某事{...} while(in)if我是对的... – 2012-08-13 15:25:49

+0

不,不是一会儿。只是一会儿。但它应该是'while(in >> a >> b >> c >> d)',而不是'while(!in)',这正是我最初写的。 – 2012-08-13 16:08:11

+0

对,我没有看到你的味精的第二部分... – 2012-08-13 17:02:41