I badly need help for my assignment which i due tomorrow. Please help me out!
So the task is to create a magic square wherein there is actually a box so each number would be in a cell. And I got mine working already but my problem is once the user enters any two-digit number above, the box kinda gets messy so the lines are no longer in place and the box isn't a box anymore. What is the correct algorithm???
while (n%2 == 0 || n == 1){
cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
cout << "Enter an odd number for the size of the Magic Square: ";
cin >> n;
if (n%2 == 0 || n == 1){
cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
}
}
clrscr();
//create an array and place 0s in it
int magicSquare[n][n];
for (int i = 0; i < n; i++){
for (int z = 0; z < n; z++){
magicSquare[i][z] = 0;
}
}
//determine first position
int i = 0;
int z = (n - 1)/2;
//first position
magicSquare[i][z] = 1;
int key = 2;
int b, c;
//determining the positions ( from 2 to n*n )
while(key <= n * n ){
//move up and left
b = (i - 1) % n;
c = (z - 1) % n;
// if row is out of bounds
if( b < 0 && c >= 0 ){
i = n - 1;
z = (z - 1)%n;
}
// if col is out of bounds
else if( c < 0 && b >= 0){
z = n - 1;
i = (i - 1) % n;
}
else if( b < 0 && c < 0 ){
i = (i + 1)%n;
}
// if space is occupied, move down
else if(magicSquare[b][c] != 0){
i = (i + 1)%n;
}
//assign b,c to i,z
else {
i = b;
z = c;
//magicSquare[i][z] = key;
}
//put the value in
magicSquare[i][z] = key;
key++;
}
//int common;
box(n);
for (int x = 0; x < n; x++){
for (int y = 0; y < n; y++){
if (n*n < 10){
gotoxy (y*2+1, x * 2 + 1); cout<< magicSquare[x][y];
}else if (n*n<100){
gotoxy (y*3+1,x*2 + 1);cout <<magicSquare [x][y];
}else{
gotoxy (y*4+1,x*2+1); cout <<magicSquare [x][y];
}
}
// common == magicSquare [0][x];
}
// gotoxy (0,n*2+2);cout<< "where the common sum is "<< common << endl;
cout << "\n\nSum of each vertical line: "<< setw(5) << n*(n*n+1)/2 << endl;
cout << "Sum of each horizontal line: " << setw(5) << n*(n*n+1)/2 << endl;
cout << "Sum of each diagonal line: " << setw(3) << n*(n*n+1)/2 <<endl;
/* for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << "|" << setw(5)<<magicSquare[i][j]<<" ";
}
cout<<endl;
} */
/* cout << "Solve for another? Y or N?";
char a;
cin >> a;
if (a == 'y','Y'){
return main();
}
else { */
cout << "\n" <<endl;
return main();
}
void box (int n) {
if (n*n < 10) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n*2){
cout << '\xB4';
}
}
if (i == n*2){
if (z == 0) {
cout << '\xC0';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n*2){
cout << '\xD9';
}
}
}
cout << endl;
}
} else if (n*n < 100) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n*2){
cout << '\xB4';
}
}
if (i == n*2){
if (z == 0) {
cout << '\xC0';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n*2){
cout << '\xD9';
}
}
}
cout << endl;
}
} else {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z !=0){
#include <iostream>
#include <iomanip>
#include <windows.h>
#include "ecpe202.h"
void box(int n);
usingnamespace std;
int main()
{
//ask user for size
int n, x, y;
while (n % 2 == 0 || n == 1) {
cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
cout << "Enter an odd number for the size of the Magic Square: ";
cin >> n;
if (n % 2 == 0 || n == 1) {
cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
}
}
clrscr();
//create an array and place 0s in it
int magicSquare[n][n];
for (int i = 0; i < n; i++) {
for (int z = 0; z < n; z++) {
magicSquare[i][z] = 0;
}
}
//determine first position
int i = 0;
int z = (n - 1) / 2;
//first position
magicSquare[i][z] = 1;
int key = 2;
int b, c;
//determining the positions ( from 2 to n*n )
while (key <= n * n) {
//move up and left
b = (i - 1) % n;
c = (z - 1) % n;
// if row is out of bounds
if (b < 0 && c >= 0) {
i = n - 1;
z = (z - 1) % n;
}
// if col is out of bounds
elseif (c < 0 && b >= 0) {
z = n - 1;
i = (i - 1) % n;
}
elseif (b < 0 && c < 0) {
i = (i + 1) % n;
}
// if space is occupied, move down
elseif (magicSquare[b][c] != 0) {
i = (i + 1) % n;
}
//assign b,c to i,z
else {
i = b;
z = c;
//magicSquare[i][z] = key;
}
//put the value in
magicSquare[i][z] = key;
key++;
}
//int common;
box(n);
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (n * n < 10) {
gotoxy(y * 2 + 1, x * 2 + 1);
cout << magicSquare[x][y];
} elseif (n * n < 100) {
gotoxy(y * 3 + 1, x * 2 + 1);
cout << magicSquare[x][y];
} else {
gotoxy(y * 4 + 1, x * 2 + 1);
cout << magicSquare[x][y];
}
}
// common == magicSquare [0][x];
}
// gotoxy (0,n*2+2);cout<< "where the common sum is "<< common << endl;
cout << "\n\nSum of each vertical line: " << setw(5) << n * (n * n + 1) / 2 << endl;
cout << "Sum of each horizontal line: " << setw(5) << n * (n * n + 1) / 2 << endl;
cout << "Sum of each diagonal line: " << setw(3) << n * (n * n + 1) / 2 << endl;
/* for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << "|" << setw(5)<<magicSquare[i][j]<<" ";
}
cout<<endl;
} */
/* cout << "Solve for another? Y or N?";
char a;
cin >> a;
if (a == 'y','Y'){
return main();
}
else { */
cout << "\n" << endl;
return main();
}
void box(int n)
{
if (n * n < 10) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n * 2) {
cout << '\xBF';
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << '\xB3';
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << '\xC3';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n * 2) {
cout << '\xB4';
}
}
if (i == n * 2) {
if (z == 0) {
cout << '\xC0';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n * 2) {
cout << '\xD9';
}
}
}
cout << endl;
}
} elseif (n * n < 100) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n * 2) {
cout << '\xBF';
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << '\xB3';
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << '\xC3';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n * 2) {
cout << '\xB4';
}
}
if (i == n * 2) {
if (z == 0) {
cout << '\xC0';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n * 2) {
cout << '\xD9';
}
}
}
cout << endl;
}
} else {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n * 2) {
cout << '\xBF';
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << '\xB3';
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << '\xC3';
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
}
}
}
}
}
///////
#include <windows.h>
#define FOREVER true
#define MAXSTACK 100
#define MAXQUEUE 100
usingnamespace std;
struct myStack {
int tos;
char s[MAXSTACK];
};
struct myQueue {
int head;
int tail;
int q[MAXQUEUE];
};
void createS(myStack & S)
{
S.tos = -1;
}
void pushS(myStack & S, char item)
{
S.s[++S.tos] = item;
}
char popS(myStack & S)
{
return (S.s[S.tos--]);
}
bool isFullS(myStack S)
{
if (S.tos == MAXSTACK - 1)
return (true);
returnfalse;
}
bool isEmptyS(myStack S)
{
if (S.tos == -1)
returntrue;
returnfalse;
}
//end of stack
void createQ(myQueue & Q)
{
Q.head = 0;
Q.tail = 0;
}
void addQ(myQueue & Q, int item)
{
Q.q[Q.tail++] = item;
Q.tail %= MAXQUEUE;
}
int retrieveQ(myQueue & Q)
{
int temp;
temp = Q.q[Q.head++];
Q.head %= MAXQUEUE;
return (temp);
}
bool isFullQ(myQueue Q)
{
if (Q.tail == MAXQUEUE)
return (true);
returnfalse;
}
bool isEmptyQ(myQueue Q)
{
if (Q.tail == Q.head)
return (true);
returnfalse;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void clrscr()
{
system("CLS");
}
> What is the correct algorithm???
You work out the length of the largest integer you're going to print, then arrange for each cell of your square to have that size.
Any number smaller than that, you pad with leading spaces.
So if your max is say 144, then 3 would be " 3" (or if you're fancy " 3 "), and 42 would be " 42".
> I badly need help for my assignment which i due tomorrow. Please help me out!
Well the short-term answer is save what you have so that you have at least something to submit, even if it's not ideal.
All non-trivial programs begin with pencil and paper.
Draw a few cells for single digit numbers.
Draw a few cells for two digit numbers.
Draw a few cells for three digit numbers.
Study the relationship between number length and the shape of your cells, and what it takes to draw each cell.
Create prototype programs which you can use to study whether your ideas would work in practice.
What you DON'T want to do is write 100's of lines of code, and then discover the night before that it doesn't work!!!
> How do I customize the size of the cells of my square?
I thought that was what the 'n' parameter to box() was all about.
It gets a LOT easier to spot what's missing when you have symbolic constants to represent all the different box elements. You can't see what's missing in a blizzard of hex.
#include <iostream>
#include <iomanip>
#include <sstream>
void box(int n);
usingnamespace std;
int main()
{
//ask user for size
int n, x, y;
// Yes,your while loop suffered from using uninitialised n
do {
cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
cout << "Enter an odd number for the size of the Magic Square: ";
cin >> n;
if (n % 2 == 0 || n == 1) {
cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
}
}
while (n % 2 == 0 || n == 1);
box(n);
return 0;
}
#ifdef WINDOWS
#else
// https://en.wikipedia.org/wiki/Box-drawing_character#Unix,_CP/M,_BBS
#define BOX_TL 0x6C
#define BOX_HOR 0x71
#define BOX_VTEE 0x77
#define BOX_TR 0x6B
#define BOX_VER 0x78
#define BOX_LTEE 0x74
#define BOX_RTEE 0x75
#define BOX_BTEE 0x76
#define BOX_CROSS 0x6E
#define BOX_BL 0x6D
#define BOX_BR 0x6A
std::string boxchar(unsignedchar b) {
std::ostringstream os;
os << "\x1b(0" << b << "\x1b(B";
return os.str();
}
#endif
void box(int n)
{
if (n * n < 10) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << boxchar(BOX_TL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_VTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_TR);
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << boxchar(BOX_VER);
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << boxchar(BOX_LTEE);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_CROSS);
}
if (z == n * 2) {
cout << boxchar(BOX_RTEE);
}
}
if (i == n * 2) {
if (z == 0) {
cout << boxchar(BOX_BL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_BTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_BR);
}
}
}
cout << endl;
}
} elseif (n * n < 100) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << boxchar(BOX_TL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_VTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_TR);
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << boxchar(BOX_VER);
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << boxchar(BOX_LTEE);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_CROSS);
}
if (z == n * 2) {
cout << boxchar(BOX_RTEE);
}
}
if (i == n * 2) {
if (z == 0) {
cout << boxchar(BOX_BL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_BTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_BR);
}
}
}
cout << endl;
}
} else {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << boxchar(BOX_TL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_VTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_TR);
}
}
if (i % 2 == 1 && i != n * 2 + 1) {
if (z % 2 == 1) {
cout << " ";
} else {
cout << boxchar(BOX_VER);
}
}
if (i % 2 == 0 && i != 0 && i != n * 2) {
if (z == 0) {
cout << boxchar(BOX_LTEE);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_CROSS);
}
if (z == n * 2) { //!! This was missing
cout << boxchar(BOX_RTEE);
}
}
if (i == n * 2) { //!! All this was missing
if (z == 0) {
cout << boxchar(BOX_BL);
}
if (z % 2 == 1 && z != n * 2 + 1) {
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
}
if (z % 2 == 0 && z != n * 2 && z != 0) {
cout << boxchar(BOX_BTEE);
}
if (z == n * 2) {
cout << boxchar(BOX_BR);
}
}
}
cout << endl; //!! also missing
}
}
}
What you should really be thinking about is that the whole interior of the square is common code comprised of nothing more than
You could even pre-create the common elements which depend on the value of n.
Then you wouldn't need the if (n * n < 10) if (n * n < 100) if (n * n < 1000) bloat generator.
1 2 3 4 5 6 7 8
int len = (int)log10(n*n) + 1;
std::ostringstream l1, l2;
for ( int i = 0 ; i < len ; i++ ) {
l1 << " ";
l2 << boxchar(BOX_HOR);
}
l1 << boxchar(BOX_VER);
l2 << boxchar(BOX_CROSS);
void box(int n) {
int len = (int)log10(n*n)+1;
// Prepare the oft repeated elements
std::ostringstream hdash, hspace;
for ( int i = 0 ; i < len ; i++ ) {
hdash << boxchar(BOX_HOR);
hspace << " ";
}
// Top Row
cout << boxchar(BOX_TL);
for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_VTEE);
cout << hdash.str() << boxchar(BOX_TR);
cout << endl;
// All the cells, except the last row of cells
for ( int r = 0 ; r < n-1 ; r++ ) {
// spaces and vertical lines
cout << boxchar(BOX_VER);
for ( int c = 0 ; c < n ; c++ ) cout << hspace.str() << boxchar(BOX_VER);
cout << endl;
// tee pieces, horizontal lines and crosses
cout << boxchar(BOX_LTEE);
for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_CROSS);
cout << hdash.str() << boxchar(BOX_RTEE);
cout << endl;
}
// Last row of spaces and vertical lines
cout << boxchar(BOX_VER);
for ( int c = 0 ; c < n ; c++ ) cout << hspace.str() << boxchar(BOX_VER);
cout << endl;
// bottom row
cout << boxchar(BOX_BL);
for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_BTEE);
cout << hdash.str() << boxchar(BOX_BR);
cout << endl;
}