Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int i, j, n, sum;
- int diagsum_l2r[15]; //diagonal sums from left-to-right go here
- int diagsum_r2l[15]; //diagonal sums from right to left go here
- //this is the code for the "\" (left-to-right) diagonals
- for (n = 0; n <= 7; n++) //n counts the squares of each diagonal
- {
- sum = 0; //initialize sum to 0, here goes the sum of each diagonal line
- for (i = 0; i <= n; i++)
- {
- for (j = 0; j <= n; j++) //What we basically do here is this:
- { //We start from the top left corner. Then,
- if (i + j == n) //if element belongs to the main diagonal of the "sub-board" //following the major "\" diagonal line of the main board,
- {//Observe: For all elements: i + j = n //we divide the board into "sub-boards" of size n.
- sum += array[i][j]; //then add it to the general sum //The desired sum is each time the sum of the sub-board's "/" major diagonal.
- }
- }
- }
- diagsum_l2r[n] = sum; //the sum of each diagonal goes to the array
- }
- for (n = 0; n <= 6; n++) //same for the rest 7 diagonals
- {
- sum = 0;
- for (i = 7; i >= 0; i--)
- {
- for (j = 7; j >= 0; j--)
- {
- if (i + j == 14 - n)
- {
- sum += array[i][j];
- }
- }
- }
- diagsum_l2r[14 - n] = sum;
- }
- for (n = 0; n <= 7; n++) //first 8 "/" diagonals (starting from the top left corner)
- {
- sum = 0;
- for (i = 7; i >= 7 - n; i--)
- {
- for (j = 0; j <= n; j++)
- {
- if (i - j == 7 - n)
- {
- sum += array[i][j];
- }
- }
- }
- diagsum_r2l[n] = sum;
- }
- for (n = 0; n <= 6; n++) //last 7 "/" diagonals
- {
- sum = 0;
- for (i = 0; i <= 7; i++)
- {
- for (j = 7; j >= 0; j--)
- {
- if (j - i == 7 - n)
- {
- sum += array[i][j];
- }
- }
- }
- diagsum_r2l[14 - n] = sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement