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`

?
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 :

**What I have tried:**

Here's my code, I'm planning to treat this square matrix as a 1D array

**[UPDATE]**

Solution was to modify the array insertion block to be :

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++

Copy Code

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) = 15Sample Input3 //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 -12Sample Output15

Here's my code, I'm planning to treat this square matrix as a 1D array

C++

Copy Code

#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++

Copy Code

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.Running :3 11 2 4 4 5 6 10 8 -12 15 (correct answer) /** 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 ------------------------- **/

Solution was to modify the array insertion block to be :

C++

Copy Code

//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.

Comments

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`

?
Comments

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.

DONE ! My code has passed all the sample tests ! I've updated my question to include the solution. THANK YOU !

Well done!

You are welcome.

You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900