Advertisement
Guest User

solution of array addition problem

a guest
Dec 21st, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int i, j, n, sum;
  4. int diagsum_l2r[15]; //diagonal sums from left-to-right go here
  5. int diagsum_r2l[15]; //diagonal sums from right to left go here
  6.  
  7. //this is the code for the "\" (left-to-right) diagonals
  8.     for (n = 0; n <= 7; n++) //n counts the squares of each diagonal
  9.     {
  10.         sum = 0; //initialize sum to 0, here goes the sum of each diagonal line
  11.         for (i = 0; i <= n; i++)
  12.         {
  13.             for (j = 0; j <= n; j++)                                    //What we basically do here is this:
  14.             {                                               //We start from the top left corner. Then,
  15.                 if (i + j == n) //if element belongs to the main diagonal of the "sub-board"        //following the major "\" diagonal line of the main board,
  16.                 {//Observe: For all elements: i + j = n                         //we divide the board into "sub-boards" of size n.
  17.                     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.
  18.                 }
  19.             }
  20.         }
  21.         diagsum_l2r[n] = sum; //the sum of each diagonal goes to the array
  22.     }
  23.  
  24.     for (n = 0; n <= 6; n++)  //same for the rest 7 diagonals
  25.     {
  26.         sum = 0;
  27.         for (i = 7; i >= 0; i--)
  28.         {
  29.             for (j = 7; j >= 0; j--)
  30.             {
  31.                 if (i + j == 14 - n)
  32.                 {
  33.                     sum += array[i][j];
  34.                 }
  35.             }
  36.         }
  37.         diagsum_l2r[14 - n] = sum;
  38.     }
  39.  
  40.     for (n = 0; n <= 7; n++) //first 8 "/" diagonals (starting from the top left corner)
  41.     {
  42.         sum = 0;
  43.         for (i = 7; i >= 7 - n; i--)
  44.         {
  45.             for (j = 0; j <= n; j++)
  46.             {
  47.                 if (i - j == 7 - n)
  48.                 {
  49.                     sum += array[i][j];
  50.                 }
  51.             }
  52.         }
  53.         diagsum_r2l[n] = sum;
  54.     }
  55.  
  56.     for (n = 0; n <= 6; n++) //last 7 "/" diagonals
  57.     {
  58.         sum = 0;
  59.         for (i = 0; i <= 7; i++)
  60.         {
  61.             for (j = 7; j >= 0; j--)
  62.             {
  63.                 if (j - i == 7 - n)
  64.                 {
  65.                     sum += array[i][j];
  66.                 }
  67.             }
  68.         }
  69.         diagsum_r2l[14 - n] = sum;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement