m0_61828817 2021-11-17 03:02 采纳率: 70%
浏览 70
已结题

不会写这个c语言,高抬贵手

Write a program to accomplish all of the following task. Each task should be handled by independent function. The functions complete these tasks should take the entire array as an argument.

(1) Read four sets of five double numbers each from a text file named “data.txt”, store the numbers in a 4*5 array which is defined in main() function. This function has no return value.
(2) Compute and show the average of each set of five values. This function has no return value.
(3) Compute the average of all the values in the array. Return the result.
(4) Determine the largest value of the entire array. Return the result.
Write a main() function to test all functions and show the returned value. You can prepare the text file with necessary data for the program running.
急,多谢多谢

  • 写回答

1条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    #include <stdio.h>
    #define Y 4
    #define X 5
    
    void read(double a[Y][X])
    {
        FILE * fp = fopen ("data.txt", "r");
        int i,j;
        for( i=0; i<Y; i++ )
        {
            for ( j = 0; j < X; j++)
               fscanf(fp, "%lf", &a[i][j]);
        }
    }
    
    void avg(double a[Y][X])
    {
        int i,j;
        double sum;
        for( i=0; i<Y; i++ )
        {
            sum = 0;
            for ( j = 0; j < X; j++)
               sum += a[i][j];
            printf("第%d组的平均值:%lf\n", i+1, sum/X);
        }
    }
    
    double allavg(double a[Y][X])
    {
        int i,j;
        double sum;
        sum = 0;
        for( i=0; i<Y; i++ )
        {
            for ( j = 0; j < X; j++)
               sum += a[i][j];
        }
        return sum/(Y*X);
    }
    
    double allmax(double a[Y][X])
    {
        int i,j;
        double max = a[0][0];
        for( i=0; i<Y; i++ )
        {
            for ( j = 0; j < X; j++)
                if (max < a[i][j])
                    max = a[i][j];
        }
        return max;
    }
    
    int main()
    {
        double a[Y][X];
        read(a);
        avg(a);
        printf("所有值的平均值:%lf\n", allavg(a));
        printf("所有值的最大值:%lf\n", allmax(a));
        return 0;
    }
    

    img

    img

    如有帮助,望采纳!谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月25日
  • 已采纳回答 11月17日
  • 创建了问题 11月17日

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据