|
10鱼币
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define N 5
void fun (int a[][N])
{
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[k][i] = 0;
count++;
}
}
}
void main()
{
FILE *wf;
int a[N][N],i,j;
int b[N][N]={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[i][j]=rand()%10;
printf("%4d", a[i][j]);
}
printf("\n");
}
fun(a);
printf("THE RESULT\n");
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
printf("%4d",a[i][j]);
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[i][j]);
fprintf(wf,"\n");
}
fclose(wf);
/*****************************/
}
本帖最后由 cosmos3919 于 2020-1-9 22:08 编辑
先回答你的疑惑吧
你把 count++; 移到外层for循环内,内层for循环外就对了。详细可以参阅我改后的代码
下面是建议:
1.提问时建议写明你这段代码的用途、适用环境等尽可能多的基本信息
就比如你代码最开始引用了conio.h,这并不是标准c的东西,我的编译器就通不过,拦住了我好一会儿,还好linux系统内建命令支持system("clear")能达到相同的效果。你如果用我的代码运行,这个地方得得改回去^_^
2.你程序用了二维数组,而你代码中却有a[k]=0;,不知你怎么编译通过的,二维数组的a[k]是一个指针,用整数0赋值不知道对不对,我感觉会有问题(主要不知道这个代码的作用),我修改了一下,不知与你的目的是否一致,下面是我改后的代码和运行结果: /* #include <conio.h> */
#include <stdio.h>
#include <stdlib.h>
#define N 5
void fun (int a[][N])
{
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[i][k] = 0;
}
count++;
}
}
void main()
{
FILE *wf;
int a[N][N],i,j;
int b[N][N]= {
{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[i][j]=rand()%10;
printf("%4d", a[i][j]);
}
printf("\n");
}
fun(a);
printf("THE RESULT\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%4d",a[i][j]);
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[i][j]);
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;
|
最佳答案
查看完整内容
先回答你的疑惑吧
你把 count++; 移到外层for循环内,内层for循环外就对了。详细可以参阅我改后的代码
下面是建议:
1.提问时建议写明你这段代码的用途、适用环境等尽可能多的基本信息
就比如你代码最开始引用了conio.h,这并不是标准c的东西,我的编译器就通不过,拦住了我好一会儿,还好linux系统内建命令支持system("clear")能达到相同的效果。你如果用我的代码运行,这个地方得得改回去^_^
2.你程序用了二维数组,而你代 ...
|