动态分配和数组

时间:2016-07-02 18:00:00

标签: c arrays dynamic-allocation

我的问题是重新定义第二个功能

float *rowavg(float *matrix,int rows,int cols)

所以我想假设动态分配带有行元素的浮点数组,如果分配失败则返回NULL。我想我做对了,对吧?我试图做的另一部分是将新数组的第i个元素设置为前一个数组的第i行中的值的平均值。这部分是我起床的地方。我是否正确调用了前一个数组(我相信不是)?

#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);

#define MAX_DIM 10

int main(void)
{
    int done = 0;
    int rows, cols;
    float *dataMatrix;
    float *rowAveVector;
    float *colAveVector;
    float overallAve;

    while (!done)
    {
    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);
    }
}
float *readMatrix(int rows, int cols)
//Dynamically allocate an array to hold a matrix of floats.
//The dimension of the matrix is numRows x numCols.
//If the malloc fails, return NULL.
//Otherwise, prompt the user to enter numRows*numCols values
//for the matrix in by-row order.
//Store the values entered by the user into the array and
//return a pointer to the array.

{

    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)
{

    int i=0;
    float mean=0;
    float *mat=malloc(rows*sizeof(float));
    if(mat==NULL)
    {
        printf("error\n");
        return NULL;
    }
    for (i=0;i<rows;i++)
    {
        readMatrix(rows,cols);
        mean=
        mat[i]=mean;
    } 
}

1 个答案:

答案 0 :(得分:0)

首先,readMatrix(rows,cols);中不需要调用rowavg

如果您希望rowavg返回矩阵中每行的平均值,请尝试以下操作:

float *rowavg(float *matrix, int rows, int cols)
{
    // check matrix befor use
    if (matrix == NULL)
    {
        return NULL;
    }
    int i=0;
    int j=0;
    double mean; // variable name from original code in the question
    float *avgarr = (float *)malloc(rows*sizeof(float));
    if(avgarr == NULL)
    {
        return NULL;
    }
    for (i=0;i<rows;i++)
    {
        mean = 0.0;
        for (j=0;j<cols;j++)
        {
            mean += matrix[i*cols+j];
        }
        avgarr[i] = (float)(mean/cols);
    }
    return avgarr; 
}

因为main已经有了

    rowAveVector = rowAverage(dataMatrix, rows, cols);
    if(rowAveVector == NULL)
    {
        printf("malloc failed.  Terminating program\n");
        return (0);  // here consider return value different from 0
    }

您不应在printf("error\n");中使用rowavg

在不再需要分配内存之后考虑使用free