文件IN.DAT中存有200个整数,函数read_dat(int inBuf[MAX])负责将这200个数读到数组inBuf[]中。请编制程序,要求;求出数组inBuf[]中最大数max及最大数的个数count和数组inBuf[]中能被3整除或能被7整除的

admin2012-07-20  38

问题 文件IN.DAT中存有200个整数,函数read_dat(int inBuf[MAX])负责将这200个数读到数组inBuf[]中。请编制程序,要求;求出数组inBuf[]中最大数max及最大数的个数count和数组inBuf[]中能被3整除或能被7整除的数的算术平均值average(保留2位小数),结果输出列OUT.DAT中。
部分程序、读函数read_dat(int inBuf[MAX])及输出格式已给出。
试题程序;
#include
#include
#define MAX 200
void read dat(int inBuf[MAX])
{
  int i,j;
  FILE*fp;
  fp=fopen("IN.DAT","r");
  for(i=0;i<20;i++)
  {
    for(j=0;j<10;j++)
    {
    fscanf(fp,"%d,",&inBuf[i*10+
    j]
}
    printf("%d",inBuf[i*10+j]);
    }
    printf("n");
    }
    fclose(fp);
   }
  void maln()
  {
    int count,inBuf[MAX],max;
    float average;
    FILE*fw;
    int i,k=0;
    long j;
    system("CLS");
    fw=fopen("OUT.DAT","w");
    read dat(inBuf);
    average=0;count=0;max=0;j=0;
    printf("\n\nmax=%d,count=%d,aver
    age=%6.2f\n",max,count,average);
    fprintf(fw,"%d\n%d\n%6.2f\n",max,
    count,average);
    fclose(fw);
  }

选项

答案 for(i=0;imax) //如果某个数比先前确定的最大值大 max;inBuf[i]; //则将该数设为新的最大值 for(i=0;i
解析 本题主要考查依据数组下标对数据进行筛选及平均值的求法。程序基本步骤是;1.对200个整数进行遍历,求出最大值max,再统计出最大值的个数。2.求出能被3或7整除的数,所以两个条件之间用"或"运算,将满足条件的数累加到j中。3.最后用j/k求其平均值。
转载请注明原文地址:https://kaotiyun.com/show/524Z777K
0

最新回复(0)