Home > Enterprise >  Returning a matrix array in C function
Returning a matrix array in C function

Time:01-07

I am trying to write an OLS regression in C . I want the user to specify how many observations and variables goes into the X (independent variable matrix). I want to write this as a function outside the int main() function. But everytime I return the array, I get a type error. How do I specify that I want function input_x to return a matrix/array?

 /******************************************************************************   
                                  Online C   Compiler.    
    *******************************************************************************/
    
    #include <iostream>
    
    using namespace std;
    
    
    int input_x(int num_rows, int num_cols){
        
        
       int mat[num_rows][num_cols];
       int user_input;
           
        for (int i=0; i<num_rows; i  ){
            for (int j=0; j<num_cols; j  ){
                    cout<<"Enter the value X:";
                    cin>>user_input;
                    mat[i][j]=user_input;
                
            }
        }
    
    
       return mat;
    }
    
    
    
    int main()
    {
        int mat[1000][1000];
        int input;
        int rows;
        int cols;
        
        cout<<"How many row?";
        cin>> rows;
        cout<<"How many columns?";
        cin>> cols;
        

        //Enter the X data
        input_x(rows,cols);
        
        
    
    
        return 0;
    }

CodePudding user response:

Mistake 1

In C the size of an array must be a compile time constant. So take for example,

int n = 10;
int arr[n]; //INCORRECT because n is not a compile time constant

The correct way to write the above would be

const int n = 10;
int arr[n]; //CORRECT

Similarly in your code:

int mat[num_rows][num_cols];//INCORRECT because num_rows and num_cols are not constant expressions

Mistake 2

The return type of your function input_x is int.

But the type of mat inside the function input_x is int [num_rows][num_cols] which decays to int (*)[num_cols] due to type decay.

So essentially the return type of your function(int) and what you're actually returning(int (*)[num_cols]) doesn't match and hence you get the following error:

error: invalid conversion from ‘int (*)[num_cols]’ to ‘int’

To solve these problems you should use std::vector instead of built in array.

CodePudding user response:

The value returned by the function is not stored.

input_x(rows,cols); // should be stored in a variable

Also, you have to change the return type of function to a matrix.

int input_x(int num_rows, int num_cols){

To solve the problem:

  1. Create a matrix dynamically
  2. Return the matrix

Here is the code for the above approach.

/******************************************************************************
                                  Online C   Compiler.
    *******************************************************************************/

    #include <iostream>

    using namespace std;


    int** input_x(int num_rows, int num_cols){

       int **mat = (int **)malloc(num_rows * sizeof(int *));
       int user_input;

        for (int i = 0; i < num_rows; i  ) {
            mat[i] = (int *)malloc(num_cols * sizeof(int));
        }
        for (int i=0; i<num_rows; i  ){
            for (int j=0; j<num_cols; j  ){
                    cout<<"Enter the value X:";
                    cin>>user_input;
                    mat[i][j]=user_input;

            }
        }

       return mat;
    }



    int main()
    {
        int** mat;
        int input;
        int rows;
        int cols;

        cout<<"How many row?";
        cin>> rows;
        cout<<"How many columns?";
        cin>> cols;


        //Enter the X data
        mat = input_x(rows,cols);
/*
        for(int i=0;i<rows;i  ){
            for(int j=0;j<cols;j  ){
                cout<<mat[i][j]<<" ";
            }
            cout << endl;
        }
*/



        return 0;
    }

CodePudding user response:

As the comment states, your return type of the function input_x is wrong.

Your matrix mat is an array of arrays of int. This is a c-style array and you can only return it as an int** which would directly lead to a dangling reference.

Instead of using c-style arrays it's better to use standard containers, like std::vector as Fred Larson pointed out.

See the documentation here about std::vector In general https://en.cppreference.com is a good start to see if any standard library type suits your needs.

You can initialize your matrix like this:

std::vector<std::vector<int>> mat(num_rows)
for (auto & row : mat)
    row.resize(num_cols);

Don't forget to include the respective header

#include <vector>
  •  Tags:  
  • Related