Home > database >  How to call a function containing multidimensional array?
How to call a function containing multidimensional array?

Time:01-07

#include <iostream>
#define MAX 100
using namespace std;

void compute_minor_sums (int a[][MAX], int B[][MAX], int n1, int n2){
    int sum=0,row[n2],col[n1];
    for(int i=0;i<n1;i  ){
        for(int j=0;j<n2;j  ){
            cin>>a[i][j];
            sum =a[i][j];
        }
    }
    for(int i=0;i<n1;i  ){
        for(int j=0;j<n2;j  ){
            row[i] =a[i][j];
            col[j]=col[j] a[i][j];
        }
    }
   for(int i=0;i<n1;i  ){
        for(int j=0;j<n2;j  ){
            B[i][j]=sum-row[i]-col[j] a[i][j];
        }
    }

}

int main()
{
    int n1,n2;cin>>n1>>n2;
    int a[n1][n2],B[n1][n2];
     compute_minor_sums ( a, B,n1,n2);
    }

Program is about computing minor of an n1*n2 matrix. I tried calling a function which computes minor.But I don't know exactly why is this giving these kind of errors? The first errors is:

Cannot convert int (*)[n2] to int int (*)[100]

The second error is:

Initialising argument 1 of void compute_minor_sums (int a[][MAX], int B[][MAX], int n1, int n2)

I can't use gdb as these errors are syntax errors. Any help would be appreciated

CodePudding user response:

The code is rewritten with vector.

(I'm not sure about matrix calculation logic.)

There would be memory bound errors if matrices are not square.

#include <iostream>
#include <vector>
#define MAX 100
using namespace std;

void compute_minor_sums(vector<vector<int>>& a, vector<vector<int>>B, int n1, int n2) {
    int sum = 0;
    vector<int> row(n2);
    vector<int>col(n1);
    for (int i = 0; i < n1; i  ) {
        for (int j = 0; j < n2; j  ) {
            cin >> a[i][j];
            sum  = a[i][j];
        }
    }
    for (int i = 0; i < n1; i  ) {
        for (int j = 0; j < n2; j  ) {
            row[i]  = a[i][j];
            col[j] = col[j]   a[i][j];
        }
    }
    for (int i = 0; i < n1; i  ) {
        for (int j = 0; j < n2; j  ) {
            B[i][j] = sum - row[i] - col[j]   a[i][j];
        }
    }

}

int main()
{
    int n1, n2;
    cin >> n1 >> n2;
    vector<vector<int>> a(n1, vector<int>(n2)); // n1 x n2 matrix
    vector<vector<int>> B(n1, vector<int>(n2)); // n1 x n2 matrix
    compute_minor_sums(a, B, n1, n2);
}

CodePudding user response:

To call a function containing a multidimensional array, you will need to specify the indices for each dimension of the array.

Here is an example of how to call a function with a 2D array:

def print_array(arr): print(arr[i][j])

Declare a 2D array

arr = [[1, 2, 3], [4, 5, 6]]

Call the function and pass the array as an argument

print_array(arr)

This will print the elements of the array in a row-column format.

You can use the same approach to call a function with a multidimensional array of any size, by specifying the indices for each dimension of the array.

CodePudding user response:

There are many issues with your code, I'll start with the few that are visible. As other users have mentioned in the comments - VLA doesn't exist in C . What is a VLA?

The size of VLA's is determined during runtime and not during compilation. So the following code won't compile because values n1 and n2 are determined during runtime by the user.

int main()
{
  int n1,n2;
  std::cin >> n1 >> n2; // <-- we get values n1 and n2 during runtime from user input
  int a[n1][n2],B[n1][n2];
  //...
}

The following code will compile:

int main()
{
  const int n1 = 1;
  const int n2 = 2; // known during compilation and guaranteed not to change
  int a[n1][n2],B[n1][n2];
  int c[1][2]; // the same as using const ints - 1 and 2 are known during compilation
  //...
}

So you might be asking:

What can we do if we do not know the size of the C-style array during compilation?

The answer is, you can allocate the array on the heap, however to have a 2D array is no longer simple, i.e.:

int main()
{
  int n1,n2;
  std::cin >> n1 >> n2; // <-- we get values n1 and n2 during runtime from user input
  int **a = new int*[n1];
  for (int i = 0; i < n1;   i) {
    a[i] = new int[n2];
  }
}

We are as you can see creating pointers to pointers to int. Because array is just that - a pointer to an int which is the beginning of the array of int and the rest of the values are just offsets of the previous element.

We can also simplify the above by doing the following:

int* a = new int[n1 * n2];

// a[i][j] is then
a[i * n1   j]

There are other issues with your code, but one thing at a time.

If you wish to pass it to the function, well you have more options now, but simply passing the pointer to the beginning of the array (i.e. a) is sufficient, however then you have to add some additional bound-checking logic. Hence why one of the answers suggested an std container called std::vector because these containers do all of this for you, with minimal overhead.

EDIT: The other issues with your code are for example here:

row[i] =a[i][j];
col[j]=col[j] a[i][j];

You do not initialize these arrays in your code, I'll be honest I'm not sure what is the default defined behavior for non-VLAs but when you allocate something on the heap as mentioned in my answer. You will get (most-likely) into undefined-behavior land with random values. So be sure to initialize row/col with default values (since you are doing mostly additions, you want to initialize with 0s)

  • Related