C++ 最终输出应该在[]和+;=应该在中间
应详细说明总和部分的输出。但我不知道该怎么做C++ 最终输出应该在[]和+;=应该在中间,c++,C++,应详细说明总和部分的输出。但我不知道该怎么做 int main() { cout << "This program is to find the sum of two Different arrays"; cout << endl; // variable declaration int Sizer = 0, Sizec = 0, first[10][10], second[10][10], sum[10][10]; co
int main()
{
cout << "This program is to find the sum of two Different arrays";
cout << endl;
// variable declaration
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)
{
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;
}
}
我刚刚纠正了它。但我不知道如何用上面的形式来表示它
你知道你能帮我吗
我添加了一些行。这是我的新代码,我修复了一些部分,只在添加+和=符号时出现问题,我知道它必须处理行数或行大小的问题,您可以调整输出顺序并使用来组织输出
cout
的工作原理。在您的示例中,第一个输出应该是 [2 3 5]
[1 2 3]
[1 4 2]
通过这种方式,输出顺序应该是
1: [a[0][0] a[0][1] a[0][2]] [b[0][0] b[0][1] b[0][2]] [c[0][0] c[0][1] c[0][2]]
为了清楚起见,输出代码应该是
for (int i = 0; i < Sizer; i++)
{
// output first matrix
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << first[i][j] << " ";
cout << "]\t";
// output the second
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << second[i][j] << " ";
cout << "]\t";
// output sum
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << sum[i][j] << " ";
cout << "]\n";
}
您可能需要进行计算,以决定何时执行cout
操作+
和=
10
比数字<10占据更多的空间,而且您也可能得到这样的输出cout
数字之前添加一个setw(4)
cout << setw(4) << first[i][j];
这可能对您有用。预期的结果是在输出的末尾。我对所有的错误表示歉意。首先修复您的测试代码中的错误,并生成测试代码,这意味着如果有副本,它将编译。。。。第二:你到底有什么问题?您可以输出一个矩阵,那么输出其中三个矩阵的问题出在哪里呢?不要试图一个接一个地打印矩阵(这是可能的,但要困难得多),而是打印整行。@user6556709我确实修复了代码。对这一切感到抱歉。谢谢你的回复。但是它没有给我所需的输出,但是他们没有教我setw,所以我不能使用它。对于输出[2 3 5],2是第一个数组的第一个元素。因此,当使用循环时,我对如何使它们出现在同一行感到困惑。我尝试了这个方法,但得到了以下输出:数组的总和:[2][2][4][3][6][4][4][8][5][10][6][6][12][7][7][14][8][16][9][9][18][0][0]这是(c=0;c
for (int i = 0; i < Sizer; i++)
{
// output first matrix
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << first[i][j] << " ";
cout << "]\t";
// output the second
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << second[i][j] << " ";
cout << "]\t";
// output sum
cout << "[";
for (int j = 0; j < Sizec; j++)
cout << sum[i][j] << " ";
cout << "]\n";
}
[1 2 3 ] [1 1 1 ] [2 3 4 ]
[4 5 6 ] [1 1 1 ] [5 6 7 ]
[7 8 9 ] [1 1 1 ] [8 9 10 ]
[1 2 3 ] [1 1 1 ] [2 3 4 ]
[4 50 6 ] + [1 1 1 ] = [5 6 7 ]
[7 8000 9 ] [1 1 1 ] [8 9 10 ]
cout << setw(4) << first[i][j];
[ 1 2 232] [ 1002301 21] [ 1012303 253]
[ 12 1 1] [ 2 343 1] [ 14 344 2]
[ 1 1 1] [ 2 3 1] [ 3 4 2]