-1

Sample Input

3
11 2 4
4 5 6
10 8 -12
Sample Output

15
Explanation

The primary diagonal is:

11
5
-12
Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is:

4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int i, j;
    int arr[i][j];
    int x1 = 0, x2 = 0;
    for (i = 1; i <= n; ++i)
    {
        for (j = 1; j <= n; ++j)
        {
            cin >> arr[i][j];
        }
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i == j)
                x1 = x1 + arr[i][j];
        }
    }
    for (i = 1; i <= n; i++)
    {
        for (j = n; j <= 1; j--)
        {
            x2 = x2 + arr[i][j];
        }
    }
    cout << abs(x1 - x2);
}
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • 2
    `int arr[i][j];` is nonstandard. And for the compilers that support it, neither `i` nor `j` has been initialized. – 1201ProgramAlarm Mar 18 '20 at 18:36
  • 3
    `int arr[i][j]` makes no sense. You need to dynamically allocate memory, or use some predecided upper bound. Also, arrays are zero-indexed, not one-indexed. – GoodDeeds Mar 18 '20 at 18:37
  • 1
    Forget about summing for now. Make sure you can input the data correctly. If you can read in all of the user's inputs and print them out without errors you'll be in much better position to tackle the rest of the program, but if you don't have that, the rest of the code is noise helping hide bugs. – user4581301 Mar 18 '20 at 18:46
  • Include what you use. In this instance, you're missing ``. This is just another issue on top of what's already been mentioned. – sweenish Mar 18 '20 at 18:48
  • Invest some time in learning to use the debugging utility that came with your development environment. A debugger allows you to control the execution of the program, stopping, starting, and advancing in increments you find useful. You can also see the values of the variables. The typical use is to advance the program line by line, monitoring what the program does, with an eye out for the unexpected. The unexpected is almost always a bug. – user4581301 Mar 18 '20 at 18:49

1 Answers1

2

Here is your corrected code:

1) First, you've declared a 2D array of garbage size.

2) Second, array indexing starts from 0 in most of the programming languages except MATLAB.

3) Always use pre-increment until and unless post-increment isn't absolutely necessary.

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int i, j;
    int arr[n][n];
    int x1 = 0, x2 = 0;
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            cin >> arr[i][j];
        }
    }
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if (i == j)
                x1 += arr[i][j];
        }
    }
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if(i + j == n - 1)
                x2 += arr[i][j];
        }
    }
    cout << abs(x1 - x2);
    return 0;
}

PS: There is a lot of scope of improvement in your code formatting.

Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
  • You should change `int arr[n][m];` to `std::vector> arr;` and use the `push_back` when reading values. The `int arr[n][m]` declares a Variable Length Array which is not standard C++. You could also use `operator new` instead. – Thomas Matthews Mar 18 '20 at 20:58
  • Yeah, that's what I intended to do, but, then I wanted to correct the given code only. After seeing the code quality I thought that, that particular user might not be familiar with C++ STL. – Arun Suryan Mar 19 '20 at 07:57