Home Forums C Programming program to make spiral matrix Re: Re: program to make spiral matrix

#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