C++ 用c+填充对称矩阵+;

C++ 用c+填充对称矩阵+;,c++,matrix,multidimensional-array,C++,Matrix,Multidimensional Array,在判断多维数组的元素是否存在的条件方面遇到问题 例如,arr[row][col]正好是arr[col][row]然后自动将arr[col][row]的值分配给arr[row][col的相同值,而不允许用户手动输入它 这应该是输出的一个例子 example[4][4] = { // 0 1 2 3 { 0, 10, 15, 18}, // 0 { 10, 0, 20, 14}, // 1 { 15

在判断多维数组的元素是否存在的条件方面遇到问题
例如,arr[row][col]正好是arr[col][row]然后自动将arr[col][row]的值分配给arr[row][col的相同值,而不允许用户手动输入它

这应该是输出的一个例子

example[4][4] = 
     {
     //   0   1   2   3
        { 0, 10, 15, 18},   //   0
        { 10, 0, 20, 14},   //   1
        { 15, 20, 0, 90},   //   2
        { 18, 14,90, 0},    //   3
这是我的密码

`   int size;
    int arr[size][size];
    cout<<"Choose size of your multidimensional array [matrix]: ",cin>>size;
    cout<<"Now enter your data [Respectively] \n";
    for(int d=0 ; d<size ; d++)
    {
        for(int j=0; j<size; j++)
        {
            if (d==j) 
            {
                arr[d][j]=0 ;
            }
            else if(arr[d][j]!=0 ) //there should be the problem
            {
                arr[j][d]=arr[d][j];
            }
            else
            {
                cin>>arr[d][j]; // Filling matrix
            }
        }
    }  `
`int size;
int arr[尺寸][尺寸];
coutsize;

cout这里有一个最低限度的工作示例(MWE),可以解决您的问题:

#include <iostream>

using namespace std;

int main()
{
    cout << "Choose size of your multidimensional array [matrix]: ";
    int size;
    cin >> size;
    int arr[size][size];
    cout << "Now enter your data [Respectively] \n";
    for(int d=0; d<size; d++)
    {
        for(int j=d+1; j<size; j++)
        {
            if (d==j)
            {
                arr[d][j] = 0;
            }
            else if(j<d)      // here laid the problem
            {
                arr[d][j] = arr[j][d];
            }
            else
            {
                cin >> arr[d][j]; // Filling matrix
            }
        }
    }

    // print matrix
    for(int d=0; d<size; d++) {
        for(int j=0; j<size; j++)
            cout << arr[d][j] << " ";
        cout << '\n';
    }

return 0;
}
#包括
使用名称空间std;
int main()
{
大小;
int arr[尺寸][尺寸];

CUD< C++和C++ [AR] [j]=& [j] [d] <代码> ARR[D] [j]=ARR[j] [d] < /Cord>。为什么,你比较地址?也,<代码> int [Rosi[St](大小)< /C> >无效c++。<代码> int ARR [大小] [大小];<代码>这里代码>大小< /C> >未初始化注意:VLA:S(可变长度数组)不是标准C++的一部分。(使用未定义行为的代码)实际起作用“这是未定义行为的有趣之处。它有时甚至可能看起来起作用,这是所有行为中最隐蔽的。@Gradzidsa如果你想让程序可移植,不要使用
int-arr[size][size];
。相反,使用
std::vector-arr(size,std::vector(size));
(int j=0;j
for(int j=d;j>arr[d][j];
=>
cin>>arr[d][j];arr[j][d]=arr[d][j];
允许删除第二种情况。您的解决方案给了我一个很好的提示,并成功地做了一个小的编辑谢谢!!@ThomasSablik:谢谢您,非常聪明,我们甚至可以通过预先设置
arr[d][d]来完全摆脱条件句=0;
到第二个内部
循环。@Gradzidsa:很高兴能提供帮助!如果我的答案解决了您的问题,您应该将我的答案作为已接受的答案打勾,除此之外,绿色的“V”打勾,谢谢。
#include <iostream>

using namespace std;

int main()
{
    cout << "Choose size of your multidimensional array [matrix]: ";
    int size;
    cin >> size;
    int arr[size][size];
    cout << "Now enter your data [Respectively] \n";

    for(int d=0 ; d<size ; d++)
    {
        arr[d][d]=0;
        for(int j=d+1; j<size; j++)
        {
            cin >> arr[d][j];
            arr[j][d]=arr[d][j];
        }
    }

    // print matrix
    for(int d=0; d<size; d++) {
        for(int j=0; j<size; j++)
            cout << arr[d][j] << " ";
        cout << '\n';
    }

return 0;
}