0

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

1 Answer 1

1

is this the proper way to do it?

You are heading in the right direction but not fully there.

Also did i return the pointer to the array properly?

No.

You can allocate memory for a matrix using one of two methods.

  1. Allocate numRows numbers of float*s. For each row, allocate numCols of floats and then return a pointer to the array of float*s. This is what you tried but you didn't do everything right.

    Take a closer look at the modified code for reading user data and the return statement.

    float **matrix(int numRows, int numCols)    
    {    
        int i=0;
        float **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] == NULL)
           {
              printf("error\n");
              exit(1);
           }
        }
    
        printf("Enter values for the matrix: ");
        for (i = 0; i < numRows; ++i )
        {
           for (int j = 0; j < numCols; ++j)
           {
              scanf("%f", &m[i][j]);
           }
        }
        return m;
    }
    
  2. Allocate numRows*numCols of floats. Treat the 1D array as holder of 2D data. Use appropriate offsets to treat the 1D array as a 2D array. Return a pointer to the array of floats.

    float *matrix(int numRows, int numCols)    
    {    
        int i=0;
        float *m = malloc(numRows*numCols*sizeof(float));
        if(m==NULL)
        {
           printf("error\n");
           exit(1);
        }
        printf("Enter values for the matrix: ");
        for (i = 0; i < numRows; ++i)
        {
           for (int j = 0; j < numCols; ++j)
           {
              int index = i*numCols+j;
              scanf("%f", &m[index]);
           }
        }
        return m;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.