15,399,749 members
See more:
I'm trying to do an exercise from Hackerrank to compute an absolute difference of 2 diagonals from a square matrix. I have a code, but not perfect since it passes only 3/8 sample cases.
Below, is a sample test case, my code can solve this case, unfortunately I can't get access to different test cases including those that my code failed to solve because I don't have a premium account. Here's basically what the task is :
C++
```Dimension matrix : 3

11 2   4    | Diagonal 1 : 11 5 -12 -- sum_d1 =  4
4 5   6    | Diagonal 2 :  4 5  10 -- sum_d2 = 19
10 8 -12    | Abs difference diagonals : abs(sum_d1-sum_d2) = 15

Sample Input

3          //user input -- dimension of matrix square (n)
11 2   4   //actual input of matrix elements, rows based on dimension (n)
4  5   6   //for each row, input each element separated by a white space
10 8 -12

Sample Output

15 ```

What I have tried:

Here's my code, I'm planning to treat this square matrix as a 1D array
C++
```#include <iostream>
#include <cmath>

using namespace std;

int main(){
int n;
cin >> n; // square matrix of what dimension ?
int size = n*n;

int arr[size];
int iter = 0;

//filling in array, input separated by white space for each row (n)
for (int i = 0; i < n; i++){
cin >> arr[iter] >> arr[iter+=1] >> arr[iter+=1];
iter++;
}

int diag1    = 0, diag2 = 0;
int sum      = 0;
int step1    = 0, step2 = 0;

//finding diagonal elements and sum them
for (int j = 0 ; j < n; j++){
diag1 += arr[step1];
// cout << "\nElements Diagonal 1 = " << arr[step1];
step1 += (n+1);
step2 += (n-1);
diag2 += arr[step2];
// cout << "\nElements Diagonal 2 = " << arr[step2];
}

sum = abs(diag1-diag2);
cout << sum << endl;

return 0;
}```

C++
```Running :
3
11 2   4
4 5   6
10 8 -12

/** commented out in my code, to check if I got the right elements
Elements Diagonal 1 = 11
Elements Diagonal 2 = 4
Elements Diagonal 1 = 5
Elements Diagonal 2 = 5
Elements Diagonal 1 = -12
Elements Diagonal 2 = 10
------------------------- **/ ```
I am aware that there are solutions online with various approaches. However, I want to try to solve this myself and want to know what did I miss and where did my code go wrong ? As I mentioned earlier, my code passes 3/8 sample cases, and I can't check failed cases because it requires premium account. Therefore, instead of a new approach, if possible please help me understand where / how did my code would fail. Thank you.

[UPDATE]
Solution was to modify the array insertion block to be :

C++
```//filling in array with input separated by space for each row (n)
for (int i = 0; i < n; i++){
for ( int j = n; j > 0; j--){
cin >> arr[iter];
iter++;
}
}```
Thank you @CPallini for pointed out the mistake in my code.
Posted
Updated 14-Oct-21 16:33pm
v3
Rick York 13-Oct-21 12:17pm

In my opinion, treating the data as a 1D array is a mistake. I think you should make it a 2D matrix. That would likely require you to figure out how to allocate a 2D matrix and that is a good thing. It is really rather simple. First allocate an 1D array of pointers to integers for each row and then fill in each slot with a 1D array of integers for the columns. The process is reversed for releasing the memory.

## Solution 2

Quote:
for (int i = 0; i < n; i++){
cin >> arr[iter] >> arr[iter+=1] >> arr[iter+=1];
iter++;
}

You know, the above code would work only if `n` is `3` ?
lock&_lock 14-Oct-21 21:37pm

Ah, i see. Out of all my trials, I didn't try with dimension other than 3. Now I have to modify this to accept value separated with spaces following n value. I might need to use getline and vector array after all, or nested for. Thank you ! I'll update my post when I can make it work.
lock&_lock 14-Oct-21 22:29pm

DONE ! My code has passed all the sample tests ! I've updated my question to include the solution. THANK YOU !
CPallini 15-Oct-21 2:07am

Well done!
You are welcome.