My question is regrading the second function
float *rowavg(float *matrix,int rows,int cols)
So i'm suppose to dynamically allocate an array of floats with row elements and return NULL if allocation fails. I think i did this right, right? The other part that i'm trying to do is set the ith element of the new array to the average of the values in the ith row of the previous array. This part is where I'm getting got up. Did i call the previous array correctly(I believe no)?
#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;
}
}
First of all, call of readMatrix(rows,cols);
not needed in rowavg
.
And if you want rowavg
to return average values for each row in your matrix, try the following:
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;
}
And because in main
you already have
rowAveVector = rowAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0); // here consider return value different from 0
}
you should not use printf("error\n");
in rowavg
.
And think over using free
after allocatied memory not needed any more.