【发布时间】:2014-04-01 19:47:26
【问题描述】:
我必须不断生成 10 x 10 数组,直到数组的对角线包含大于或等于 7 的数字。但是尝试次数不应增加一百万。如果尝试次数少于一百万,即打印出对角线上的数字大于 7 的数组和尝试次数。这是我的代码,问题是我的代码总是显示尝试次数超过一百万。有人可以看看代码并告诉我为什么它不能正常工作吗?
int i, j, matrix2[10][10], attempts, count = 0;
for (attempts=0; attempts<1000000; attempts++)
{
for (i=0; i<10; i++)
{
for (j=0; j<10; j++)
{
matrix2[i][j] = rand()%10;
if(i==j&&matrix2[i][j] >= 7)
count++;
}
}
}
printf("\n\nNumber of attempts : %d", count);
if (attempts >= 1000000)
printf("\n\nNumber of attempts exceed one million :\t ACTION TERMINATED!!!");
else
{
for (i=0; i<10; i++)
{
for (j=0; j<10; j++)
if (i==j&&matrix2[i][j] >= 7)
printf("%5d",matrix2[i][j]);
printf("\n\n");
}
}
【问题讨论】:
标签: c arrays multidimensional-array