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.
急,多谢多谢
你题目的解答代码如下:
#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;
}
如有帮助,望采纳!谢谢!