0

I'm dealing with arbitrary dimension matrix/array, therefore I need to access the multi-dimensional array using pointers.

I have attempted to write a function that extracts a specific column of the multi-dimensional in C:

I have followed several accepted answers: How to use pointer expressions to access elements of a two-dimensional array in C?

Here is my C code:

#include <stdio.h>

//prototype
void get_column_vector_from_matrix (float * info, int rows, 
        int column_dimension, float * single_dimension);

//main
int main()
{

  //test data 
      float points[][3] = {{3, -2, 1}, 
                        {17, 15, 1}, 
                        {13, 15, 1}, 
                        {6, 12, 12}, 
                        {9, 1, 2}, 
                        {-1, 7, 2}, 
                        {10, 17, 2},
                        {10, 20, 2}}; 


    int rows = sizeof(points)/sizeof(points[0]); 

    float single_dimension [rows]; 
    //extract column vector 0, therefore 3,17,13,7,9,-1,10,10 should be returned
    get_column_vector_from_matrix(&points,rows,0,single_dimension); 

 printf("Column 0\n");
    for (int i=0; i<rows; i++)
   {
          printf("data[%i]=%f\n",i,single_dimension[i]);
   }

      //extract column vector 1, therefore -2,15,15,12,1,7,17,20 should be returned
    get_column_vector_from_matrix(&points,rows,1,single_dimension); 
 printf("Column 1\n");
    for (int i=0; i<rows; i++)
   {
          printf("data[%i]=%f\n",i,single_dimension[i]);
   }

      //extract column vector 2, therefore 1,1,1,1,2,2,2,2 should be returned
    get_column_vector_from_matrix(&points,rows,2,single_dimension); 
 printf("Column 2\n");
    for (int i=0; i<rows; i++)
   {
          printf("data[%i]=%f\n",i,single_dimension[i]);
   }

}

/*=============================================================================
Function:       get_column_vector_from_matrix
Description:    this function points to an arbitrary multidimensional array of  
 *              dimensions m(rows) by  n(columns) and extracts a single dimension
 *              with all rows. This attempts to extract column vector given a 
 *              m(rows) by n(columns) matrix.The pointer to extracted column is
 *              set to  float * single_dimension parameter. 
 *              
=============================================================================*/
void get_column_vector_from_matrix (float * info, int rows, 
        int column_dimension, float * single_dimension)
{

    int row_i=0;  
    float value = 0; 
    if (info!=NULL && single_dimension!=NULL)
        {
        //return data only at column_dimension
         for ( row_i; row_i< rows; row_i++ ) {
             //(*(info+row_i)+column_dimension) below is based on https://stackoverflow.com/questions/13554244/how-to-use-pointer-expressions-to-access-elements-of-a-two-dimensional-array-in/13554368#13554368

          value = (float)(*(info+row_i)+column_dimension);
          *(single_dimension+row_i) = value; 
       }//end 
    }
}

Call to get_column_vector_from_matrix(&points,rows,1,single_dimension);

Should return extracting should return 3,17,13,7,9,-1,10,10 however its returning

Column 0
data[0]=3.000000
data[1]=-2.000000
data[2]=1.000000
data[3]=17.000000
data[4]=15.000000
data[5]=1.000000
data[6]=13.000000
data[7]=15.000000

cyber101
  • 2,822
  • 14
  • 50
  • 93

2 Answers2

0

You are not modifying the offset to compensate for the number of columns in your matrix. In memory, the multidimensional array is flat (i.e. 1-dimensional), and it's up to your program to use an appropriate step size to skip over each row.

Since you seem to want your function to operate on arbitrary dimensions, then you must pass the actual number of columns as a parameter (e.g. num_cols), and change your code as follows:

value = info[row_i * num_cols + column_dimension];
single_dimension[row_i] = value;
paddy
  • 60,864
  • 6
  • 61
  • 103
  • yes your right , i modified the main extraction loop to look like this : `//extract data only along column_dimension for ( row_i; row_i< row_length; row_i++ ) { //based on value = *(info+(row_i * column_length + column_dimension)); *(out_put_array+row_i) = value; }//end` – cyber101 Oct 17 '19 at 18:38
  • were is good resource for practicing multidimensional array, specifically in C with pointers. I find that a lot pointer tutorials are to trivial. – cyber101 Oct 17 '19 at 18:39
0

notice that the parameter info is of type float*, means every step of its increment is always a single float value. You should use pattern like *(info + row_i * NUMBER_OF_COLUMNS + column_dimension) to access the column data.

user8510613
  • 1,242
  • 9
  • 27