Objective Implement a nonItrivial algorithm in C Problem A M
Objective: Implement a nonItrivial algorithm in C++.
Problem: A Magic Square of order N consists of N2 squares, each of which contains a number from 1 through N2 such that the rows, columns, and diagonals sum to the same thing (called the magic sum).
Input: Your program should ask the user to enter an odd integer. For the purposes of this program, treat the input as an unsigned short. Every odd number up to 999 is valid for this program. The program should continue prompting for numbers and printing magic squares until the user enters a zero. If an even number is entered, print an appropriate error message and prompt the user to enter another number.
Output: The program should output the magic squares to a text file. You don’t need to print the boxes around the numbers, but arrange the numbers in columns and rows so the square is readable using print formatting. In addition to the magic square, also print out the magic sum.
Miscellaneous: This program must use functions and arrays. It is your decision how many functions are used and how they are defined. There is an algorithm for creating magic squares; make sure you do your research before tackling this program.
Solution
//C++ code to produce magic squares
#include <iostream>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <vector>
using namespace std;
unsigned short input()
{
unsigned short n;
while(true)
{
cout << \"\ Enter an odd integer: \";
cin >> n;
if( n == 0)
break;
else if(n > 999 || n%2 == 0)
{
cout << \"Invalid Input\ \";
}
else
break;
}
return n;
}
// Driver program to test above function
int main()
{
ofstream outputFile;
outputFile.open (\"outputFile.txt\");
unsigned short n;
while(true)
{
n = input();
if(n== 0)
break;
int square_Magic[n][n];
memset(square_Magic, 0, sizeof(square_Magic));
int i = n/2;
int j = n-1;
// One by one put all values in magic square
for (int number=1; number <= n*n; )
{
if (i==-1 && j==n)
{
j = n-2;
i = 0;
}
else
{
if (j == n)
j = 0;
if (i < 0)
i=n-1;
}
if (square_Magic[i][j])
{
j -= 2;
i++;
continue;
}
else
square_Magic[i][j] = number++;
i--;
j++;
}
int Magicsum = n*(n*n+1)/2;
// output to file the magic square and the sum
outputFile << \"\ \ Magic Square n: \" << n << \"\ Magic sun: \"<<Magicsum << \"\ \ \";
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
outputFile << setw(3) << square_Magic[i][j];
outputFile << \"\ \";
}
}
outputFile.close();
return 0;
}
/*
output:
Enter an odd integer: 3
Enter an odd integer: 11
Enter an odd integer: 7
Enter an odd integer: -3
Invalid Input
Enter an odd integer: 9001
Invalid Input
Enter an odd integer: 5
Enter an odd integer: 0
outputFile.txt
Magic Square n: 3
Magic sun: 15
2 7 6
9 5 1
4 3 8
Magic Square n: 11
Magic sun: 671
54 42 30 18 6115103 91 79 67 66
41 29 17 5114102 90 78 77 65 53
28 16 4113101 89 88 76 64 52 40
15 3112100 99 87 75 63 51 39 27
2111110 98 86 74 62 50 38 26 14
121109 97 85 73 61 49 37 25 13 1
108 96 84 72 60 48 36 24 12 11120
95 83 71 59 47 35 23 22 10119107
82 70 58 46 34 33 21 9118106 94
69 57 45 44 32 20 8117105 93 81
56 55 43 31 19 7116104 92 80 68
Magic Square n: 7
Magic sun: 175
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Magic Square n: 5
Magic sun: 65
9 3 22 16 15
2 21 20 14 8
25 19 13 7 1
18 12 6 5 24
11 10 4 23 17
*/


