【发布时间】:2014-10-13 03:33:06
【问题描述】:
我一直在寻找正确的循环来打印值,所以我认为 For 循环会更容易使用。我想打印 10 次值以及从 1000 到 10000 或 1 到 10 的计时器结果,无论你怎么称呼它。代码如下:
cout << " N Value | Array Sort Time | Listed List Sort Time" << endl;
cout << " (seconds) (seconds) " << endl;
cout << endl;
for(int x = 1; x <= 10; x++)
{
x = x*n;
printf(" %d", n);
int m = n; //copy N value for new integer to do 2nd while loop
begin = clock();
while(n != 0)
{
input = rand()%n;
addArray(input, pArray, size);
n--;
if( input == -1)
{
break; // end of list, don't add it to list
}
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf(" %5.3f", time_spent);
begin = clock();
while(m != 0)
{
input = rand()%m;
addNode(pHead, input);
m--;
// break if end of list
if( input == -1)
{
break; // end of list, don't add it to list
}
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf(" %5.3f", time_spent);
}
它只打印一次,所以我的愚蠢代码是怎么回事?!
【问题讨论】:
-
什么只打印一次?
-
我输入的第一个值。我希望它能用 x++ 打印出接下来的 9 个值。
-
所以看起来你的
while循环永远不会结束 - 在调试器中运行它或添加一些打印来跟踪正在发生的事情。 -
多种可能性,请出示
n, begin, end,input, time_spent的声明。显示n的值。假设input和n是int,input = rand()%n; ... if( input == -1)永远不会是真的。
标签: c++ c for-loop printing timer