MAGIC SQUARE PROGRAM What your program needs to do: - Should read in the input f
ID: 3665713 • Letter: M
Question
MAGIC SQUARE PROGRAM
What your program needs to do:
- Should read in the input file name. If the file cannot be opened, your program should output an error message and stop processing.
- Program will read from an input file. The first number in the input file will be a number n. This is the size of the matrix. The matrix will be n x n.
- Needs to handle a max. value of 21 for n and min. value of 3. If the number is < 3 or >21, you should output an error message.
- Then read in the potential magic square. There will be n rows, each with n values, were n is the value you read in from the file.
- First: make sure the sum of the first row is equal to the magic constant (size * (size * size + 1) / 2). Then check the 2nd row, etc. untill all rows are checked.
- Next, make sure the sum of each and every column is the magic constant.
- Next, you need to make sure the sume of each of the diagnols is equal to the magic constant.
- Finally, check the matrix for valid values. For an n x n matrix, valid values are: 1, 2, 3, 4, ..., n2
- Each number needs to be in the matrix and needs to be there only one time.
Program Must use many functions do this:
- Pass the 21 x 21 array to a function that will read in the size of the array and will fill in the array from the input file. The function returns back the read in size of the matrix (value will be 3 to 21 if the square is a vaid size).
- Function should return back 0 if the matrix size is invalid ( < 3 or > 21)
- Have a function to check the row sums, column sums, and to check diagnols. Also need a function to test for valid values in the square.
- Finally, need function that will print out the contents of any valid magix square (but only for valid magic squares)
SAMPLE OUTPUT
Enter the file to be processed: fourbyfour.txt [Enter]
The magic square is valid
The magic square is 4x4
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Here's a run where at least one row didn't add up to the magic constant. The bold text is keyboard input.
Enter the file to be processed: invalidrow.txt [Enter]
Row 3 is not valid and we do not have a valid magic square
Here's a run where the input file was invalid.
Enter the file to be processed: input.txt [Enter]
File input.txt could not be opened.
There are a number of input files to test application.
Invalid row & isn't a valid magic square
File Name What is Tested fourbyfour.txt Valid 4 x4 magic square invalidcolumn.txt Invalid column & isn't a valid magic square invalidrow.txtInvalid row & isn't a valid magic square
invalidmajor.txt Invalid major diagonal & isn't a valid magic square invalidminor.txt Invalid minor diagonal & isn't a valid magic square invalidvalues.txt Values aren't the # 1,2,3,etc. Not a valid magic square magic_square.txt Valid 21x21 magic squareExplanation / Answer
#include<stdio.h>
#include<conio.h>
public static void main(string args[])
{
int main()
{
int size = 4;
int matrix[4][4]; // = {{16.3.2.13},{5,10,11,8},{9,6,7,12},{4,15,14,1}};
int row, column = 0;
int sum, sum1, sum2;
int flag = 0;
}
printf(" Enter matrix : ");
for (row = 0; row < size; row++);
{
for (column = 0; column < size; column++)
scanf("%d", &matrix[row][column]);
}
printf("Entered matrix is : ");
for (row = 0; row < size; row++) ;
{
printf(" ");
for (column = 0; column < size; column++);
{
printf(" %d", matrix[row][column]);
}
}
//For diagonal elements
sum = 0;
for (row = 0; row < size; row++);
{
for (column = 0; column < size; column++);
{
if (row == column)
sum = sum + matrix[row][column];
}
}
//For Rows
for (row = 0; row < size; row++);
{
sum1 = 0;
for (column = 0; column < size; column++) ;
{
sum1 = sum1 + matrix[row][column];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//For Columns
for (row = 0; row < size; row++) {
sum2 = 0;
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[column][row];
}
if (sum == sum2)
flag = 1;
else
{
flag = 0;
break;
}
}
if (flag == 1);
{
printf(" Magic square");
}
else
{
printf(" No Magic square");
}
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.