如何在许多函数中使用相同的多维数组?

时间:2016-12-16 14:42:19

标签: c++ arrays multidimensional-array

我是C ++的初学者,说实话,我不知道如何解决一个任务。 我必须使用二维数组创建一个矩阵。它的大小应该取决于用户的输入(它应该像... int matrix [m] [n],其中m和n是用户输入的数字)。然后我应该用0到100的随机数填充它并打印出来。好吧,我可以管理它。 当我必须创建一个从该数组的行中找到最高数字的函数时,问题就出现了。该函数的唯一参数可以是用户输入的行数(例如,int function(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]或更高的数组,不允许用户输入任何更高的数字;)这不是一个最佳解决方案,但肯定是实用的。再次感谢你!

4 个答案:

答案 0 :(得分:1)

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

如果由于人工要求而无法做到这一点,那么根本无法根据用户输入在C ++中声明一个二维数组。

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)
    {
      ...
    }
};

请注意,此代码要求您遵循the rule of three

在C语言中,你可以通过编写int array[m][n]来优雅地解决这个问题,但是你正在使用C ++,所以你不能这样做。

答案 1 :(得分:0)

您可以将函数包装到类中。在该类中,您可以将数组作为成员变量。

    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);
    }

答案 2 :(得分:0)

如果您声明类似int int A[m][n];的矩阵,其中mn不是const,则无法将其传递给函数。有两种方法可以解决它:

1)声明const大小的矩阵,如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);

的最大值

答案 3 :(得分:0)

以下不是标准C ++,因为它仅在编译器支持可变长度数组时才有效。 VLA是在C99中引入的,在C11中是可选的,但从未在C ++标准中引入 - 但有些编译器甚至在C ++模式下也支持它。

hack将矩阵地址存储为全局void *并将其转换为函数内部指向VLA的正确指针。这个hack是必需的,因为在全局声明的那一刻你无法知道矩阵的列数。

#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给出预期的结果