我是否正确实施了float overallavg(float* matrix, int rows, int cols)
?我想我没有。我试图返回矩阵指向的数组元素的平均值。我应该将其他两个函数调用到整个函数中,然后除以数组中的总数元素吗?
#include <stdio.h>
#include <stdlib.h>
float *readMatrix(int rows, int cols);
float *rowavg(float *matrix, int rows, int cols);
float *colavg(float *matrix, int rows, int cols);
float overallavg(float* matrix, int rows, int cols);
void printavg(float *matrix, float *rowAve, float *colAve, float overallAve, int rows, int cols);
#define MAX_DIM 10
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
float *rowAveVector;
float *colAveVector;
float overallAve;
while (!done)
{
// Prompt user to enter row and column dimensions of matrix (must be > 0)
do
{
printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &rows);
} while(rows <= 0 || rows > MAX_DIM);
do
{
printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &cols);
} while(cols <= 0 || cols > MAX_DIM);
dataMatrix = readMatrix(rows, cols);
if (dataMatrix == NULL)
{
printf ("Program terminated due to dynamic memory allocation failure\n");
return (0);
}
rowAveVector = rowAverage(dataMatrix, rows, cols);
colAveVector = colAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
overallAve = overallAverage(dataMatrix, rows, cols);
//Print Averages
printAverages(dataMatrix, rowAveVector, colAveVector, overallAve, rows, cols);
free(dataMatrix);
free(rowAveVector);
free(colAveVector);
//Check if user wants to enter a new matrix
printf("Enter 0 to continue with a new matrix\n");
printf("Enter any other number to terminate the program: ");
scanf("%d", &done);
}
//That's it, we are done
return (0);
}
float *readMatrix(int rows, int cols)
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *readMatrix(int rows, int cols)
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(rows*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
mean+=matrix[i*cols+j];
}
Average_array[i]=(float)(mean/cols);
}
return Average_array;
}
float *colavg(float *matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean=0;
float *Average_array=malloc(cols*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for (i=0;i<cols;i++)
{
for (j=0;j<rows;j++)
{
mean+=matrix[j*cols+i];
}
Average_array[i]=(float)(mean/rows);
}
return Average_array;
}
float overallavg(float* matrix, int rows, int cols)
{
if (matrix==NULL)
{
return NULL;
}
int i=0;
int j=0;
float mean_1=0;
float mean_2=0;
float avg=0;
float elements=0;
float sum=0;
elements=rows*cols;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
mean_1+=matrix[i*rows+j];
}
}
for (i=0;i<cols;i++)
{
for (j=0;j<rows;j++)
{
mean_2+=matrix[j*cols+i];
}
}
sum=mean_1+mean_2;
avg=sum/elements-1;
}
答案 0 :(得分:1)
以下代码:
#include
语句注意:这一行:
avg=sum/elements-1;
不会计算所有元素的平均值,而是计算一个稍大的值,因为元素的总数减少1
建议,在函数中:overallavg()
不计算单独的'均值'值,而是简单地逐步遍历整个数组求和每个元素,然后除以元素总数
建议,使用4个空格的缩进宽度,因为即使使用可变宽度字体,也很容易看到。 2个带有可变宽度字体的空格看起来像缩进的行缩进约为字母宽度的1/2。 I.E.几乎看不见,看起来非常凌乱。 (代码被多次读取所以应该写得非常容易阅读。
#include <stdio.h> // printf() scanf()
#include <stdlib.h> // malloc, free()
float *readMatrix( size_t rows, size_t cols)
{
//int i=0;
//int j=0;
size_t elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (size_t i=0; i<rows; i++)
{
for ( size_t j=0; j<cols; j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, size_t rows, size_t cols)
{
if (matrix==NULL)
{
return NULL;
}
//int i=0;
//int j=0;
float mean=0;
float *Average_array=malloc(rows*sizeof(float));
if (Average_array==NULL)
{
return NULL;
}
for ( size_t i=0; i<rows; i++)
{
for ( size_t j=0; j<cols; j++)
{
mean+=matrix[i*cols+j];
}
Average_array[i] = (mean/(float)cols);
}
return Average_array;
}
float *colavg(float *matrix, size_t rows, size_t cols)
{
if (matrix==NULL)
{
return NULL;
}
//int i=0;
//int j=0;
float mean=0;
float *Average_array = malloc( cols*sizeof(float) );
if (Average_array==NULL)
{
return NULL;
}
for ( size_t i=0; i<cols; i++)
{
for (size_t j=0; j<rows; j++)
{
mean+=matrix[j*cols+i];
}
Average_array[i] = (mean/(float)rows);
}
return Average_array;
}
float overallavg(float* matrix, size_t rows, size_t cols)
{
#if 0
if (matrix==NULL)
{
return NULL;
}
#endif
//int i=0;
//int j=0;
float mean_1=0;
float mean_2=0;
float avg=0;
float elements=0;
float sum=0;
elements= (float)rows* (float)cols;
for ( size_t i=0;i<rows;i++)
{
for ( size_t j=0;j<cols;j++)
{
mean_1+=matrix[i*rows+j];
}
}
for ( size_t i=0; i<cols; i++)
{
for ( size_t j=0; j<rows ;j++)
{
mean_2+=matrix[j*cols+i];
}
}
sum=mean_1+mean_2;
avg=sum/elements-1;
return avg;
}