【问题标题】:Having the parameters of a matrix be read in?读取矩阵的参数?
【发布时间】:2019-11-07 05:38:40
【问题描述】:

所以我的部分作业是编写一个读取二维数组参数的程序。

这是我的代码:

#include <iostream>
#define ROWCAP 100
#define COLCAP 100
using namespace std;
void readValues(int x, int y, double matrix[x][y]);
void printValues(int x, int y, double matrix[x][y]);
int main() {
    int row;
    int cols;

    cout<<"Enter the number of rows: ";
    cin>>row;
    while(row>ROWCAP){
        cout<<"Number is too large, try again: ";
        cin>>row;
    }
    cout<<"Enter the number of columns: ";
    cin>>cols;
    while(cols>COLCAP){
        cout<<"Number is too large, try again: ";
        cin>>cols;
    }
    double matrix[row][cols];
    cout<<"Enter the matrix: \n";
    readValues(row,cols,matrix[row][cols]);
    cout<<"\nThe matrix entered was:\n";
    printValues(row,cols,matrix[row][cols])
    return 0;
}

void readValues(int x, int y, double matrix[x][y])
{
    for(int i=0; i<x; i++)
    {
        for(int j=0; j<y; j++)
        {
            cin>>matrix[i][j];
        }
    }
}

void printValues(int x, int y, double matrix[x][y])
{
    for(int i=0; i<x; i++)
    {
        for(int j=0; j<y; j++)
        {
            cout<<matrix[i][j]<<"\t";
        }
        cout<<endl;
    }
}

这些是错误:

error: 'matrix' declared as array of references of type 'double &'
void readValues(int x, int y, double &matrix[x][y]);

我已经坚持了三个小时。我做错了什么?
附言只能使用iostream

【问题讨论】:

  • define -> #definevoid readValues(int x, int y, double &amp;matrix[x][y]); -> void readValues(int x, int y, double matrix[x][y]);(你在 main() 中声明数组,只是传递它,而不是指向它的指针)
  • double matrix[row][cols]; -- C++ 中没有 VLA(可变长度数组)(非标准编译器扩展除外)。为什么不使用向量的向量?如果你必须使用普通的double,那么你需要动态分配或声明double matrix[ROWCAP][COLCAP];,并且只对row, col定义的空间进行操作(这有其缺点,但可行)
  • 不要编辑你的问题来合并你在 cmets 中得到的答案,然后切换到另一个问题,这被认为是一个移动目标问题(如果你已经得到官方答案,真的不感激..)。如果您至少也调整了所有问题,那么它仍然适合新问题。 IE。发布您现在收到的正确错误消息,而不是旧的错误消息。请再次edit您的问题以解决该问题,而不是在对部分旧问题的评论中解释新问题。
  • 那么您是否了解了可变长度数组及其特殊的非便携方面?您是否已经了解了所用编译器的假设,这意味着什么?如果不是,但您已经了解了所有编译器都知道要创建但需要静态大小定义的普通数组,那么请使用它们。这是相当困难的。

标签: c++ matrix parameters


【解决方案1】:

您可以使用二维数组名称作为实参或形参。在被调用函数中定义形状参数组时,可以参考

要确定所有维度的大小,也可以省略第一个维度的大小描述,如: void Func(int array[3][10]); void Func(int array[][10]); 两者都是合法且等效的,但不能省略第二个或更高维度的大小。

#include <iostream>
#define ROWCAP 100
#define COLCAP 100

using namespace std;
double matrix[ROWCAP][COLCAP];
void readValues(int x, int y, double matrix[][100]);
void printValues(int x, int y, double matrix[][100]);

int main() {

    int row;

    int cols;

    cout << "Enter the number of rows: ";
    cin >> row;
    while (row > ROWCAP) {
        cout << "Number is too large, try again: ";
        cin >> row;
    }
    cout << "Enter the number of columns: ";
    cin >> cols;
    while (cols > COLCAP) {
        cout << "Number is too large, try again: ";
        cin >> cols;
    }

    cout << "Enter the matrix: \n";
    readValues(row, cols, matrix);
    cout << "\nThe matrix entered was:\n";
    printValues(row, cols, matrix);
    return 0;
}

void readValues(int x, int y, double matrix[][100])
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            cin >> matrix[i][j];
        }
    }
}

void printValues(int x, int y, double matrix[][100])
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }
}

顺便说一句: (1)参数是二维数组,但是指定了第二维的维数。

int array[10][10];

函数声明:void fuc(int a[][10]);

函数调用:fuc(array);

  • 在函数 fuc 中,a 是一个二维数组。使用a[i][j] 表单访问数组中的元素。

(2)参数使用一维指针数组。

Int *array[10];

For(i = 0; i < 10; i++)

Array[i] = new int[10];

函数声明:void fuc(int *a[10]);

函数调用:fuc(array);

  • 在函数 fuc 中,a 是一维指针数组。使用*(a[i] + j) 表单访问数组中的元素。

(3)参数的指针使用指针。

Int **array;

Array = new int *[10];

For(i = 0; i <10; i++)

Array[i] = new int[10];

函数声明:void fuc(int **a);

函数调用:fuc(array);

  • 在函数 fuc 中,a 是指向指针的指针。使用*(int *)(a + i*d2 + j) 表单访问数组中的元素。

【讨论】:

    【解决方案2】:

    如果您的任务是使用数组,仅此而已,则必须使用整数字面量声明数组,设置每个维度的元素数,例如

    int main() {
    
        double matrix[ROWCAP][COLCAP];
    

    初始化您的变量以使您的初始比较适合您:

        int row = 101;
        int col = 101;
    
        while (row > ROWCAP) {
            cout << "\nEnter the number of rows: ";
            if (!(cin >> row)) {
                cerr << "error: invalid integer input - row\n";
                return 1;
            }
            if (row <= ROWCAP)
                break;
            cerr << "error: exceeds " << ROWCAP << '\n';
        }
    

    colCOLCAP 相同,注意:您可以简单地不断循环,直到获得所需的输入,例如 while (1) {...}

    要将二维数组传递给函数,您有两种选择:

    void readValues(int x, int y, double matrix[][COLCAP]);
    

    void readValues(int x, int y, double (*matrix)[COLCAP]);
    

    相应的函数定义将是:

    void readValues (int x, int y, double matrix[][COLCAP])
    {
        for (int i=0; i<x; i++)
        {
            for (int j=0; j<y; j++)
            {
                if (!(cin >> matrix[i][j])) {
                    cerr << "error: invalid integer input - matrix[i][j]\n";
                    exit (EXIT_FAILURE);
                }
            }
        }
    }
    

    或与

    void readValues (int x, int y, double (*matrix)[COLCAP])
    

    注意:您必须验证每个用户输入

    当您调用传递二维数组的函数时,您只传递数组的名称:

        cout << "Enter the matrix:\n";
        readValues (row, col, matrix);
    

    总而言之,你可以这样做:

    #include <iostream>
    
    #define ROWCAP 100
    #define COLCAP 100
    
    using namespace std;
    
    void readValues(int x, int y, double matrix[][COLCAP]);
    void printValues(int x, int y, double matrix[][COLCAP]);
    
    int main() {
    
        double matrix[ROWCAP][COLCAP];
        int row = 101;
        int col = 101;
    
        while (row > ROWCAP) {
            cout << "\nEnter the number of rows: ";
            if (!(cin >> row)) {
                cerr << "error: invalid integer input - row\n";
                return 1;
            }
            if (row <= ROWCAP)
                break;
            cerr << "error: exceeds " << ROWCAP << '\n';
        }
    
        while (col > COLCAP) {
            cout << "\nEnter the number of cols: ";
            if (!(cin >> col)) {
                cerr << "error: invalid integer input - col\n";
                return 1;
            }
            if (col <= COLCAP)
                break;
            cerr << "error: exceeds " << COLCAP << '\n';
        }
    
        cout << "Enter the matrix:\n";
        readValues (row, col, matrix);
        cout<<"\nThe matrix entered was:\n";
        printValues (row, col, matrix);
        return 0;
    }
    
    void readValues (int x, int y, double matrix[][COLCAP])
    {
        for (int i=0; i<x; i++)
        {
            for (int j=0; j<y; j++)
            {
                if (!(cin >> matrix[i][j])) {
                    cerr << "error: invalid integer input - matrix[i][j]\n";
                    exit (EXIT_FAILURE);
                }
            }
        }
    }
    
    void printValues (int x, int y, double matrix[][COLCAP])
    {
        for (int i=0; i<x; i++)
        {
            for(int j=0; j<y; j++)
            {
                cout << matrix[i][j] << "\t";
            }
            cout << endl;
        }
    }
    

    使用/输出示例

    $ ./bin/matrix_cin
    
    Enter the number of rows: 3
    
    Enter the number of cols: 3
    Enter the matrix:
    1 2 3 4 5 6 7 8 9
    
    The matrix entered was:
    1       2       3
    4       5       6
    7       8       9
    

    查看一下,如果您还有其他问题,请告诉我。

    【讨论】:

      【解决方案3】:

      查看问题 这种方法的主要问题是 VLA(可变大小数组)。 阅读本文

      维度必须在编译时已知,这意味着维度必须是常量表达式。 但是在您的情况下,您正在使用 x 和 y 创建一个数组,其值将在运行时已知。你很幸运,你的编译器允许你这样做。但请记住,C++ 不支持这些 VLA。

      你能做的就是——

      double matrix[100][100]; // declare it globally. Btw it's your choice it can be inside function. Notice we have provided the size at compiler time(100).
      void readValues(int x, int y, double matrix[][100]); 
      void printValues(int x, int y, double matrix[][100]);
      int main() {
          int row;
          int cols;
      
          cout<<"Enter the number of rows: ";
          cin>>row;
          while(row>ROWCAP){
              cout<<"Number is too large, try again: ";
              cin>>row;
          }
          cout<<"Enter the number of columns: ";
          cin>>cols;
          while(cols>COLCAP){
              cout<<"Number is too large, try again: ";
              cin>>cols;
          }
      
          cout<<"Enter the matrix: \n";
          readValues(row,cols,matrix); // Just pass it like normal argument.
          cout<<"\nThe matrix entered was:\n";
          printValues(row,cols,matrix);
          return 0;
      }
      
      void readValues(int x, int y, double matrix[][100]) //
      {
          for(int i=0; i<x; i++)
          {
              for(int j=0; j<y; j++)
              {
                  cin>>matrix[i][j];
              }
          }
      }
      
      void printValues(int x, int y, double matrix[][100])
      {
          for(int i=0; i<x; i++)
          {
              for(int j=0; j<y; j++)
              {
                  cout<<matrix[i][j]<<"\t";
              }
              cout<<endl;
          }
      }
      

      输出

      Enter the number of rows: 3
      Enter the number of columns: 3
      Enter the matrix:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      The matrix entered was:
      1       2       3
      4       5       6
      7       8       9
      
      Process returned 0 (0x0)   execution time : 7.435 s
      Press any key to continue.
      

      编辑 - 我注意到您的老师要求使用 const 全局行和列。所以你必须这样做。

      
      const int row = 100;
      const int column = 100;
      double matrix[row][column]; // Now you can do this as row and column are const. before it was error.
      void readValues(int x, int y, double matrix[][100]);
      void printValues(int x, int y, double matrix[][100]);
      int main() {
          int roww, colss;
          cout<<"Enter the number of rows: ";
          cin>>roww;
          while(roww>row){
              cout<<"Number is too large, try again: ";
              cin>>roww;
          }
          cout<<"Enter the number of columns: ";
          cin>>colss;
          while(colss>column){
              cout<<"Number is too large, try again: ";
              cin>>colss;
          }
          double matrix[100][100];
          cout<<"Enter the matrix: \n";
          readValues(roww,colss,matrix);
          cout<<"\nThe matrix entered was:\n";
          printValues(roww,colss,matrix);
          return 0;
      }
      
      void readValues(int x, int y, double matrix[][100])
      {
          for(int i=0; i<x; i++)
          {
              for(int j=0; j<y; j++)
              {
                  cin>>matrix[i][j];
              }
          }
      }
      
      void printValues(int x, int y, double matrix[][100])
      {
          for(int i=0; i<x; i++)
          {
              for(int j=0; j<y; j++)
              {
                  cout<<matrix[i][j]<<"\t";
              }
              cout<<endl;
          }
      }
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 2022-12-19
        • 1970-01-01
        相关资源
        最近更新 更多