如何在许多功能中使用相同的多维数组

How to use the same multidimensional array in many functions?

本文关键字:数组 多功能      更新时间:2023-10-16

我是C 的初学者,老实说,我不知道如何解决一个任务。我必须使用二维数组创建一个矩阵。它的大小应取决于用户的输入(应该像... int matrix [m] [n],其中m和n是用户输入的数字)。然后,我应该用0到100的随机数填充并打印。好吧,我可以管理它。当我必须创建一个函数从此数组的行中找到最高数字时,问题就开始了。此功能的唯一参数可以是用户输入的行数(例如int函数(int i))。问题是我可以在多个功能中使用相同的数组吗?考虑到我是新手的事实,有什么办法可以做到这一点?还是任务形成不正确?对不起,很长的帖子,并提前感谢PS有人要求代码,所以这里是:

#include <iostream>
#include <cstdlib>
using namespace std;
int function1(int i)
{
//this is one of the functions I'm supposed to create-I described it earlier
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n];
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1);
    cout<<A[a][b]<<" ";
}
cout<<"n";
}
}

编辑:我要感谢大家的帮助。我问我的老师,他终于回应了。如果您好奇的话,他告诉我们(我没有听到)定义像INT [100] [100]或更高的数组,并且不允许用户输入任何更高的数字;)这不是最佳解决方案实用之一。再次感谢您!

在C 中执行此操作的正确方法是使用std :: vector或std :: array。

如果您由于人工要求而无法执行此操作,那么基于用户输入的C 中的2D数组根本就无法做到这一点。

cin >> m >> n;
...
int array [m][n];  // not possible
int** wannabe;     // not an array
int array [m * n]; // not possible

您可以做的是"弄糊"的2D数组:

int* mangled = new int[m * n];

使用的示例:

class int_matrix
{
  private:
    int*   mangled;
    size_t rows;
    size_t cols;
  public:
    int_matrix(size_t row, size_t col)
      :rows(row),
       cols(col)
    {
      mangled = new int[row * col];
    }
    int highest_in_row (size_t row)
    {
      ...
    }
};

请注意,此代码要求您遵循三个规则。


在C中,您只会通过编写int array[m][n]来优雅地解决了这一点,但是您正在使用C ,因此无法做到。

您可以将您的功能包装到类中。在该课程中,您可以将数组作为成员变量。

    class A { 
      int **matrix;
      public:
       A(int rows, int columns) {
        matrix = new int*[rows];
        for(int i = 0; i < rows; ++i)
          matrix[i] = new int[columns];
       }
   int function(int i); //you can use your matrix in this function
}

如果您不能使用类,则可以使用全局变量。

在file.cpp

    int **matrix;
    int function(int i) {
       //Do Something
    }
//With rows the number of rows and columns the number of columns
//You can take these as parameters
    int main() {
       matrix = new int*[rows];
        for(int i = 0; i < rows; ++i)
          matrix[i] = new int[columns];
       function(42);
    }

如果您声明了像int int A[m][n];这样的矩阵,其中 mn不是 const,则不能将其传递给函数。有两种修复方法:

1)声明具有int A[10][10];等常量大小的矩阵。在这种情况下,找到Max的功能将如下所示:

int max_in_row(int matr[10][10], int row) {
    int max = 0;
    for (int col = 0; col < 10; ++col)
        if (matr[row][col] > max)
            max = matr[row][col];
    return max;
}

您可以找到简单的最大值作为int max = max_in_row(A, <row you want>);

2)(如果您不知道尺寸)将矩阵声明为数组:

int **A = new int*[n];
for (int i = 0; i < n; ++i)
    A[i] = new int[m];
// fill A like you did

然后该功能看起来像

int max_in_row(int **matr, int row, int m) {
    int max = 0;
    for (int col = 0; col < m; ++col)
        if (matr[row][col] > max)
            max = matr[row][col];
    return max;
}

您可以通过int max = max_in_row(A, <row you want>, m);

找到MAX

以下不是标准的C ,因为仅当编译器支持可变长度阵列时,它才能起作用。VLA是在C99中引入的,并在C11中进行了可选,但从未在C 标准中引入 - 但有些编译器即使在C 模式下也支持它。

黑客将将矩阵地址存储为全局void *,并将其投放到函数内部的VLA的适当指针。需要此黑客攻击,因为在全局声明的那一刻,您不知道矩阵的列数。

#include <iostream>
#include <cstdlib>
void *Matrix;
int Columns;
using namespace std;
int function1(int i)
{
    typedef int MAT[Columns];   // BEWARE!!! VLA is not standard C++
    MAT *mat = static_cast<MAT *>(Matrix);
    int mx = mat[i][0];
    for(int j=0; j<Columns; j++) {
        cout << " " << mat[i][j];
        if (mat[i][j] > mx) mx = mat[i][j];
    }
    std::cout << endl;
    return mx;
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n];         // BEWARE!!! VLA is not standard C++
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1);  // Note that I now use a and b here !
    cout<<A[a][b]<<" ";
}
cout<<"n";
}
Matrix = static_cast<void *>(A);
Columns = n;
cout << "Enter row number to process: ";
cin >> a;
b = function1(a);
cout << "Max of row " << a << " is " << b << endl;
return 0;
}

不是真正的c - ish,但至少它会编译并通过Clang版本3.4.1

给出预期的结果。