二维数组输出一个连续的行

时间:2014-04-16 20:30:59

标签: c++ arrays

您好我是C ++的新手,我很难理解为什么这个二维数组只生成一行和多列。它会读取正确的信息,但不会使用正确的列和行输出。

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>

    using namespace std;

    char pat [9][9]= {'$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$',
              '$','$','$','$','$','$','$','$','$'}; // 9x9 matrix
    int main ()
    {
int pattern,
    dimensions;


do
{
    cout << "1) Display Pattern 1" << endl;                //menu selections
    cout << "2) Display Pattern 2" << endl;
    cout << "3) Display Pattern 3" << endl;
    cout << "4) Display Pattern 4" << endl;
    cout << "5) Exit Program" << endl << endl;
    cout << "Please select an option. ";
    cin >> pattern;

    if (pattern == 1)
    {
        system("cls");
        do
        {
            cout << "Note: Choose a number between 1 and 10." << endl; 
            cout << "Choose a number ";
            cin >> dimensions;
            cout << endl;


            if (dimensions > 1 && dimensions < 10)
                           /* based on the user's input for dimension
                           it will output a square i.e. 2x2, 3x3, 4x4 etc */ 

            {
                cout << "True!" << endl;
                for (int rows = 0; rows < dimensions; rows++)
                {
                  for (int cols = 0;  cols < dimensions; cols++)
                    cout << pat[cols][rows];
                }

            }
            else 
            {
            cout << "Error! Number is not between this set!" << endl;
            Sleep(3000);
            cout << endl;
            }
        }
        while (pattern == 1);
    }
    else if (pattern == 2)
    {
    system("cls");
        do
        {
            cout << "Note: Choose a number between 1 and 10." << endl;
            cout << "Choose a number ";
            cin >> dimensions;
            cout << endl;


            if (dimensions > 1 && dimensions < 10)
            {
                cout << "True!";
            }
            else 
            {
            cout << "Error! Number is not between this set!" << endl;
            Sleep(3000);
            cout << endl;
            }
        }
        while (pattern == 2);
    }
    else if (pattern == 3)
    {
     system("cls");
        do
        {
            cout << "Note: Choose a number between 1 and 10." << endl;
            cout << "Choose a number ";
            cin >> dimensions;
            cout << endl;


            if (dimensions > 1 && dimensions < 10)
            {
                cout << "True!";
            }
            else 
            {
            cout << "Error! Number is not between this set!" << endl;
            Sleep(3000);
            cout << endl;
            }
        }
        while (pattern == 3);   
    }
    else if (pattern == 4)
    {
     system("cls");
        do
        {
            cout << "Note: Choose a number between 1 and 10." << endl;
            cout << "Choose a number ";
            cin >> dimensions;
            cout << endl;


            if (dimensions > 1 && dimensions < 10)
            {
                cout << "True!";
            }
            else 
            {
            cout << "Error! Number is not between this set!" << endl;
            Sleep(3000);
            cout << endl;
            }
        }
        while (pattern == 4);   
    }
    else if (pattern == 5)
    { 
    return 0;
    }
    else 
    {
        cout << "Please input a valid entry." << endl << endl;
        Sleep(3000);
        cout << endl;
    }
}
while (pattern != 5);

}

2 个答案:

答案 0 :(得分:1)

如果要初始化多维数组,则必须这样做:

int multiarray[3][3] = 
{
{1,2,3},
{4,5,6},
{7,8,9}
};

使用嵌套大括号分隔尺寸。

在此之后按照您希望的方式输出它:

for(int i = 0; i < 3; i++){
  for(int j = 0; j < 3; j++){
    std::cout << multiarray[i][j];
  }
  std::cout << std::endl;
}

答案 1 :(得分:1)

当您打印阵列时:

for (int rows = 0; rows < dimensions; rows++)
{
    for (int cols = 0;  cols < dimensions; cols++)
        cout << pat[cols][rows]
}

您永远不会打印新行,因此它们都在一行上。你想要这样的东西:

for (int rows = 0; rows < dimensions; rows++)
{
    for (int cols = 0;  cols < dimensions; cols++)
        cout << pat[cols][rows]
    cout << endl;
}