第二次作业

基础作业
请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。
 输入:
请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。
 输出
在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件。

1).实验代                                                                                                  导图https://www.edrawsoft.cn/viewer/public/s/6a1fe257016539

```c

第二次实验代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
 FILE *fp;
 int a[10],b,i,c;
 if((fp=fopen("C:\\Users\\Lenovo\\Desktop\\text.txt","a+"))==NULL)
 {
  printf("File open error!\n");
  exit (0);
 }
 fscanf(fp,"%d,",&i);
 printf("%d ",i);
 for(b=0;b<i;b++){
  fscanf(fp,"%d",&a[b]);
  printf("%d ",a[b]);
  if(b+1==i){
   printf("\n");
  }
 }
 for(c=b=0;b<i-1;b++){
 if(a[c]<a[b+1]){
     c=b+1;
    }
 }
 printf("%d  %d",a[c],c);
 fprintf(fp,"\n%d  %d",a[c],c);
 if(fclose(fp)){
  printf("Can not close the file!\n");
  exit(0);
 }
 return 0;
}#include<stdio.h>
#include<stdlib.h>
int main()
{
 FILE *fp;
 int a[10],b,i,c;
 if((fp=fopen("C:\\Users\\Lenovo\\Desktop\\text.txt","a+"))==NULL)
 {
  printf("File open error!\n");
  exit (0);
 }
 fscanf(fp,"%d,",&i);
 printf("%d ",i);
 for(b=0;b<i;b++){
  fscanf(fp,"%d",&a[b]);
  printf("%d ",a[b]);
  if(b+1==i){
   printf("\n");
  }
 }
 for(c=b=0;b<i-1;b++){
 if(a[c]<a[b+1]){
     c=b+1;
    }
 }
 printf("%d  %d",a[c],c);
 fprintf(fp,"\n%d  %d",a[c],c);
 if(fclose(fp)){
  printf("Can not close the file!\n");
  exit(0);
 }
 return 0;}

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/ydlBLOG/p/10501992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值