so at run time we don't know the size of the array(matrix) and I want the user to input the elements of the array(matrix). is this the proper way to do it?Also did i return the pointer to the array properly?
#define MAX_DIM 10
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
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);
}
float *matrix(int numRows, int numCols)
{
int i=0;
float **m= NULL;
m=malloc(numRows*sizeof(float*));
if(m==NULL)
{
printf("error\n");
exit(1);
}
for(i=0;i<numRows;i++)
{
m[i]=malloc(numCols*sizeof(float));
}
if(m[i-1]==NULL)
{
printf("error\n");
exit(1);
}
printf("Enter values for the matrix: ");
scanf("%f",m[i]);
return m[i];
}