# Code Vita: Chakravyuha

Problem Statement

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

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

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:

#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("%d\t", a[ i ][ j ]);
}
printf("\n");
}

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

printf("Total Power points : %d\n",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.

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("%d\n", 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("%d\n", 1 + (n2 / 11));
print_coords(get_coords_of_army(1, n), stdout);
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("%d\n", 1 + (n2 / 11));
for (int i = 1; i <= n2; ++i) {
if (has_power_point(i)) {
print_coords(get_coords_of_army(i, n), stdout);
}
}

• So my logic to create the matrix is good ? . I didnt find anything about it in the review. Thanks. – Phoenix Aug 1 '18 at 8:50
• @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!) – Quuxplusone Aug 1 '18 at 18:13