Note: I developed this program using the DEVC++ editor.
Could you plz go this code and let me know if u need
any changes in this.Thank You
_________________
_________________
square1.txt
3
4 9 2
3 5 7
8 1 6
___________________
square2.txt
3
2 9 4
3 5 7
8 1 6
___________________
square3.txt
5
9 2 25 18 11
3 21 19 12 10
22 20 13 6 4
16 14 7 5 23
15 8 1 24 17
__________________
// cpp Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
//Declaring constants
const int MAX_SIZE = 21;
// Function Declarations
int readSquare(int square[MAX_SIZE][MAX_SIZE],
string inputFileName);
void displaySquare(int square[MAX_SIZE][MAX_SIZE], int
size);
bool validateSquare(const int square[MAX_SIZE][MAX_SIZE], int
size);
// main function
int main() {
//Creating an 2-D array
int square[MAX_SIZE][MAX_SIZE];
//Declaring variable
string inputFileName;
//Getting the input entered by the
user
cout << "Enter the filename :";
cin >> inputFileName;
int size = readSquare(square,
inputFileName);
displaySquare(square, size);
bool b = validateSquare(square, size);
if (b) {
cout << "Valid magic square" << endl;
} else {
cout << "Invalid magic square" << endl;
}
return 0;
}
int readSquare(int square[MAX_SIZE][MAX_SIZE], string
inputFileName) {
//defines an input stream for the data file
ifstream dataIn;
int size;
//Opening the input file
dataIn.open(inputFileName.c_str());
//checking whether the file name is valid or not
if (dataIn.fail()) {
cout <<"File \""<<inputFileName<<"\" could
not be opened"<< endl;
exit(0);
} else {
dataIn >> size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
dataIn >> square[i][j];
}
}
//Closing the intput file
dataIn.close();
}
return size;
}
void displaySquare(int square[MAX_SIZE][MAX_SIZE], int size)
{
cout << "Magic Square" << endl;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << square[i][j] << "\t";
}
cout << endl;
}
}
//This function will check whether the square is
magic square or not
bool validateSquare(const int
square[MAX_SIZE][MAX_SIZE], int size) {
int n = 1;
int flag = 0;
int val = size * (size * size + 1) /
2;
/* this block of nested for loop will
check
* whether the square contains the numbers
* between the range
*/
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (square[i][j] < 1 && square[i][j] >
val)
return false;
}
}
/* this block of nested for loop will
check
* whether the square contains the unique
numbers
* between the range
*/
for (int i = 0; i < size * size; i++) {
flag = 0;
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
if (square[j][k] == n) {
flag = 1;
}
}
}
if (flag == 0) {
return false;
} else {
n++;
}
}
// Creating arrays and declaring
variables
int sumDiag[2], sumCol = 0, sumRow = 0;
// Creating array dynamically
int * rowSum = new int[size];
int * colSum = new int[size];
// Calculating the sum of each
row
for (int row = 0; row < size; row++)
{
sumRow = 0;
for (int col = 0; col < size; col++) {
sumRow += square[row][col];
rowSum[row] = sumRow;
}
}
// calculating the Sum of Each
column
for (int col = 0; col < size; col++)
{
sumCol = 0;
for (int row = 0; row < size; row++) {
sumCol += square[row][col];
colSum[col] = sumCol;
}
}
sumDiag[0] = 0;
// calculating the Sum of Diagonal0
for (int row = 0; row < size; row++) {
sumDiag[0] += square[row][row];
}
sumDiag[1] = 0;
// calculating the Sum of Diagonal 1
for (int row = 0; row < size; row++) {
sumDiag[1] += square[row][size - 1 - row];
}
/* checking whether sum of elements in each row,each
column
* and each diagonal are equal or not
* If yes, The square array is Magic Square
* if not, The square array is not Magic Square
*/
bool boolean = true;
int sum = rowSum[0];
for (int i = 1; i < size; i++) {
boolean = boolean && (sum == rowSum[i]);
}
for (int i = 0; i < size; i++) {
boolean = boolean && (sum == colSum[i]);
}
for (int i = 0; i < 2; i++) {
boolean = boolean && (sum == sumDiag[i]);
}
delete[] rowSum;
delete[] colSum;
if (boolean) {
return true;
} else {
return false;
}
}
_____________________
Output1:
______________________
Output2:
______________________
Output3:
_______________.Thank You
We were unable to transcribe this image
L' C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileMagicSquareOfMaxSize21MagicSquareOrN... Enter the filename :square1.txt Magic Square Valid magic square Process exited after 3.652 seconds with return value o Press any key to continue . . .
m! C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileMagicSquareOfMaxSize21MagicSquareOrN... Enter the filename Esquare2.txt Magic Square Invalid magic square Process exited after 4.339 seconds with return value o Press any key to continue - - - -
We were unable to transcribe this image
18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...
I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...
IN C++ Write a program that uses a 3x3 array and randomly places each integer from 1 to 9 into the nine squares. The program calculates the "magic number" by adding all the numbers in the array and then dividing the sum by 3. The 3x3 array is said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must use at least the following functions:...
Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...
need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class. Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop() Test your class thoroughly before using it within the soduku program Part II. Create...
I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...
Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...
c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...
#include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...
**IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...
Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.The function is called checkdiag (int checkdiag (int matrix[][100], int...