zoukankan      html  css  js  c++  java
  • 第二次作业

    基础作业
    请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从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! ");
      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(" ");
      }
     }
     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," %d  %d",a[c],c);
     if(fclose(fp)){
      printf("Can not close the file! ");
      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! ");
      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(" ");
      }
     }
     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," %d  %d",a[c],c);
     if(fclose(fp)){
      printf("Can not close the file! ");
      exit(0);
     }
     return 0;}

     

  • 相关阅读:
    验证码
    九九乘法表
    P121 6.7 第一题和第二题
    二分搜索法(转载自vanezkw)
    用for循环打印菱形
    用while循环语句计算1!+2!+……20!之和
    数的阶乘之和
    9.29
    doGet与doPost的区别
    JavaScript习题
  • 原文地址:https://www.cnblogs.com/ydlBLOG/p/10501992.html
Copyright © 2011-2022 走看看