Home Forums C Programming program to make spiral matrix

Viewing 3 reply threads
  • Author
    Posts
    • #2152
      KerriReeks
      Participant

      hi !!!
      i want to make a program for spiral matrix………i have exactly no idea how to code..
      spiral matrix is like this::if input is 4

      1 2 3 4
      12 13 14 5
      11 16 15 6
      10 9 8 7

    • #3473
      GWILouisaxwzkla
      Participant

      Can you give a definition of spiral matrix?

    • #3474
      KerriReeks
      Participant

      firstly tell me u r that cman?

      ya spiral matrix is like as i have given inthe question.in it i f the input is 4,then it will print the numers in 4 row and 4 column.just like a coil structure.

      it will print 1 2 3 4,then it will go to 4th column and print 5
      6
      7
      and so on…….in my previous post u can c that.although it seems not clear in it.
      try to understand that matrix by observing the figure u can understand

    • #3475
      GWILouisaxwzkla
      Participant

      I think this does what you want it to:



      /****************************************************************
      * File Name : c:programshelptemp.cpp
      * Date :
      * Comments : new project
      * Compiler/Assembler :
      *
      *
      *
      *
      *
      * Program Shell Generated At: 3:59:55 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include < iostream.h >
      #include < ctype.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >

      //using namespace std;


      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@



      //##################################################################################


      //main function ******************************

      int main ( )
      {


      int sizeOfMatrix;

      cout << "enter the size of the matrix: ";
      cin >> sizeOfMatrix;

      //allocate appropriate matrix:

      int ** matrix = new int * [ sizeOfMatrix ];
      if ( matrix == NULL )
      {
      cout << "Allocation Error!! ";
      return 1;
      }

      int i = 0;
      while ( i < sizeOfMatrix )
      {
      matrix [ i ] = new int [ sizeOfMatrix ];
      if ( matrix [ i ] == NULL )
      {

      while ( i >= 0 )
      delete [] matrix [ i -- ]; //clean up memory
      return 0;
      }
      i ++;
      }
      //write matrix
      i = 1;
      int j = 0;
      int input = 1;

      while ( j < sizeOfMatrix )
      {
      i = 0;

      while ( i < sizeOfMatrix )
      {

      matrix [ i ] [ j ] = input;
      i ++;
      input ++;
      }
      j ++;
      }

      //output matrix

      i = 0;
      j = 0;

      cout << endl;
      while ( i < sizeOfMatrix )
      {
      j = 0;

      while ( j < sizeOfMatrix )
      {
      cout << matrix [ i ] [ j ];
      //should use iomanip here!!!!!!!!
      if ( matrix [ i ] [ j ] < 10 )
      cout << " ";
      else
      cout << " ";
      j ++;
      }
      cout << endl;
      i ++;
      }

      //destroy matrix

      i = 0;
      while ( i < sizeOfMatrix )
      delete [] matrix [ i ++ ];


      }

      heres the ouput:

      enter the size of the matrix: 5

      1 6 11 16 21
      2 7 12 17 22
      3 8 13 18 23
      4 9 14 19 24
      5 10 15 20 25
      Press any key to continue

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.