一个双层循环的问题
#include <conio.h>#include <stdio.h>
#include <stdlib.h>
#define N 5
void fun (int a[])
{
int i,count = 0;
int k;
for(i = 0;i < N; i++)//这个大循环只执行i = 0这一次循环,我哪里搞错了吗?
{
for(k = count;k < N;k++)
{
printf("count = %d\n",count);
printf("i = %d\n",i);
a = 0;
count++;
}
}
}
void main()
{
FILE *wf;
int a,i,j;
int b={1,9,7,2,4,2,3,8,1,2,4,5,6,7,5,4,0,6,8,0,2,7,1,6,4};
system("CLS");
printf("*****The array*****\n");
for(i=0;i<N;i++) /*产生一个随机的5*5矩阵*/
{ for(j=0;j<N;j++)
{a=rand()%10;
printf("%4d", a);
}
printf("\n");
}
fun(a);
printf("THE RESULT\n");
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
printf("%4d",a);
printf("\n");
}
/******************************/
wf=fopen("out.dat","w");
fun(b);
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
fprintf(wf,"%4d",b);
fprintf(wf,"\n");
}
fclose(wf);
/*****************************/
}
本帖最后由 cosmos3919 于 2020-1-9 22:08 编辑
先回答你的疑惑吧
你把count++;移到外层for循环内,内层for循环外就对了。详细可以参阅我改后的代码
下面是建议:
1.提问时建议写明你这段代码的用途、适用环境等尽可能多的基本信息
就比如你代码最开始引用了conio.h,这并不是标准c的东西,我的编译器就通不过,拦住了我好一会儿,还好linux系统内建命令支持system("clear")能达到相同的效果。你如果用我的代码运行,这个地方得得改回去^_^
2.你程序用了二维数组,而你代码中却有a=0;,不知你怎么编译通过的,二维数组的a是一个指针,用整数0赋值不知道对不对,我感觉会有问题(主要不知道这个代码的作用),我修改了一下,不知与你的目的是否一致,下面是我改后的代码和运行结果:
/* #include <conio.h> */
#include <stdio.h>
#include <stdlib.h>
#define N 5
void fun (int a[])
{
int i,count = 0;
int k;
for(i = 0; i < N; i++)
{
for(k = count; k < N; k++)
{
printf("i = %d;", i);
printf("k = %d;", k);
printf("count = %d;\n", count);
a = 0;
}
count++;
}
}
void main()
{
FILE *wf;
int a,i,j;
int b= {
{1,9,7,2,4},
{2,3,8,1,2},
{4,5,6,7,5},
{4,0,6,8,0},
{2,7,1,6,4}
};
system("clear");
printf("*****The array*****\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
a=rand()%10;
printf("%4d", a);
}
printf("\n");
}
fun(a);
printf("THE RESULT\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%4d",a);
printf("\n");
}
wf=fopen("out.dat","w");
fun(b);
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
fprintf(wf,"%4d",b);
fprintf(wf,"\n");
}
fclose(wf);
}
运行结果:
*****The array*****
3 6 7 5 3
5 6 2 9 1
2 7 0 9 3
6 0 6 2 6
1 8 7 9 2
i = 0;k = 0;count = 0;
i = 0;k = 1;count = 0;
i = 0;k = 2;count = 0;
i = 0;k = 3;count = 0;
i = 0;k = 4;count = 0;
i = 1;k = 1;count = 1;
i = 1;k = 2;count = 1;
i = 1;k = 3;count = 1;
i = 1;k = 4;count = 1;
i = 2;k = 2;count = 2;
i = 2;k = 3;count = 2;
i = 2;k = 4;count = 2;
i = 3;k = 3;count = 3;
i = 3;k = 4;count = 3;
i = 4;k = 4;count = 4;
THE RESULT
0 0 0 0 0
5 0 0 0 0
2 7 0 0 0
6 0 6 0 0
1 8 7 9 0
i = 0;k = 0;count = 0;
i = 0;k = 1;count = 0;
i = 0;k = 2;count = 0;
i = 0;k = 3;count = 0;
i = 0;k = 4;count = 0;
i = 1;k = 1;count = 1;
i = 1;k = 2;count = 1;
i = 1;k = 3;count = 1;
i = 1;k = 4;count = 1;
i = 2;k = 2;count = 2;
i = 2;k = 3;count = 2;
i = 2;k = 4;count = 2;
i = 3;k = 3;count = 3;
i = 3;k = 4;count = 3;
i = 4;k = 4;count = 4;
只需要看void fun (int a[])这个函数里面的内容就行。 因为你的fun()函数中,在执行完第一次外循环后,内层for循环执行了次,count=5,在执行第二次外循环的时候,进入内层循环,k=count,k=5,直接就不符合条件k<N,因此内层循环不执行,count也不在变化,一直到外循环结束,也就是说外循环执行了一次后,内循环就进不去了,外循环的后几次都是在空转。。。 cosmos3919 发表于 2020-1-9 21:56
先回答你的疑惑吧
你把count++;移到外层for循环内,内层for循环外就对了。详细可以参阅我改后的代码
...
有的区别,我的目的是让左下为0,不过思路get 道了,谢谢了
页:
[1]