我想让+和=符号出现在中间

时间:2019-05-03 14:09:31

标签: c++

我在寻找一种方法来确保+=符号位于中间或应该位于它们的位置时遇到问题;例如,如果包含3行,则在第二行;如果包含5行,则在第三行;如果包含2行,则在第二行。

int main()
{
    int Sizer = 0, Sizec = 0, first[10][10], second[10][10], sum[10][10];
    cout << "Enter the number of rows for array: ";

    // input
    cin >> Sizer;
    cout << "Enter the number of columns for an array: ";
    cin >> Sizec;
    cout << endl;
    cout << "Enter the elements of first Array";
    cout << endl;

    // nested loop execution and input
    for (int c = 0; c < Sizer; c++)
    {
        for (int d = 0; d < Sizec; d++)
        {
            cout << "Enter elements of array [" << c + 1 << "]" << "[" << d + 1 << "]: ";
            cin >> first[c][d];
        }
    }

    cout << endl;
    cout << "Enter the elements of second Array";
    cout << endl;

    // nested loop and execution
    for (int c = 0; c < Sizer; c++)
    {
        for (int d = 0; d < Sizec; d++)
        {
            cout << "Enter elements of array [" << c + 1 << "]" << "[" << d + 1 << "]: ";
            cin >> second[c][d];
        }
    }

    // outputting a sum
    cout << endl;
    cout << "Sum of Arrays: ";
    cout << endl;

    // loop execution and sum
    for (int c = 0; c < Sizer; c++)
    {
        cout << "[";

        for (int d = 0; d < Sizec; d++)
        {
            cout << first[c][d];

            if (d != Sizec - 1)
                cout << " ";
        }
        cout << "]";

        if (Sizer == Sizer / 2 + 0.5)
        {
            cout << "+";
        }
        else
        {
            cout << "   ";
        }

        cout << "[";
        for (int d = 0; d < Sizec; d++)
        {
            cout << second[c][d];
            if (d != Sizec - 1)
                cout << " ";
        }
        cout << "]   ";
        // output sum
        cout << "[";
        for (int d = 0; d < Sizec; d++)
        {
            sum[c][d] = first[c][d] + second[c][d];
            cout << sum[c][d];
            if (d != Sizec - 1)
                cout << " ";
        }
        cout << "]";
        cout << endl;
    }
}

我希望输出看起来像这样

[2 3 5] [2 5 0] [4 8 5]
[1 2 3]+[4 8 1]=[5 10 4]
[1 1 3] [4 1 1] [5 2 4]

1 个答案:

答案 0 :(得分:1)

问题是您对中间行的测试:

if (Sizer == Sizer / 2 + 0.5)

如果true大于Sizer,则永远不会是0,您想要的是这样的东西:

if (c == Sizer / 2)

编辑:因为c是行数,并且您正在测试它是否等于中间行(Sizer / 2)。