例题:请编写函数fun,其功能是:将所有大于1且小于整数 n的非素数存入a所指数组中,非素数的个数通过m传回。
例如,若输入11,则应输出4 6 8 9 10
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
代码如下:
#include<conio.h>
#include<stdio.h>
void fun(int n,int*m,int a[])
{
int i,j;
int t=0;
for(i=2;i<n;i++)
{
j=2;
while(j<i)
{
if(i%j==0)
{
a[t]=i;
t++;
break;
}
j++;
}
*m=t;
}
}
main()
{
int k,h,c[100];
FILE*out;
printf("\nPlease enter an nteger number between 10 and 100:");
scanf("%d",&h);
fun(h,&k,c);
printf("\n\nThere are %d nonprime numbers less than %d:",k,h);
for(h=0;h<k;h++)
printf("%4d\t",c[h]);
out=fopen("outfile.dat","w");
fun(10,&k,c);
fprintf(out,"%d\n",k);
for(h=0;h<k;h++)
fprintf(out,"%d\n",c[h]);
fclose(out);
}
输出运行窗口如下:
越努力越幸运!
加油,奥力给!!!