2

having a trouble with a condition which tells if an element of a multidimensional array
as example arr[row][col] is exactly arr[col][row] then automatically assign the value of arr[col][row] to the same value of arr[row][col] without letting user enter it manually

That should be an example of the output

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

here's my code

`   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
            }
        }
    }  `
  • 2
    `&arr[d][j]==&arr[j][d]` --> `arr[d][j]== arr[j][d]`. Why are you comparing the addresses? Also, `int arr[size][size]` is not valid c++. – cigien Aug 23 '20 at 16:57
  • 1
    `int arr[size][size];` here `size` is used uninitialized – sebastian Aug 23 '20 at 16:57
  • 1
    Note that VLA:s (variable-length arrays) aren't part of standard C++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo Aug 23 '20 at 17:02
  • well initializing my array that way >> int arr[size][size]; actually worked, it's just that i don't get my desired output –  Aug 23 '20 at 17:04
  • Yeah that's my problem , I actually want to assign that rr[j][d]=arr[d][j]; after checking if rr[d][j] is equal to something , that part should be erased && arr[d][j]==arr[j][d] –  Aug 23 '20 at 17:10
  • @Gradzidsa when you declare the matrix `arr` the value of `size` is completely random, when you do `cin>>size` the matrix is already allocated, updating the value of `size` does not affect `arr` – sebastian Aug 23 '20 at 17:11
  • 2
    _"well (code used undefined behavior) actually worked"_ That's the funny thing about **undefined behavior**. It can even sometimes appear to work, which is the most insidious of all. – Eljay Aug 23 '20 at 17:12
  • 2
    @Gradzidsa If you want the program to be portable, don't use `int arr[size][size];`. Instead, use `std::vector> arr(size, std::vector(size));` – Ted Lyngmo Aug 23 '20 at 17:13
  • well i'm removing && arr[d][j]==arr[j][d] because it doesn't make any sense but still there is the problem –  Aug 23 '20 at 17:16

1 Answers1

2

Here is a Minimum Working Example (MWE) that should solve your issues:

#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;
}

This below is even cleaner, gets rid of the conditional inside the inner loop, thank you @ThomasSablik

#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;
}
Giogre
  • 1,444
  • 7
  • 19
  • 2
    `for(int j=0; j `for(int j=d; j> arr[d][j];` => `cin >> arr[d][j]; arr[j][d] = arr[d][j];` allows removing the second case. – Thomas Sablik Aug 23 '20 at 17:25
  • Well your solution gave me a great hint and managed to do a small edit thanks !! –  Aug 23 '20 at 17:36
  • 1
    @ThomasSablik : thank you, very clever, we can even get rid of conditionals altogether by prepending `arr[d][d]=0;` to the second inner `for` loop. – Giogre Aug 23 '20 at 17:37
  • @Gradzidsa: glad to be of help! You should tick my answer as the accepted one if it has solved your issue, the green 'V' tick besides it, thank you. – Giogre Aug 23 '20 at 17:42