Home › Forums › C Programming › program to make spiral matrix › Re: Re: program to make spiral matrix
November 14, 2008 at 7:48 pm
#3475
GWILouisaxwzkla
Participant
I think this does what you want it to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | <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