Anonymous user
Magic squares of odd order: Difference between revisions
→{{header|C++}}: removed "new" and "delete" (bad style)
(Added Wren) |
(→{{header|C++}}: removed "new" and "delete" (bad style)) |
||
Line 906:
#include <sstream>
#include <iomanip>
#include <cassert>
#include <vector>
using namespace std;
class
{
public:
{
sqr = new int[sz * sz];▼
fillSqr();
}
void display()
{
cout << "Odd Magic Square: " << sz << " x " << sz << "\n";
cout << "It's Magic Sum is: " << magicNumber() << "\n\n";
ostringstream cvr
int l = cvr.str().size();
for( int y = 0; y < sz; y++ )
{
Line 935 ⟶ 932:
for( int x = 0; x < sz; x++ )
cout << setw( l + 2 ) << sqr[yy + x];
cout << "\n";
}
cout << "\n\n";
}
private:
void fillSqr()
Line 959 ⟶ 955:
}
}
int magicNumber()
{ return sz * ( ( sz * sz ) + 1 ) / 2; }
void inc( int& a )
{ if( ++a == sz ) a = 0; }
void dec( int& a )
{ if( --a < 0 ) a = sz - 1; }
bool checkPos( int x, int y )
{ return( isInside( x ) && isInside( y ) && !sqr[sz * y + x] ); }
bool isInside( int s )
{ return ( s < sz && s > -1 ); }
vector<int
int sz;
};
int main(
{
s.display();
return 0;
Line 989 ⟶ 984:
{{out}}
<pre>
Odd Magic Square: 7 x 7
It's Magic Sum is: 175
|