Home › Forums › C Programming › program to make spiral matrix
- This topic has 3 replies, 2 voices, and was last updated 16 years ago by GWILouisaxwzkla.
- AuthorPosts
- November 10, 2008 at 5:03 am #2152KerriReeksParticipant
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 41 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7 - November 10, 2008 at 9:38 pm #3473GWILouisaxwzklaParticipant
Can you give a definition of spiral matrix?
- November 14, 2008 at 5:58 am #3474KerriReeksParticipant
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 - November 14, 2008 at 7:48 pm #3475GWILouisaxwzklaParticipant
I think this does what you want it to:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118<br /><br />/****************************************************************<br />* File Name : c:programshelptemp.cpp<br />* Date :<br />* Comments : new project<br />* Compiler/Assembler :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:59:55 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream.h ><br />#include < ctype.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />//using namespace std;<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br /><br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br /><br />int sizeOfMatrix;<br /><br />cout << "enter the size of the matrix: ";<br />cin >> sizeOfMatrix;<br /><br />//allocate appropriate matrix:<br /><br />int ** matrix = new int * [ sizeOfMatrix ];<br />if ( matrix == NULL )<br />{<br />cout << "Allocation Error!! ";<br />return 1;<br />}<br /><br />int i = 0;<br />while ( i < sizeOfMatrix )<br />{<br />matrix [ i ] = new int [ sizeOfMatrix ];<br />if ( matrix [ i ] == NULL )<br />{<br /><br />while ( i >= 0 )<br />delete [] matrix [ i -- ]; //clean up memory<br />return 0;<br />}<br />i ++;<br />}<br />//write matrix<br />i = 1;<br />int j = 0;<br />int input = 1;<br /><br />while ( j < sizeOfMatrix )<br />{<br />i = 0;<br /><br />while ( i < sizeOfMatrix )<br />{<br /><br />matrix [ i ] [ j ] = input;<br />i ++;<br />input ++;<br />}<br />j ++;<br />}<br /><br />//output matrix<br /><br />i = 0;<br />j = 0;<br /><br />cout << endl;<br />while ( i < sizeOfMatrix )<br />{<br />j = 0;<br /><br />while ( j < sizeOfMatrix )<br />{<br />cout << matrix [ i ] [ j ];<br />//should use iomanip here!!!!!!!!<br />if ( matrix [ i ] [ j ] < 10 )<br />cout << " ";<br />else<br />cout << " ";<br />j ++;<br />}<br />cout << endl;<br />i ++;<br />}<br /><br />//destroy matrix<br /><br />i = 0;<br />while ( i < sizeOfMatrix )<br />delete [] matrix [ i ++ ];<br /><br /><br />}<br />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
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.