Challenge cum problem in C/C++

The problem is to derive the following pattern for n=5 in output:
1 2 3 4 5
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09

simmilarly for n=6 it should be
1 2 3 4 5 6
20 21 22 23 24 7
19 32 33 34 25 8
18 31 36 35 26 9
17 30 29 28 27 10
16 15 14 13 12 11
and so on.
The code should be only in C/C++.
Let's see who got the solution first.

Replies

  • Prasad Ajinkya
    Prasad Ajinkya
    heres a hint - spiral
  • RajdeepCE
    RajdeepCE
    I know it is spiral, but how to implement it in program?
  • slashfear
    slashfear
    Hi Rajdeep,

    Nice question dude!!!! 😁

    But I am wondering......😕 if this is your home work or what......?

    Any ways I know the solution buddy.......;-) it is also called as The Spiral Matrix

    So this is how you implement in C++ (as well as in C 😛)

    
    [B]/* Written by: Arvind(slashfear)
       Language: C++     */
    
    
    #include 
    
    using namespace std;
    
    void SpiralIni(int **A, int N)
    {
      int i = 0, j = 0;
      int x0 = 0, x1 = N, y0 = 0, y1 = N;
      int m = 0;
      bool up = true;
    
      while(1)
      {
        if(up)
        {
          for (j; j < y1; ++ j) A[i][j] = ++ m;
    
          -- j; ++ i; -- y1;
    
          for (i; i < x1; ++ i) A[i][j] = ++ m;
    
          -- i; -- j; -- x1;
    
          up = false;
        } else
        {
          for (j; j >= y0; -- j) A[i][j] = ++ m;
    
          ++ j; -- i; ++ y0;
    
          for (i; i >= x0+1; -- i) A[i][j] = ++ m;
    
          ++ i; ++ j; ++ x0;
    
          up = true;
        }
    
        if (m+1 > N*N) break;
      }
    }
    
    int main()
    {
      int N;
      cout<<"Enter the value of N : ";
      cin>> N;
      int **a = new int* [N];
      for (int i = 0; i < N; ++ i) a[i] = new int [N];
    
      SpiralIni(a,N);
    
      for( int i = 0; i < N; ++ i)
      {
        for(int j=0; j < N; ++ j) cout<<"\t"<< a[i][j];
    
        cout<<"\n";
      }
    
      for (int i = 0; i < N; ++i) delete []a[i];
      delete []a;
    return 0;
    }[/B]
    


    Output:

    [B]Enter the value of N: 5
    
    1   2   3   4   5
    16  17  18  19  6
    15  24  25  20  7
    14  23  22  21  8
    13  12  11  10  9[/B]
    

    Mission accomplished ;-)

    -Arvind(slashfear)
  • RajdeepCE
    RajdeepCE
    You are really rocking slashfear. This problem was asked in coding challenge last year in our college, but I didnt go through it. Than I suddnely remember it & I posted it here. And now I found the solution thanks to you. I want to learn lots of stuff from you. You are great!!!!
  • slashfear
    slashfear
    Hi Rajdeep,


    No probs buddy....... knowledge is to share no need thanks and all dude😁






    -Arvind(slashfear);-)
  • pradeep_agrawal
    pradeep_agrawal
    That's cool, the program work perfectly as required. Slashfear, you rock buddy.

    Here is my piece of code for same problem.

    #include "stdio.h"
    
    
    void create_spiral(int **spiral, int spiral_size, int spiral_level) {
      int i = 0;
    
      //Calculating first value at level spiral_level
      int current_size = spiral_size - 2*spiral_level;
      int value = spiral_size*spiral_size - current_size*current_size + 1;
    
      //Populate data from left to right for spiral at level spiral_level
      for(i = 0; i < (spiral_size - 2*spiral_level); i++) {
        spiral[spiral_level][spiral_level+i] = value;
        value++;
      }
    
      //Populate data from top to bottom for spiral at level spiral_level
      for(i = 1; i < (spiral_size - 2*spiral_level); i++) {
        spiral[spiral_level + i][spiral_size - spiral_level - 1] = value;
        value++;
      }
    
      //Populate data from right to left for spiral at level spiral_level
      for(i = (spiral_size - 2*spiral_level -2); i >= 0; i--) {
        spiral[spiral_size - spiral_level -1][spiral_level + i] = value;
        value++;
      }
    
      //Populate data from bottom to top for spiral at level spiral_level
      for(i = (spiral_size - 2*spiral_level -2); i > 0; i--) {
        spiral[spiral_level + i][spiral_level] = value;
        value++;
      }
    }
    
    
    void print_spiral(int **spiral, int spiral_size) {
      int i = 0, j = 0;
    
      for(i = 0; i < spiral_size; i++) {
        for(j = 0; j < spiral_size; j++) {
          printf("%d ", spiral[i][j]);
        }
        printf("\n");
      }
    }
    
    
    int main() {
      int spiral_size = 0;
      int **spiral = NULL;
      int max_spiral_level = 0;
      int i = 0;
    
      //Take size of spiral as input
      scanf("%d", &spiral_size);
    
      //validate size of spiral
      if(spiral_size < 1) {
        printf("Invalid size for spiral\n");
        return 0;
      }
    
      //Allocate memory for spiral
      spiral = (int**)malloc(sizeof(int*) * spiral_size);
      for(i = 0; i < spiral_size; i++) {
        spiral[i] = (int*)malloc(sizeof(int) * spiral_size);
      }
    
      //Determine number of level of spiral
      max_spiral_level = (spiral_size + 1)/2;
    
      //For each level of spiral populate data
      for(i = 0; i < max_spiral_level; i++) {
        create_spiral(spiral, spiral_size, i);
      }
    
      //Print the spiral
      print_spiral(spiral, spiral_size);
    
      //Free memory allocated for spiral
      for(i = 0; i < spiral_size; i++) {
        free(spiral[i]);
      }
      free(spiral);
    
      return 0;
    }
    
    Compile the file with above code and run as:
    a.exe < input.txt > output.txt [on windows]
    a.out < input.txt > output.txt [on linux]

    Here,
    a.exe or a.out are the executables generated after compilation

    input.txt is the file containing order of the spiral matrix need to be given as input, e.g., if the matrix is of order 5, then input.txt will be
    5

    output.txt is the output file containing result, for given example output.txt will contain
    1 2 3 4 5
    16 17 18 19 6
    15 24 25 20 7
    14 23 22 21 8
    13 12 11 10 9

    -Pradeep

You are reading an archived discussion.

Related Posts

Dear Fellow engineer... I am looking for anybody who can help me with software, "gambit".
How long we will have to wait for a good movie? These producers and multiplex owners should resolve their issues as early as possible as there is a drought of...
Okay, so that solar-powered electroluminescent wallpaper never caught on, and neither did the EL sports jerseys that stood poised to make the most boring of events at least look exciting....
hey everybody, i got an Atmel avr STK500 development kit, it was working great but now i got a problem. whenever i turn the power switch on the power led...
I am a chemical engineering student and my final project is about a processing gas plant. It is a DDP (Dew Point Plant). In the end of the process, there...