首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在While循环中打印出矩阵格式的二维数组

如何在While循环中打印出矩阵格式的二维数组
EN

Stack Overflow用户
提问于 2020-02-28 23:15:28
回答 1查看 49关注 0票数 0

我有一个很大的while循环,它将整数从文本文件读取到二维数组中,并计算数组的长度。这个循环运行得非常好。

代码语言:javascript
运行
复制
#include <iostream>
#include  <fstream>
#include <string>
#define MAX_ROWS  3
#define MAX_COLUMNS 2

using namespace std;

int main()
{
    string fileName = "inFilePgm2A.txt";                                                           
    ifstream inFile;                                                                             
    inFile.open(fileName);                                                                       

    int checkNbr;
    int ArrB[MAX_ROWS][MAX_COLUMNS];
    bool bad = false;
    bool invalidnum = false;

    while (!inFile.eof())
    {
        bad = false;
        for (int i = 0; (i < MAX_ROWS) && !bad; i++) {
            for (int j = 0; (j < MAX_COLUMNS) && !bad; j++) {
                inFile >> ArrB[i][j];
                if (ArrB[i][j] == -1) {
                    bad = true;
                    cout << "\nThe array does not have enough integers\n";
                }
                else {
                    if (ArrB[i][j] < 1) {
                        invalidnum = true;
                    }
                }
                if (!bad) {
                    cout << *(*(ArrB + i) + j) << " ";
                }
            }
        }

        if (bad == false) {
            inFile >> checkNbr;
            if (checkNbr == -1) {
                cout << "\nThe size of the array is correct." << endl;
            }
            else {

                while (checkNbr != -1)
                {
                    cout << checkNbr;
                    cout << " ";
                    inFile >> checkNbr;
                }

                cout << "\nYou have too many numbers in this array\n";
            }
        }

        if (invalidnum == true) {
            invalidnum = false;
            cout << "\nThere is/are negative number(s) or zero(s) in the array imported from your text file.\n";
        }
    }
    return 0;
}

例如,如果我的文本文件包含以下整数:

1 2 3 4 5 6 -1 1 2 3 7 5 8 -1 -5 9 4 -1

这将是结果:

问题是,当数组在while循环中时,我不知道如何打印矩阵格式的二维数组。

因此,我希望它显示为“1 2 3 4 5 6”

1 2

3 4

5 6

数组的大小是正确的…。

通常,我可以使用下面的代码打印矩阵格式的数组

代码语言:javascript
运行
复制
for(int i = 0; i < MAX_ROWS; i++)
{
    for(int j = 0; j < MAX_COLUMNS; j++)
    {
        cout<<ArrB[i][j]<<"\t";
    }
    cout<<endl;
}

但是这段代码在while循环中不起作用,如果我将上面的代码放在while循环中(在文本文件中使用完全相同的整数),它将只输出一堆随机值…

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-28 23:24:26

这是一个例子,我测试了以下打印矩阵格式的二维数组的代码。您可以使用printf("%5d"....)而不是cout。%nd,其中n是加上那么多空格的整数。

代码语言:javascript
运行
复制
while (!inFile.eof())
{
    bad = false;
    for (int i = 0; (i < MAX_ROWS) && !bad; i++) {
        for (int j = 0; (j < MAX_COLUMNS) && !bad; j++) {
            inFile >> ArrB[i][j];
            if (ArrB[i][j] == -1) {
                bad = true;
                cout << "\nThe array does not have enough integers\n";
            }
            else {
                if (ArrB[i][j] < 1) {
                    invalidnum = true;
                }
            }
            if (!bad) {
                printf("%3d",ArrB[i][j]);
                //cout << *(*(ArrB + i) + j) << " ";
            }
        }
        printf("\n");
    }

输出:

代码语言:javascript
运行
复制
  1  2
  3  4
  5  6

 The size of the array is correct.
  1  2
  3  7
  5  8

 The size of the array is correct.
 -5  9
  4
 The array does not have enough integers


 There is/are negative number(s) or zero(s) in the array imported from 
 your text file.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60454533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档