简体   繁体   中英

How to pass and return 2d array to a function

I'm trying to change a 2D array values in different function so it suits my need. For an example in the code below I'm trying to change the Matrix to reflexive by changing the to "1" .

I have an issue returning the new array and replace it with the old one in the main program so I can use it again.

int reflexive(int m, int n, int matrix[100][100])
{

 for(int i = 1;i <= m; i++)
  {
    for(int j = 1; j <= n; j++)
    {
        if(i == j)
        {
            if(matrix[i][j] != 1)
                matrix[i][j] = 1;
        }
 return matrix;
     }
   }
 }
int main()
{
    int  matrix[100][100];
    int m , n;
    for(int i = 1;i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cin>>matrix[i][j];
        }
    }

    matrix[m][n] = reflexive(m,n,matrix);
 return 0;
}

I want to fix this code so it can return the full new 2D array to the main program.

In your function reflexive the return statement is inside the loop. This causes that the function is terminated immediately when the return statement is reached. If you would proper format your code you would be able to see this behavior easily.

Further note, that the first element in an array has the index 0 and the index of the last element is the the size of the array -1. This is, because the array index is the element "offset". This means that

a[i]

and

*(a + i)

access the same array element.

for(int i = 1;i <= m; i++)      // <--- should be  for(int i=0; i<m; i++)
{
    for(int j = 1; j <= n; j++) // <--- should be for(int j=0; j<n; j++)
    {
        if(i == j)
        {
            if(matrix[i][j] != 1)
                matrix[i][j] = 1;
        }
        return matrix;          // <--- here the function is terminated
    }
}

An array is passed to a function by a pointer and not by value. This means if a formal parameter of a function is an array, then not all the values of the actual parameter are copied to the array. There is only passed a pointer to the values of the paramter to the function.
Because of that you do not need any return value in your case.

The following code should do what you want:

int reflexive(int m, int n, int matrix[100][100])
{
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(i == j)
            {
                if(matrix[i][j] != 1)
                    matrix[i][j] = 1; // <----- write to the data by pointer
            }
        }
    }
}

int main()
{
    int  matrix[100][100];
    ......
    reflexive(m,n,matrix);
    ......
}

使用vector<vector<int>>并通过引用传递

Using an array here is tedious and error-prone. Therefore:

#include <vector>

std::vector<std::vector<int> > reflexive(std::vector<std::vector<int> > matrix)
{
    for(int i = 0; i < matrix.size; i++){
        for(int j = 0; j < matrix[i].size; j++){
....

int main()
{
    std::vector<std::vector<int> > matrix(100, std::vector<int>(100));
    int m , n; //<<whereever they come from I guess? Saw no cin in your code. Have no set value right now.
    for(int i = 0; i < m; i++)
    {
        for(int j =0; j < n; j++)
        {
            cin>>matrix[i][j];
        }
    }
}

or

#include<array>

...

std::array<std::array<int, 100>, 100> matrix;

std::array is of fixed size, std::vector is dynamic. Also learn about std::list while you're at it.

In any case, you should find a basic tutorial that talks about them and read all of it. Important standards.

That said, especially if you want to use dynamic sized arrays, you have no guarantee that every sub-array is of the same size ("a jagged 2d array), which is somehwat okay in a small program but should be avoided in the big picture, by using a class that ensures this property.

In the case of a matrix, one easy option is to use a library that deals with matrices, like for example Eigen.

edit:

If at some point you need the code above in const-correct:

std::vector<std::vector<int> > reflexive(const std::vector<std::vector<int> >& input)
{
    std::vector<std::vector<int> > output = input;
    ....
    return output;
}

(or make it void if it is allowed to modify the original matrix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM