我想在C ++中显示2D数组的所有条目

时间:2019-01-30 14:54:36

标签: c++

我想创建一个程序,从用户那里获取2D数组的行和列的大小,然后从用户那里获取数组的所有条目。最后显示从array [0] [0]到array [size] [size]的所有条目。

我的代码是:

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string written in JSON format, into a JavaScript function.</h2>

<p id="demo"></p>

<script>
    function test(val){return val + " it's OK;}
    var someVar = "yup";
    var myObj = { "func": "test(someVar);" };
    document.getElementById("demo").innerHTML = eval(myObj.func);
</script>

</body>
</html>

输出为:

#include <iostream>
using namespace std;

int main()
{
    int rows, coloumns;
    int tom[rows][coloumns];
    cout << "Size of array rows: ";
    cin >> rows;
    cout << "Size of array coloumns: ";
    cin >> coloumns;
    for(int count1 = 0; count1 < rows; count1++)
    {
        for(int count2 = 0; count2 < coloumns; count2++)
        {
            cout << "Enter entry of row " << count1 << " and coloumn " << count2 << ": ";
            cin >> tom[count1][count2];
        }
    }
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < coloumns; j++)
        {
            cout << tom[i][j] << endl;
        }
    }
    return 0;
}

它应该提供输出:

Size of array rows: 2
Size of array coloumns: 3
Enter entry of row 0 and coloumn 0: 1
Enter entry of row 0 and coloumn 1: 2
Enter entry of row 0 and coloumn 2: 3
Enter entry of row 1 and coloumn 0: 12
Enter entry of row 1 and coloumn 1: 13
Enter entry of row 1 and coloumn 2: 14
12
13
14
12
13
14

出什么问题了? 请帮忙。

3 个答案:

答案 0 :(得分:2)

您不能动态创建这样的数组。这甚至不应该编译。即使这样做,您仍在创建数组 before ,让用户输入尺寸。对于正确的方法,请使用std::vector

#include <iostream>
#include <vector>

int main()
{
    int rows, coloumns;
    std::cout << "Size of array rows: ";
    std::cin >> rows;
    std::cout << "Size of array coloumns: ";
    std::cin >> coloumns;
    std::vector<std::vector<int>> tom(rows, std::vector<int>(coloumns));

    for (int count1 = 0; count1 < rows; count1++)
    {
        for (int count2 = 0; count2 < coloumns; count2++)
        {
            std::cout << "Enter entry of row " << count1 << " and coloumn " << count2 << ": ";
            std::cin >> tom[count1][count2];
        }
    }
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < coloumns; j++)
        {
            std::cout << tom[i][j] << std::endl;
        }
    }
    return 0;
}

输出:

Size of array rows: 2
Size of array coloumns: 3
Enter entry of row 0 and coloumn 0: 1
Enter entry of row 0 and coloumn 1: 2
Enter entry of row 0 and coloumn 2: 3
Enter entry of row 1 and coloumn 0: 12
Enter entry of row 1 and coloumn 1: 13
Enter entry of row 1 and coloumn 2: 14
1
2
3
12
13
14

请不要使用using namespace std;-请阅读here原因。

答案 1 :(得分:0)

您不能声明这样的数组:

    int tom[rows][coloumns];

因为行和列的值尚不清楚。 相反,您应该在询问值后动态初始化此数组。 这是您的固定代码:

#include <iostream>
using namespace std;

int main()
{
    int rows, coloumns;
    int **tom;
    cout << "Size of array rows: ";
    cin >> rows;
    cout << "Size of array coloumns: ";
    cin >> coloumns;

    tom = new int*[rows];
    for (int i = 0; i < rows; i++) {
      tom[i] = new int[coloumns];
    }

    for(int count1 = 0; count1 < rows; count1++)
    {
        for(int count2 = 0; count2 < coloumns; count2++)
        {
            cout << "Enter entry of row " << count1 << " and coloumn " << count2 << ": ";
            cin >> tom[count1][count2];
        }
    }
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < coloumns; j++)
        {
            cout << tom[i][j] << endl;
        }
    }
    return 0;
}

答案 2 :(得分:0)

您在实际获得行数/列数之前初始化了tom

#include <iostream>

int main()
{
    int rows, columns;
    std::cout << "Rows: ";
    std::cin >> rows;
    std::cout << "Columns: ";
    std::cin >> columns;
    int arr[rows][columns];

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            std::cout << "Enter the value for [" << i << "][" << j << "] : ";
            std::cin >> arr[i][j];
        }
    }

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}