从文件中获取矩阵并将其分配给具有最大大小的数组

发布于 2024-09-30 09:00:43 字数 1087 浏览 2 评论 0原文

好的,所以我有一个数组 a[maxsize] [maxsize] 其中 maxsize=10 当从文件中获取数组时,如何使数组正常工作小于最大尺寸。

示例矩阵:

1 2 3
4 5 6
7 8 9

即将推出:

1 2 3 4 5 6 7 8 9

我的代码: `int main() { 常量 int 最大大小 = 10; 双原始矩阵[最大尺寸][最大尺寸],转置矩阵[最大尺寸][最大尺寸];

ifstream fin;
ofstream fout;

fin.open ("input.txt");

if (fin.fail())
{
    cout << "Input file opening failed. \n";
    return 0;
}

for(i=0; i<maxsize; i++)
{
    for(j=0; j<maxsize; j++)
    {
        fin >> original_matrix [i][j];
    }
}

transpose(original_matrix[][maxsize], transposed_matrix[][maxsize],maxsize)


return 0;
}

我希望数组像这样分配它

M [0] [0]=1 M [0] [1]=2 M [0] [2]=3
M [1] [0]=4 M [1] [1]=5 M [1] [2]=6
M [2] [0]=7 M [2] [1]=8 M [2] [2]=9

M [0] [0]=1 M [0] [1]=1 M [0] [2]=1 M [0] [3]=1 M [0] [4]=1 M [0] [5]=1 M [0] [6]=1 M [0] [7]=1 M [0] [8]=1

......所以

文本文件看起来像这样:

2
1 0
0 1
3
8 9 1
3 5 2
-2 3 -1
0

Okay, so i have an array a[maxsize] [maxsize] where maxsize=10 how do i get an array to work correctly when taking an array from a file that is smaller than the max size.

Example matrix:

1 2 3
4 5 6
7 8 9

Is coming out:

1 2 3 4 5 6 7 8 9

My code:
`int main()
{
const int maxsize = 10;
double original_matrix[maxsize][maxsize], transposed_matrix[maxsize][maxsize];

ifstream fin;
ofstream fout;

fin.open ("input.txt");

if (fin.fail())
{
    cout << "Input file opening failed. \n";
    return 0;
}

for(i=0; i<maxsize; i++)
{
    for(j=0; j<maxsize; j++)
    {
        fin >> original_matrix [i][j];
    }
}

transpose(original_matrix[][maxsize], transposed_matrix[][maxsize],maxsize)


return 0;
}

I want the array to assign it like this

M [0] [0]=1 M [0] [1]=2 M [0] [2]=3
M [1] [0]=4 M [1] [1]=5 M [1] [2]=6
M [2] [0]=7 M [2] [1]=8 M [2] [2]=9

Not

M [0] [0]=1 M [0] [1]=1 M [0] [2]=1 M [0] [3]=1 M [0] [4]=1 M [0] [5]=1 M [0] [6]=1 M [0] [7]=1 M [0] [8]=1

..... So on

The text file looks like this:

2
1 0
0 1
3
8 9 1
3 5 2
-2 3 -1
0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-10-07 09:00:43

如果您希望矩阵以矩阵形式出现,为什么不呢:

    for(i=0; i<maxsize; i++)
{
    for(j=0; j<maxsize; j++)
    {
        fin >> original_matrix [i][j];
    }
    cout << "\n"
}

这样它就会以一个盒子而不是一条线的形式出现。我认为你还必须使用 maxsize,因为你希望它是一个没有空的矩阵,所以你应该检查有多少向量组成矩阵,并将其用作条件(而不是 maxsize)

编辑 - 输入文件伪代码

因此第一行是一个数字,它告诉使用矩阵将有多少行或列。因此,该程序将假设它是方阵。您想要的条件值是第一行(即2,3,0)。确保检查该值是否大于 0 且小于 10。解析每个值的输入行,将其注入到矩阵中。设置条件值,因此你将得到一个完美的方阵。

  1. 打开文件
  2. 读取行,是矩阵的长度吗?
    * 如果是,则设置为条件
    * 如果不是,则为矩阵中的值
  3. 检查条件以确保 0 <条件< 10
  4. 打印。

If you want the matrix to come out in matrix form, why dont you:

    for(i=0; i<maxsize; i++)
{
    for(j=0; j<maxsize; j++)
    {
        fin >> original_matrix [i][j];
    }
    cout << "\n"
}

That way it comes out as a box instead of a single line. I think you will also have to play around with maxsize, because you want it to be a matrix with no empties, so you should check how many vectors make up the matrix, and use that as a condition (instead of maxsize)

edit - Input file psuedo code

So the first line is a single number, that tells use how many rows or columns the matrix will be. This program will assume that it is a square matrix, therefore. the condition value that you want is the first line (ie 2,3,0). Make sure to check that the value is greater than 0 and less than 10. Parse through the input line for each value, inject it into your matrix. Set the condition value, and therefore you will have a perfect square matrix.

  1. Open File
  2. Read line, is it the length of matrix?
    * if so, set to condition
    * if not, it is a value in the matrix
  3. Check conition to make sure 0 < cond < 10
  4. Print.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文