2019年春季学期第二周作业

2019年春季学期第二周作业

基础作业

请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。

输入:

请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。

输出

在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件。

1).实验代码

include<stdio.h>

include<string.h>

include<stdlib.h>

int main(void)
{
FILE*fp;
int index, i,n=7;

if((fp=fopen("D:\\新建文件夹\\Brent.txt","a+"))==NULL){
    printf("File open error!\n");
    exit(0); 
}

fscanf(fp,"%d", &index);
int a [10];
for (i=0;i<n;i++)
   fscanf (fp,"%d,",&a [i]);
for (index=0;index<n;index++)
{
    if (a[0]<a[index])
    {
        a[0]=a[index];
        i=index;
    }
    else if (a[0]==a[index])
    {
        a[0]=a[index];
        if (i>index)
        {
            i=index;
        }
    }
}
fscanf (fp,"%d %d",&a[0],&i);
printf ("%d %d",a[0],i);
fseek(fp, 0L, SEEK_END);

if(fclose(fp)){
    printf("Can not close the file!\n");
    exit(0);
}
return 0;

}

2).设计思路

3).本题过程碰到的问题以及解决办法

这个题目在做的时候就是有些知识点不会处理,然后我的室友他们就去查看了很多资料,就是刚开始要不就写不进,要不就写不出,而我是答案一直输不对,最后一起帮助下完成了。

4).运行结果截图

猜你喜欢

转载自www.cnblogs.com/521-PENG/p/10484206.html