CrazyEngineers
  • Challenge cum problem in C/C++

    RajdeepCE

    RajdeepCE

    @rajdeepce-7UdrG8
    Updated: Oct 25, 2024
    Views: 1.1K
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Prasad Ajinkya

    MemberMay 15, 2009

    heres a hint - spiral
    Are you sure? This action cannot be undone.
    Cancel
  • RajdeepCE

    MemberMay 15, 2009

    I know it is spiral, but how to implement it in program?
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberMay 15, 2009

    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 <iostream>
    
    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)
    Are you sure? This action cannot be undone.
    Cancel
  • RajdeepCE

    MemberMay 15, 2009

    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!!!!
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberMay 15, 2009

    Hi Rajdeep,


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






    -Arvind(slashfear);-)
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberMay 15, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register