2-D array diagonal help

May 27, 2017 at 9:09am
Trying to calculate the Difference in the sum of left diagonal and right diagonal of a 2-D array. This is the code that I wrote and it is working but not giving the correct results. The right diagonal sum is working properly but the left diagonal sum keeps coming out to be 0.

This is a problem from the HackerRank algorith section "Diagonal Difference"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() {
   
    int a[100][100], sum1=0, sum2=0, sum3;
    int n;
    cin>>n;
  //input to 2-d array
    for(int i=0;i<n;i++){      
        for(int f=0;f<n;f++)
            cin>>a[i][f];
    }
   // sum for right diagonal
    for(int p=0;p<n;p++)
        sum1 = sum1 + a[p][p];
   //sum for left diagonal
    for(int r=0;r<n;r++){
        for(int g=n;g=0;g++)
            sum2 = sum2 + a[r][g];
    }
    
    sum3 = sum1 - sum2;
   sum3 = abs(sum3);  //abs is used to return absolute value
    cout<<sum3;
    return 0;
}
Last edited on May 27, 2017 at 9:15am
May 27, 2017 at 10:28am
for(int g=n;g=0;g++)

Can you explain what the 3 parts of a for loop do?
May 27, 2017 at 10:46am
OH FUCK THAT WAS SO DUMB OF ME!!
THANKS!
May 27, 2017 at 10:48am
I tried it again
for(int g=n;g=0;g--)

it still gives 0 as the answer
May 27, 2017 at 10:53am
Repeat:

Can you explain what the 3 parts of a for loop do?
May 27, 2017 at 11:02am
The first part
int i=0;
is used for initializing

the second part
i<n
sets the constraint, that is it cannot loop more than n numbers

the third part
i++
iterates the value of i by 1
May 27, 2017 at 11:07am
Right, but that is not what you have. Can you explain why g=0 doesn't work as a conditional?
May 27, 2017 at 11:31am
I'm not sure but it maybe wrong because = is an assignment operator and I should be using == operator?
May 27, 2017 at 11:38am
Yes. It will always be true. The compiler tells us so:

16:23: warning: suggest parentheses around assignment used as truth value [-Wparentheses]


Also investigate compound assignment operators, such as += on lines 13 and 17.

Good Luck !!
May 27, 2017 at 4:53pm
Hello DatDankMeme,

You have more wrong with the left diagonal the just "g=0". After a few tries I discovered that the inner for loop never executed. I am using this as a something to work with:


  C  0  1  2  3  4
R ----------------
0 -  1  2  3  4  5
1 -  6  7  8  9 10
2 - 11 12 13 14 15
3 - 16 17 18 19 20
4 - 21 22 23 24 25


Now the right diagonal goes from R0C0 to R4C4 just fine. Keep in mind though that the left diagonal should go from R0C4 to R4C0 and the for loop should account for this. Now the first solution I came up with is:
1
2
3
4
5
6
int row{0};

for (int col = n - 1; col > -1;)
{
	sum2 += a[row++][col--];
}

note the n - 1 or it would not catch col 0 and would through the loop off by 1. I changed the variable names to make it easier to understand.

Hope that helps,

Andy

Edit: Also found this to work"
1
2
3
4
for (int col = n - 1, row = 0; col > -1; col--, row++)
{
	sum2 += a[row][col];
}
Last edited on May 27, 2017 at 7:03pm
Topic archived. No new replies allowed.