无法弄清楚为什么只从文件中读取一行矩阵以及为什么我不能将2D数组传递给函数

问题描述 投票:1回答:3

我现在已经在这个问题上挣扎了大约一个小时,所以我转向全能的互联网实体。

我正在尝试编写一个程序,它将以下列格式读取txt文件中的矩阵,其中第一个数字是列(4),第二个数字是矩阵中的行(3)。并且每行数字对应于矩阵中的一行。

4 3
1 2 3 4
0 1 2 7
4 1 9 2

和B)计算矩阵中的1的数量。所以上面的例子将返回3.我的代码如下。

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void count_ones(int matrix[][], int rows, int columns)
{

int count = 0;

for(int i = 0; i < rows; i++)
{
        for( int j = 0; j < columns; j++)
        {
                if( matrix[i][j] == 1)
                { count++;}
        }
}

cout << "There are " << count << " ones in this matrix.";
}

int main(int argc, char* argv[])
{

int rows, columns;
string file_name = argv[1];

ifstream reader("m1.txt");

reader >> columns;
reader >> rows;

int matrix[rows][columns];

for(int i = 0; i < rows; i++)
{
        for(int j = 0; j < columns; j++)
        {
          reader >>  matrix[i][j];
        }
}


cout << columns << " " << rows;
cout << endl;
for( int k = 0; k < rows; k++) {
      for( int l = 0; l < columns; l++)
         cout << matrix[k][l] << " ";
      cout << endl;

reader.close();

count_ones(matrix, rows,columns);
return 0;
}
}

现在我有两个问题。我用来打印我从“m1.txt”文件中读取的矩阵的代码只打印前两行,我完全不知道是什么导致这个,但我猜它有事情要做用我的ifstream阅读器。

4 3
1 2 3 4

其次,当我尝试将矩阵传递给count_ones函数时,我遇到了一些我不理解的错误。我对C ++不太满意,所以我很感激能得到的所有帮助。

c++ matrix ifstream cout
3个回答
3
投票

在评论中,你问

有没有人有更好的方法将矩阵传递给count_ones方法?

  1. 不要用 int matrix[rows][columns]; 这不是标准的C ++。一些编译器支持它作为扩展。
  2. 使用 std::vector<std::vector<int>> matrix; 您可以使用正确的行和列大小对其进行初始化 std::vector<std::vector<int>> matrix(rows, std::vector<int>(columns));
  3. 更改count_ones的声明以接受std::vector<std::vector<in>>int count_ones(std::vector<std::vector<in>> const& matrix); 相应地更新其实施。

Suggestion for improvement

您可以通过使用辅助函数将矩阵写入}来避免将关闭的cout放在错误的位置的错误。

std::ostream& operator<<(std::ostream& out, std::vector<int> const& row)
{
   for ( int item : row )
      out << item << " ";
   return out;
}

std::ostream& operator<<(std::ostream& out, std::vector<std::vector<int>> const& matrix)
{
   for ( auto const& row : matrix )
      out << row << std::endl;
   return out;
}

然后使用

std::cout << matrix;

main


3
投票

你在最后一个for循环中犯了一个错误。

for( int k = 0; k < rows; k++) {
      for( int l = 0; l < columns; l++)
         cout << matrix[k][l] << " ";
      cout << endl;

reader.close();

count_ones(matrix, rows,columns);
return 0;
}
}

它应该是这样的

for( int k = 0; k < rows; k++) {
      for( int l = 0; l < columns; l++)
         cout << matrix[k][l] << " ";
      cout << endl;
}

reader.close();

count_ones(matrix, rows,columns);
return 0;
}

因此,代码中的外部for循环只运行一次并仅打印第一行矩阵。

编辑:还有一些要纠正的事情。你不能使用matrix [] []作为函数参数,它将通过错误多维数组必须具有除第一个之外的所有维度的边界

您可以使用双指针进行此工作。将check one函数声明更改为此

void count_ones(int **matrix, int rows, int columns)

更换

int matrix[rows][columns];

int **matrix = (int **)malloc(sizeof(int *)*columns);
for(int i=0; i < columns; i++)
    *(matrix + i) = (int *)malloc(sizeof(int)*rows);

并且代码应该像魅力一样工作。并且还删除了这一行,它的冗余,因为没有使用file_name。

string file_name = argv[1];

0
投票

所以,我不知道错误是什么,直到你发布它们,但我知道为什么你的输出过早被切断。

所以,回顾一下,让我们再看看你的代码(相关部分“:

cout << columns << " " << rows;
cout << endl;
for( int k = 0; k < rows; k++) {
    for( int l = 0; l < columns; l++) /* { */
        cout << matrix[k][l] << " ";
    /* } */
    cout << endl;

    reader.close();

    count_ones(matrix, rows,columns);
    return 0;
}

我缩进了它,因此它更容易阅读以及添加注释括号,只是因为更清楚什么是由什么执行。

而现在,输出:

4 3
1 2 3 4

好的,现在让我们分解正在发生的事情。

cout << columns << " " << rows;
cout << endl;

这是创建线:

4 3

到目前为止一切都那么好吧?

现在,我们进入lop:

for( int k = 0; k < rows; k++) {
    for( int l = 0; l < columns; l++) /* { */
        cout << matrix[k][l] << " ";
    /* } */
    cout << endl;

得到这个:

1 2 3 4

这必须是矩阵的第一行。

更多代码执行:

reader.close();

count_ones(matrix, rows,columns);

这与您的问题无关。

现在这个:

    return 0;
}

哎呦!我们刚刚通过调用return离开了函数。

循环将不再执行,因为我们通过返回过早地终止它,只输出矩阵的第一行。

解决方案:只需将return语句移动到循环外部,如下所示:

cout << columns << " " << rows;
cout << endl;
for( int k = 0; k < rows; k++) {
    for( int l = 0; l < columns; l++) /* { */
        cout << matrix[k][l] << " ";
    /* } */
    cout << endl;

    reader.close();

    count_ones(matrix, rows,columns);
}
return 0;

这应该可以解决问题。

最后,一些友好的建议,采取萨米Kuhmonen的建议并缩进你的代码。它使阅读和捕捉这样的事情变得更容易。

编辑:还有一点,如R.k. Lohana提到,你可能也希望将这些线路拉出循环:

    reader.close();

    count_ones(matrix, rows,columns);

像这样:

for( int k = 0; k < rows; k++) {
    for( int l = 0; l < columns; l++) /* { */
        cout << matrix[k][l] << " ";
    /* } */
    cout << endl;

}

reader.close();

count_ones(matrix, rows,columns);

return 0;

因为你可能只想做一次而不是多次。

© www.soinside.com 2019 - 2024. All rights reserved.