Advertisement
Lisaveta777

Parts of 2D array

Nov 4th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define WIDE 15
  4. #define HIGH 9
  5. #define SIZE 5 //this is square size
  6. //first part - printint triangles - just to illustrate which areas of array
  7. //are summarise at every instance
  8. //1. prints triangles of differents directions (with stars), inside rectangles (-)
  9. //and for gaps uses +. this used as preparation for 2D array
  10. //2. prints array elements, covered by triangles in first part, summarise them
  11. //? i think, for j-formula it's better to use the following order: top_gap, left_gap
  12. //SIZE, i - it have to be standartased, IMHO, and that order seems the most logical
  13. //plus - have to check all formulas! seem they far from being ideal
  14.  
  15.  
  16. int main()
  17. {
  18.    int i,j,arr[HIGH][WIDE],top_gap,left_gap,sum=0;
  19.    printf("Enter rows_gap,cols_gap: ");
  20.    scanf("%d %d",&top_gap,&left_gap);
  21.    for(i=0;i<HIGH;i++)//populate&print array
  22.    {
  23.        for(j=0;j<WIDE;j++)
  24.        {
  25.            arr[i][j] = WIDE*i+j;
  26.            printf("%4d",arr[i][j]);
  27.        }
  28.        printf("\n");
  29.    }
  30.    for(i=0;i<HIGH;i++)//1  1 star + 9 spaces
  31.    {
  32.        if(i<top_gap||i>=top_gap+SIZE)
  33.             {
  34.                 for(j=0;j<WIDE;j++)
  35.                     printf("+");
  36.                 printf("\n");
  37.             }
  38.        else
  39.         {
  40.             for(j=0;j<WIDE;j++)
  41.             {
  42.                 printf("%c",(j>=left_gap&&j<=left_gap-top_gap+i)?'*':'-');//1 star
  43.             }
  44.         printf("\n");
  45.  
  46.         }//end of else
  47.    }
  48.    for(i=0;i<HIGH;i++)//2. 10 stars+ 0 spaces
  49.    {
  50.        if(i<top_gap||i>=top_gap+SIZE)
  51.             {
  52.                 for(j=0;j<WIDE;j++)
  53.                     printf("+");
  54.                 printf("\n");
  55.             }
  56.        else
  57.        {
  58.            for(j=0;j<WIDE;j++)
  59.             {
  60.                 printf("%c",(j>=left_gap&&j<SIZE+left_gap+top_gap-i)?'*':'-');
  61.             }
  62.           printf("\n");
  63.        }
  64.  
  65.  
  66.    }
  67.    for(i=0;i<HIGH;i++)//3. 9 spaces + 1 star
  68.    {
  69.        if(i<top_gap||i>=top_gap+SIZE)
  70.             {
  71.                 for(j=0;j<WIDE;j++)
  72.                     printf("+");
  73.                 printf("\n");
  74.             }
  75.        else
  76.        {
  77.            for(j=0;j<WIDE;j++)
  78.             {
  79.                 printf("%c",(j>=left_gap+top_gap+SIZE-i-1&&j<left_gap+SIZE)?'*':'-');
  80.             }
  81.        printf("\n");
  82.        }
  83.  
  84.  
  85.    }
  86.    for(i=0;i<HIGH;i++)//4. 0 spaces + 10 stars
  87.    {
  88.        if(i<top_gap||i>=top_gap+SIZE)
  89.             {
  90.                 for(j=0;j<WIDE;j++)
  91.                     printf("+");
  92.                 printf("\n");
  93.             }
  94.        else
  95.        {
  96.            for(j=0;j<WIDE;j++)
  97.             {
  98.                 printf("%c",(j>=left_gap-top_gap+i&&j<left_gap+SIZE)?'*':'-');
  99.             }
  100.            printf("\n");
  101.        }
  102.  
  103.  
  104.    }
  105.  
  106.     for(i=top_gap;i<top_gap+SIZE;i++)//1 arr
  107.     {
  108.         //for(j=left_gap;j<=left_gap+i;j++)//before top_gap
  109.         for(j=left_gap;j<=left_gap-top_gap+i;j++)
  110.             printf("%d  ",arr[i][j]),sum+=arr[i][j];
  111.         printf("\n");
  112.     }
  113.     printf("equals to %d\n",sum);
  114.  
  115.  
  116.     sum=0;//have to be set up to zero for new cycle
  117.     for(i=top_gap;i<top_gap+SIZE;i++)//2 arr
  118.     {
  119.         //for(j=left_gap;j<left_gap+SIZE-i;j++)/before top_gap
  120.         for(j=left_gap;j<SIZE+left_gap+top_gap-i;j++)
  121.             printf("%d  ",arr[i][j]),sum+=arr[i][j];
  122.         printf("\n");
  123.     }
  124.     printf("equals to %d\n",sum);
  125.  
  126.     sum=0;//have to be set up to zero for new cycle
  127.     for(i=top_gap;i<top_gap+SIZE;i++)//3 arr
  128.     {
  129.         //for(j=left_gap+SIZE-i-1;j<left_gap+SIZE;j++)//before top_gap
  130.         for(j=left_gap+top_gap+SIZE-i-1;j<left_gap+SIZE;j++)
  131.             printf("%d  ",arr[i][j]),sum+=arr[i][j];
  132.         printf("\n");
  133.     }
  134.     printf("equals to %d\n",sum);
  135.  
  136.     sum=0;//have to be set up to zero for new cycle
  137.     for(i=top_gap;i<top_gap+SIZE;i++)//4 arr
  138.     {
  139.         //for(j=left_gap+i;j<left_gap+SIZE;j++)//before top_gap
  140.         for(j=left_gap-top_gap+i;j<left_gap+SIZE;j++)
  141.             printf("%d  ",arr[i][j]),sum+=arr[i][j];
  142.         printf("\n");
  143.     }
  144.     printf("equals to %d\n",sum);
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement