Dynamic allocation and Arrays to find average : Alright did i implement `float overallavg(float* matrix, int rows, int cols)`correctly? I think i didn't. I'm trying to Return the average value of the elements of the array pointed to by matrix. Should I call the other two functions into the overall one and then divide by the total number elements in the array?     

    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;
    
    
    }