我正在尝试打印函数void printavg(float *matrix,float *rowAve,float *colAve, float overallAve, int rows, int cols);
中的平均值
但是当我试图在打印出行的平均值时进行测试时,我得不到正确的答案。使用printavg
函数打印平均值的正确方法是什么。这是程序应该做的一个例子:
Enter row dimension <must be between 1 and 5>: 3
Enter column dimension <must be between 1 and 5>: 2
Enter 6 data values for 3 x 2 matrix, in by-row order
1
2
3
4
5
6
1.00 2.00 1.50
3.00 4.00 3.50
5.00 6.00 5.50
3.00 4.00
Overall average is: 3.500000
Enter 0 to continue with a new matrix
Enter any other number to terminate the program:
这是我到目前为止的代码
#include <stdio.h>
#include <stdlib.h>
// Function Prototypes
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 15
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 = rowavg(dataMatrix, rows, cols);
colAveVector = colavg(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
overallAve = overallavg(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 *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)
{
float sum=0;
int i=0;
float elements=0;
float mean;
elements=rows*cols;
for (i=0;i<elements;i++)
{
sum+=matrix[i];
}
mean=sum/elements;
return mean;
}
void printavg(float *matrix, float *rowAve, float *colAve, float overallAve, int rows, int cols)
{
printf("%f",rowAve[rows,cols]);
}