Code Vita : Chakravyuha












2












$begingroup$


Problem Statement




A Chakravyuha is a wheel-like formation. Pictorially it is depicted as
below



enter image description here



A Chakravyuha has a very well-defined co-ordinate system. Each point
on the co-ordinate system is manned by a certain unit of the army. The
Commander-In-Chief is always located at the center of the army to
better co-ordinate his forces. The only way to crack the Chakravyuha
is to defeat the units in sequential order.



A Sequential order of units differs structurally based on the radius
of the Chakra. The radius can be thought of as length or breadth of
the matrix depicted above. The structure i.e. placement of units in
sequential order is as shown below



enter image description here



The entry point of the Chakravyuha is always at the (0,0) co-ordinate
of the matrix above. This is where the 1st army unit guards. From
(0,0) i.e. 1st unit Abhimanyu has to march towards the center at (2,2)
where the 25th i.e. the last of the enemy army unit guards. Remember
that he has to proceed by destroying the units in sequential fashion.
After destroying the first unit, Abhimanyu gets a power point.
Thereafter, he gets one after destroying army units which are
multiples of 11. You should also be a in a position to tell Yudhisthir
Maharaj the location at which Abhimanyu collected his power points.




Input Format:




First line of input will be length as well as breadth of the army units, say N




Output Format:





  • Print NxN matrix depicting the placement of army units, with unit numbers delimited by (t) Tab character

  • Print Total power points collected

  • Print coordinates of power points collected in sequential fashion (one per line)

  • Print coordinates of power points collected in sequential fashion (one per line)




Constraints:




0 < N <=100




The Logic:




enter image description here




The Code:



#include<stdio.h>

int isDiv(int);

int main(){

int n;
scanf("%d",&n);

//check contraint
if(n<=0 || n>100){
return 0;
}

int a[100][100]; // main matrix

int counter=1; // keeps track of number

int i,j; //keeps track of position

int w; // w - window

int size=n-1; // size - size of segment of window

for( w = 0 ; w < n / 2 ; w++){
i = w;
j = w;

//go right
for( j = j ; j < size + w ; j++){
a[ i ][ j ] = counter;
counter++;
}

//go down
for( i = i ; i < size + w ; i++){
a[ i ][ j ] = counter;
counter++;
}

//go left
for( j = j ; j > w ; j--){
a[ i ][ j ] = counter;
counter++;
}

//go up
for( i = i ; i > w ; i--){
a[ i ][ j ] = counter;
counter++;
}
size = size-2;
}
if( n % 2 != 0){
a[ w ][ w ] = counter;
}

//print matrix
for( i = 0 ; i < n ; i++){
for( j = 0 ; j < n ; j++){
printf("%dt", a[ i ][ j ]);
}
printf("n");
}

int no_div = (n*n)/11 + 1 ; // no of divisibles

printf("Total Power points : %dn",no_div);

printf("(0,0)n");

size=n-1;

//print positions
for( w = 0 ; w < n / 2 ; w++){
i = w;
j = w;

//go right
for( j = j ; j < size + w ; j++){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)n", i, j);
}
}

//go down
for( i = i ; i < size + w ; i++){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)n", i, j);
}
}

//go left
for( j = j ; j > w ; j--){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)n", i, j);
}
}

//go up
for( i = i ; i > w ; i--){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)n", i, j);
}
}
size = size-2;
}
if( n % 2 != 0){
if(isDiv( a[ i ][ j ] )){
printf("(%d,%d)n", i, j);
}
}

return 0;
}

int isDiv(int x){
if( x % 11 == 0 ){
return 1;
}
else{
return 0;
}
}


The Code is working fine as can be checked on Ideone










share|improve this question











$endgroup$

















    2












    $begingroup$


    Problem Statement




    A Chakravyuha is a wheel-like formation. Pictorially it is depicted as
    below



    enter image description here



    A Chakravyuha has a very well-defined co-ordinate system. Each point
    on the co-ordinate system is manned by a certain unit of the army. The
    Commander-In-Chief is always located at the center of the army to
    better co-ordinate his forces. The only way to crack the Chakravyuha
    is to defeat the units in sequential order.



    A Sequential order of units differs structurally based on the radius
    of the Chakra. The radius can be thought of as length or breadth of
    the matrix depicted above. The structure i.e. placement of units in
    sequential order is as shown below



    enter image description here



    The entry point of the Chakravyuha is always at the (0,0) co-ordinate
    of the matrix above. This is where the 1st army unit guards. From
    (0,0) i.e. 1st unit Abhimanyu has to march towards the center at (2,2)
    where the 25th i.e. the last of the enemy army unit guards. Remember
    that he has to proceed by destroying the units in sequential fashion.
    After destroying the first unit, Abhimanyu gets a power point.
    Thereafter, he gets one after destroying army units which are
    multiples of 11. You should also be a in a position to tell Yudhisthir
    Maharaj the location at which Abhimanyu collected his power points.




    Input Format:




    First line of input will be length as well as breadth of the army units, say N




    Output Format:





    • Print NxN matrix depicting the placement of army units, with unit numbers delimited by (t) Tab character

    • Print Total power points collected

    • Print coordinates of power points collected in sequential fashion (one per line)

    • Print coordinates of power points collected in sequential fashion (one per line)




    Constraints:




    0 < N <=100




    The Logic:




    enter image description here




    The Code:



    #include<stdio.h>

    int isDiv(int);

    int main(){

    int n;
    scanf("%d",&n);

    //check contraint
    if(n<=0 || n>100){
    return 0;
    }

    int a[100][100]; // main matrix

    int counter=1; // keeps track of number

    int i,j; //keeps track of position

    int w; // w - window

    int size=n-1; // size - size of segment of window

    for( w = 0 ; w < n / 2 ; w++){
    i = w;
    j = w;

    //go right
    for( j = j ; j < size + w ; j++){
    a[ i ][ j ] = counter;
    counter++;
    }

    //go down
    for( i = i ; i < size + w ; i++){
    a[ i ][ j ] = counter;
    counter++;
    }

    //go left
    for( j = j ; j > w ; j--){
    a[ i ][ j ] = counter;
    counter++;
    }

    //go up
    for( i = i ; i > w ; i--){
    a[ i ][ j ] = counter;
    counter++;
    }
    size = size-2;
    }
    if( n % 2 != 0){
    a[ w ][ w ] = counter;
    }

    //print matrix
    for( i = 0 ; i < n ; i++){
    for( j = 0 ; j < n ; j++){
    printf("%dt", a[ i ][ j ]);
    }
    printf("n");
    }

    int no_div = (n*n)/11 + 1 ; // no of divisibles

    printf("Total Power points : %dn",no_div);

    printf("(0,0)n");

    size=n-1;

    //print positions
    for( w = 0 ; w < n / 2 ; w++){
    i = w;
    j = w;

    //go right
    for( j = j ; j < size + w ; j++){
    if(isDiv( a[ i ][ j ] )){
    printf("(%d,%d)n", i, j);
    }
    }

    //go down
    for( i = i ; i < size + w ; i++){
    if(isDiv( a[ i ][ j ] )){
    printf("(%d,%d)n", i, j);
    }
    }

    //go left
    for( j = j ; j > w ; j--){
    if(isDiv( a[ i ][ j ] )){
    printf("(%d,%d)n", i, j);
    }
    }

    //go up
    for( i = i ; i > w ; i--){
    if(isDiv( a[ i ][ j ] )){
    printf("(%d,%d)n", i, j);
    }
    }
    size = size-2;
    }
    if( n % 2 != 0){
    if(isDiv( a[ i ][ j ] )){
    printf("(%d,%d)n", i, j);
    }
    }

    return 0;
    }

    int isDiv(int x){
    if( x % 11 == 0 ){
    return 1;
    }
    else{
    return 0;
    }
    }


    The Code is working fine as can be checked on Ideone










    share|improve this question











    $endgroup$















      2












      2








      2





      $begingroup$


      Problem Statement




      A Chakravyuha is a wheel-like formation. Pictorially it is depicted as
      below



      enter image description here



      A Chakravyuha has a very well-defined co-ordinate system. Each point
      on the co-ordinate system is manned by a certain unit of the army. The
      Commander-In-Chief is always located at the center of the army to
      better co-ordinate his forces. The only way to crack the Chakravyuha
      is to defeat the units in sequential order.



      A Sequential order of units differs structurally based on the radius
      of the Chakra. The radius can be thought of as length or breadth of
      the matrix depicted above. The structure i.e. placement of units in
      sequential order is as shown below



      enter image description here



      The entry point of the Chakravyuha is always at the (0,0) co-ordinate
      of the matrix above. This is where the 1st army unit guards. From
      (0,0) i.e. 1st unit Abhimanyu has to march towards the center at (2,2)
      where the 25th i.e. the last of the enemy army unit guards. Remember
      that he has to proceed by destroying the units in sequential fashion.
      After destroying the first unit, Abhimanyu gets a power point.
      Thereafter, he gets one after destroying army units which are
      multiples of 11. You should also be a in a position to tell Yudhisthir
      Maharaj the location at which Abhimanyu collected his power points.




      Input Format:




      First line of input will be length as well as breadth of the army units, say N




      Output Format:





      • Print NxN matrix depicting the placement of army units, with unit numbers delimited by (t) Tab character

      • Print Total power points collected

      • Print coordinates of power points collected in sequential fashion (one per line)

      • Print coordinates of power points collected in sequential fashion (one per line)




      Constraints:




      0 < N <=100




      The Logic:




      enter image description here




      The Code:



      #include<stdio.h>

      int isDiv(int);

      int main(){

      int n;
      scanf("%d",&n);

      //check contraint
      if(n<=0 || n>100){
      return 0;
      }

      int a[100][100]; // main matrix

      int counter=1; // keeps track of number

      int i,j; //keeps track of position

      int w; // w - window

      int size=n-1; // size - size of segment of window

      for( w = 0 ; w < n / 2 ; w++){
      i = w;
      j = w;

      //go right
      for( j = j ; j < size + w ; j++){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go down
      for( i = i ; i < size + w ; i++){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go left
      for( j = j ; j > w ; j--){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go up
      for( i = i ; i > w ; i--){
      a[ i ][ j ] = counter;
      counter++;
      }
      size = size-2;
      }
      if( n % 2 != 0){
      a[ w ][ w ] = counter;
      }

      //print matrix
      for( i = 0 ; i < n ; i++){
      for( j = 0 ; j < n ; j++){
      printf("%dt", a[ i ][ j ]);
      }
      printf("n");
      }

      int no_div = (n*n)/11 + 1 ; // no of divisibles

      printf("Total Power points : %dn",no_div);

      printf("(0,0)n");

      size=n-1;

      //print positions
      for( w = 0 ; w < n / 2 ; w++){
      i = w;
      j = w;

      //go right
      for( j = j ; j < size + w ; j++){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go down
      for( i = i ; i < size + w ; i++){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go left
      for( j = j ; j > w ; j--){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go up
      for( i = i ; i > w ; i--){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }
      size = size-2;
      }
      if( n % 2 != 0){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      return 0;
      }

      int isDiv(int x){
      if( x % 11 == 0 ){
      return 1;
      }
      else{
      return 0;
      }
      }


      The Code is working fine as can be checked on Ideone










      share|improve this question











      $endgroup$




      Problem Statement




      A Chakravyuha is a wheel-like formation. Pictorially it is depicted as
      below



      enter image description here



      A Chakravyuha has a very well-defined co-ordinate system. Each point
      on the co-ordinate system is manned by a certain unit of the army. The
      Commander-In-Chief is always located at the center of the army to
      better co-ordinate his forces. The only way to crack the Chakravyuha
      is to defeat the units in sequential order.



      A Sequential order of units differs structurally based on the radius
      of the Chakra. The radius can be thought of as length or breadth of
      the matrix depicted above. The structure i.e. placement of units in
      sequential order is as shown below



      enter image description here



      The entry point of the Chakravyuha is always at the (0,0) co-ordinate
      of the matrix above. This is where the 1st army unit guards. From
      (0,0) i.e. 1st unit Abhimanyu has to march towards the center at (2,2)
      where the 25th i.e. the last of the enemy army unit guards. Remember
      that he has to proceed by destroying the units in sequential fashion.
      After destroying the first unit, Abhimanyu gets a power point.
      Thereafter, he gets one after destroying army units which are
      multiples of 11. You should also be a in a position to tell Yudhisthir
      Maharaj the location at which Abhimanyu collected his power points.




      Input Format:




      First line of input will be length as well as breadth of the army units, say N




      Output Format:





      • Print NxN matrix depicting the placement of army units, with unit numbers delimited by (t) Tab character

      • Print Total power points collected

      • Print coordinates of power points collected in sequential fashion (one per line)

      • Print coordinates of power points collected in sequential fashion (one per line)




      Constraints:




      0 < N <=100




      The Logic:




      enter image description here




      The Code:



      #include<stdio.h>

      int isDiv(int);

      int main(){

      int n;
      scanf("%d",&n);

      //check contraint
      if(n<=0 || n>100){
      return 0;
      }

      int a[100][100]; // main matrix

      int counter=1; // keeps track of number

      int i,j; //keeps track of position

      int w; // w - window

      int size=n-1; // size - size of segment of window

      for( w = 0 ; w < n / 2 ; w++){
      i = w;
      j = w;

      //go right
      for( j = j ; j < size + w ; j++){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go down
      for( i = i ; i < size + w ; i++){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go left
      for( j = j ; j > w ; j--){
      a[ i ][ j ] = counter;
      counter++;
      }

      //go up
      for( i = i ; i > w ; i--){
      a[ i ][ j ] = counter;
      counter++;
      }
      size = size-2;
      }
      if( n % 2 != 0){
      a[ w ][ w ] = counter;
      }

      //print matrix
      for( i = 0 ; i < n ; i++){
      for( j = 0 ; j < n ; j++){
      printf("%dt", a[ i ][ j ]);
      }
      printf("n");
      }

      int no_div = (n*n)/11 + 1 ; // no of divisibles

      printf("Total Power points : %dn",no_div);

      printf("(0,0)n");

      size=n-1;

      //print positions
      for( w = 0 ; w < n / 2 ; w++){
      i = w;
      j = w;

      //go right
      for( j = j ; j < size + w ; j++){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go down
      for( i = i ; i < size + w ; i++){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go left
      for( j = j ; j > w ; j--){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      //go up
      for( i = i ; i > w ; i--){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }
      size = size-2;
      }
      if( n % 2 != 0){
      if(isDiv( a[ i ][ j ] )){
      printf("(%d,%d)n", i, j);
      }
      }

      return 0;
      }

      int isDiv(int x){
      if( x % 11 == 0 ){
      return 1;
      }
      else{
      return 0;
      }
      }


      The Code is working fine as can be checked on Ideone







      performance algorithm c programming-challenge






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 31 '18 at 19:54







      Phoenix

















      asked Jul 31 '18 at 19:35









      PhoenixPhoenix

      1216




      1216






















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

          You should try to keep your main function small and focused. (Well, you should try to keep every function you write small and focused!) For example, you might do something like this:



          struct Coord {
          int row, col;
          };
          struct CoordList {
          int size;
          Coord *data;
          };

          void print_coords(Coord c, FILE *out) {
          printf("(%d,%d)n", c.row, c.col);
          }

          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          CoordList coords = collect_power_points(n);
          printf("%dn", coords.size);
          for (int i=0; i < coords.size; ++i) {
          print_coords(coords.data[i]);
          }
          }


          Writing the little functions print_tab_delimited_matrix and collect_power_points is left as an exercise for the reader.





          Or, you might consider that there's no real point to collecting up all of the power-point coordinates in a physical list. You already know the formula for computing where each power point is picked up! So you might scrap the above code and rewrite your whole program as:



          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          int n2 = n * n;
          printf("%dn", 1 + (n2 / 11));
          print_coords(get_coords_of_army(1, n), stdout);
          printf("(%d,%d)n", c.row, c.col);
          for (int i = 11; i <= n2; i += 11) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }


          Writing the function get_coords_of_army is left as an exercise for the reader. But the important thing to realize is that writing that function is supposed to be the challenge! You should develop the instinct and intuition for quickly breaking down a problem statement into sub-problems, and then solving those sub-problems. Notice that outside of main I don't need to do any input or output; I just have a very clean mathematical problem to solve.



          Coord get_coords_of_army(int which_army, int breadth_of_square);


          Everything else going on in this problem statement is basically a red herring.





          I note in passing that isDiv(x) is not a good name for a function that computes whether x is divisible by 11. You get points for naming it is...-something, but surely (A) is_divisible_by_11 would have been a better name, and (B) you don't really need a function for that!



          You could make the argument that this is futureproofing the code: what if the next iteration of the puzzle changes the locations of the power points? That's fair, and makes is_divisible_by_11 a bad name... but it doesn't make isDiv a good name! We might better write



          bool has_power_point(int which_army) {
          return (which_army == 1) || (which_army % 11 == 0);
          }


          and then back in our main function we can write simply



              printf("%dn", 1 + (n2 / 11));
          for (int i = 1; i <= n2; ++i) {
          if (has_power_point(i)) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }





          share|improve this answer









          $endgroup$













          • $begingroup$
            So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
            $endgroup$
            – Phoenix
            Aug 1 '18 at 8:50










          • $begingroup$
            @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
            $endgroup$
            – Quuxplusone
            Aug 1 '18 at 18:13











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200690%2fcode-vita-chakravyuha%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2












          $begingroup$

          You should try to keep your main function small and focused. (Well, you should try to keep every function you write small and focused!) For example, you might do something like this:



          struct Coord {
          int row, col;
          };
          struct CoordList {
          int size;
          Coord *data;
          };

          void print_coords(Coord c, FILE *out) {
          printf("(%d,%d)n", c.row, c.col);
          }

          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          CoordList coords = collect_power_points(n);
          printf("%dn", coords.size);
          for (int i=0; i < coords.size; ++i) {
          print_coords(coords.data[i]);
          }
          }


          Writing the little functions print_tab_delimited_matrix and collect_power_points is left as an exercise for the reader.





          Or, you might consider that there's no real point to collecting up all of the power-point coordinates in a physical list. You already know the formula for computing where each power point is picked up! So you might scrap the above code and rewrite your whole program as:



          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          int n2 = n * n;
          printf("%dn", 1 + (n2 / 11));
          print_coords(get_coords_of_army(1, n), stdout);
          printf("(%d,%d)n", c.row, c.col);
          for (int i = 11; i <= n2; i += 11) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }


          Writing the function get_coords_of_army is left as an exercise for the reader. But the important thing to realize is that writing that function is supposed to be the challenge! You should develop the instinct and intuition for quickly breaking down a problem statement into sub-problems, and then solving those sub-problems. Notice that outside of main I don't need to do any input or output; I just have a very clean mathematical problem to solve.



          Coord get_coords_of_army(int which_army, int breadth_of_square);


          Everything else going on in this problem statement is basically a red herring.





          I note in passing that isDiv(x) is not a good name for a function that computes whether x is divisible by 11. You get points for naming it is...-something, but surely (A) is_divisible_by_11 would have been a better name, and (B) you don't really need a function for that!



          You could make the argument that this is futureproofing the code: what if the next iteration of the puzzle changes the locations of the power points? That's fair, and makes is_divisible_by_11 a bad name... but it doesn't make isDiv a good name! We might better write



          bool has_power_point(int which_army) {
          return (which_army == 1) || (which_army % 11 == 0);
          }


          and then back in our main function we can write simply



              printf("%dn", 1 + (n2 / 11));
          for (int i = 1; i <= n2; ++i) {
          if (has_power_point(i)) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }





          share|improve this answer









          $endgroup$













          • $begingroup$
            So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
            $endgroup$
            – Phoenix
            Aug 1 '18 at 8:50










          • $begingroup$
            @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
            $endgroup$
            – Quuxplusone
            Aug 1 '18 at 18:13
















          2












          $begingroup$

          You should try to keep your main function small and focused. (Well, you should try to keep every function you write small and focused!) For example, you might do something like this:



          struct Coord {
          int row, col;
          };
          struct CoordList {
          int size;
          Coord *data;
          };

          void print_coords(Coord c, FILE *out) {
          printf("(%d,%d)n", c.row, c.col);
          }

          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          CoordList coords = collect_power_points(n);
          printf("%dn", coords.size);
          for (int i=0; i < coords.size; ++i) {
          print_coords(coords.data[i]);
          }
          }


          Writing the little functions print_tab_delimited_matrix and collect_power_points is left as an exercise for the reader.





          Or, you might consider that there's no real point to collecting up all of the power-point coordinates in a physical list. You already know the formula for computing where each power point is picked up! So you might scrap the above code and rewrite your whole program as:



          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          int n2 = n * n;
          printf("%dn", 1 + (n2 / 11));
          print_coords(get_coords_of_army(1, n), stdout);
          printf("(%d,%d)n", c.row, c.col);
          for (int i = 11; i <= n2; i += 11) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }


          Writing the function get_coords_of_army is left as an exercise for the reader. But the important thing to realize is that writing that function is supposed to be the challenge! You should develop the instinct and intuition for quickly breaking down a problem statement into sub-problems, and then solving those sub-problems. Notice that outside of main I don't need to do any input or output; I just have a very clean mathematical problem to solve.



          Coord get_coords_of_army(int which_army, int breadth_of_square);


          Everything else going on in this problem statement is basically a red herring.





          I note in passing that isDiv(x) is not a good name for a function that computes whether x is divisible by 11. You get points for naming it is...-something, but surely (A) is_divisible_by_11 would have been a better name, and (B) you don't really need a function for that!



          You could make the argument that this is futureproofing the code: what if the next iteration of the puzzle changes the locations of the power points? That's fair, and makes is_divisible_by_11 a bad name... but it doesn't make isDiv a good name! We might better write



          bool has_power_point(int which_army) {
          return (which_army == 1) || (which_army % 11 == 0);
          }


          and then back in our main function we can write simply



              printf("%dn", 1 + (n2 / 11));
          for (int i = 1; i <= n2; ++i) {
          if (has_power_point(i)) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }





          share|improve this answer









          $endgroup$













          • $begingroup$
            So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
            $endgroup$
            – Phoenix
            Aug 1 '18 at 8:50










          • $begingroup$
            @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
            $endgroup$
            – Quuxplusone
            Aug 1 '18 at 18:13














          2












          2








          2





          $begingroup$

          You should try to keep your main function small and focused. (Well, you should try to keep every function you write small and focused!) For example, you might do something like this:



          struct Coord {
          int row, col;
          };
          struct CoordList {
          int size;
          Coord *data;
          };

          void print_coords(Coord c, FILE *out) {
          printf("(%d,%d)n", c.row, c.col);
          }

          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          CoordList coords = collect_power_points(n);
          printf("%dn", coords.size);
          for (int i=0; i < coords.size; ++i) {
          print_coords(coords.data[i]);
          }
          }


          Writing the little functions print_tab_delimited_matrix and collect_power_points is left as an exercise for the reader.





          Or, you might consider that there's no real point to collecting up all of the power-point coordinates in a physical list. You already know the formula for computing where each power point is picked up! So you might scrap the above code and rewrite your whole program as:



          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          int n2 = n * n;
          printf("%dn", 1 + (n2 / 11));
          print_coords(get_coords_of_army(1, n), stdout);
          printf("(%d,%d)n", c.row, c.col);
          for (int i = 11; i <= n2; i += 11) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }


          Writing the function get_coords_of_army is left as an exercise for the reader. But the important thing to realize is that writing that function is supposed to be the challenge! You should develop the instinct and intuition for quickly breaking down a problem statement into sub-problems, and then solving those sub-problems. Notice that outside of main I don't need to do any input or output; I just have a very clean mathematical problem to solve.



          Coord get_coords_of_army(int which_army, int breadth_of_square);


          Everything else going on in this problem statement is basically a red herring.





          I note in passing that isDiv(x) is not a good name for a function that computes whether x is divisible by 11. You get points for naming it is...-something, but surely (A) is_divisible_by_11 would have been a better name, and (B) you don't really need a function for that!



          You could make the argument that this is futureproofing the code: what if the next iteration of the puzzle changes the locations of the power points? That's fair, and makes is_divisible_by_11 a bad name... but it doesn't make isDiv a good name! We might better write



          bool has_power_point(int which_army) {
          return (which_army == 1) || (which_army % 11 == 0);
          }


          and then back in our main function we can write simply



              printf("%dn", 1 + (n2 / 11));
          for (int i = 1; i <= n2; ++i) {
          if (has_power_point(i)) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }





          share|improve this answer









          $endgroup$



          You should try to keep your main function small and focused. (Well, you should try to keep every function you write small and focused!) For example, you might do something like this:



          struct Coord {
          int row, col;
          };
          struct CoordList {
          int size;
          Coord *data;
          };

          void print_coords(Coord c, FILE *out) {
          printf("(%d,%d)n", c.row, c.col);
          }

          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          CoordList coords = collect_power_points(n);
          printf("%dn", coords.size);
          for (int i=0; i < coords.size; ++i) {
          print_coords(coords.data[i]);
          }
          }


          Writing the little functions print_tab_delimited_matrix and collect_power_points is left as an exercise for the reader.





          Or, you might consider that there's no real point to collecting up all of the power-point coordinates in a physical list. You already know the formula for computing where each power point is picked up! So you might scrap the above code and rewrite your whole program as:



          int main() {
          int n;
          scanf("%d", &n);
          print_tab_delimited_matrix(n, stdout);
          int n2 = n * n;
          printf("%dn", 1 + (n2 / 11));
          print_coords(get_coords_of_army(1, n), stdout);
          printf("(%d,%d)n", c.row, c.col);
          for (int i = 11; i <= n2; i += 11) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }


          Writing the function get_coords_of_army is left as an exercise for the reader. But the important thing to realize is that writing that function is supposed to be the challenge! You should develop the instinct and intuition for quickly breaking down a problem statement into sub-problems, and then solving those sub-problems. Notice that outside of main I don't need to do any input or output; I just have a very clean mathematical problem to solve.



          Coord get_coords_of_army(int which_army, int breadth_of_square);


          Everything else going on in this problem statement is basically a red herring.





          I note in passing that isDiv(x) is not a good name for a function that computes whether x is divisible by 11. You get points for naming it is...-something, but surely (A) is_divisible_by_11 would have been a better name, and (B) you don't really need a function for that!



          You could make the argument that this is futureproofing the code: what if the next iteration of the puzzle changes the locations of the power points? That's fair, and makes is_divisible_by_11 a bad name... but it doesn't make isDiv a good name! We might better write



          bool has_power_point(int which_army) {
          return (which_army == 1) || (which_army % 11 == 0);
          }


          and then back in our main function we can write simply



              printf("%dn", 1 + (n2 / 11));
          for (int i = 1; i <= n2; ++i) {
          if (has_power_point(i)) {
          print_coords(get_coords_of_army(i, n), stdout);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 1 '18 at 1:05









          QuuxplusoneQuuxplusone

          12.5k12061




          12.5k12061












          • $begingroup$
            So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
            $endgroup$
            – Phoenix
            Aug 1 '18 at 8:50










          • $begingroup$
            @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
            $endgroup$
            – Quuxplusone
            Aug 1 '18 at 18:13


















          • $begingroup$
            So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
            $endgroup$
            – Phoenix
            Aug 1 '18 at 8:50










          • $begingroup$
            @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
            $endgroup$
            – Quuxplusone
            Aug 1 '18 at 18:13
















          $begingroup$
          So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
          $endgroup$
          – Phoenix
          Aug 1 '18 at 8:50




          $begingroup$
          So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks.
          $endgroup$
          – Phoenix
          Aug 1 '18 at 8:50












          $begingroup$
          @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
          $endgroup$
          – Quuxplusone
          Aug 1 '18 at 18:13




          $begingroup$
          @Phoenix: Well, I didn't look at your code very deeply — just deeply enough to see that it was one huge clump of nested for-loops that I didn't want to get too far into. From the above review, you should be able to tell that I would have tried to write print_tab_delimited_matrix(n) in terms of get_army_for_coords(coords, n), which is just the inverse of get_coords_of_army(i, n). (Think about how to use that relationship to write unit tests for your code!)
          $endgroup$
          – Quuxplusone
          Aug 1 '18 at 18:13


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200690%2fcode-vita-chakravyuha%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар