How to Print Out 2-D Array in Matrix Format in a While-Loop

我与影子孤独终老i 提交于 2021-01-29 10:23:51

问题


I have a big while loop that read integers from a text file into 2-D array(s) and assesses array(s’) length. This loop works perfectly fine.

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

For example, if my text file contains the following integers:

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

This will be the result:

The problem is, I don’t know how to print a 2-D array in matrix format when the array is in a while loop.

So, instead “1 2 3 4 5 6”, I want it to display

1 2

3 4

5 6

The size of the array is correct….

Usually, I can use the code below to print out an array in matrix format

for(int i = 0; i < MAX_ROWS; i++)
{
    for(int j = 0; j < MAX_COLUMNS; j++)
    {
        cout<<ArrB[i][j]<<"\t";
    }
    cout<<endl;
}

But this code is not working in the while loop, if I put the code above in my while loop, (with the exactly same integers in the text file), it will just output a bunch of random values…


回答1:


This is an example, I tested the below code for printing a 2D array in matrix format. You can use printf("%5d"....) instead of cout. %[n]d, where n is the integer adding that many spaces.

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

Output:

  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.


来源:https://stackoverflow.com/questions/60454533/how-to-print-out-2-d-array-in-matrix-format-in-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!