As per your requirement i have written code which fulfill all your requirements please follow it step by step.
import java.util.*;
import java.io.*;
class SquaresMatrixClass
{
int elementN;
int magicObject[][];
SquaresMatrixClass()
{
String filename = "nums.txt";
try
{
//Here we will create Scanner object to read from file
Scanner scannerObject= new Scanner(new File(filename));
//Actually here we will read first integer from file
elementN = scannerObject.nextInt();
magicObject = new int[elementN][elementN];
for(int w=0; w<elementN; w++)
for(int q=0; q<elementN; q++)
{
magicObject[w][q]=scannerObject.nextInt();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
SquaresMatrixClass(int elementAObject[])
{
elementN= (int)Math.sqrt(elementAObject.length);
magicObject = new int[elementN][elementN];
int s=0;
for(int w=0;w<elementN;w++)
for(int q=0; q<elementN ;q++)
{
magicObject[w][q] = elementAObject[s];
s++;
}
}
int SumrightLeftDiagonal()
{
int elementSum=0;
for(int w=elementN-1;w>=0;w--)
{
elementSum=elementSum+magicObject[w][elementN-1-w];
}
return elementSum;
}
int leftRightDiagonalSum()
{
int elementSum=0;
for(int w=elementN-1;w>=0;w--)
{
elementSum=elementSum+magicObject[w][w];
}
return elementSum;
}
int Sumcolumn()
{
int elementSum=0;
for(int q=0;q<elementN;q++)
elementSum=elementSum+magicObject[0][q];
return elementSum;
}
int Sumrow()
{
int elementSum=0;
for(int q=0;q<elementN;q++)
elementSum=elementSum+magicObject[q][0];
return elementSum;
}
boolean correctnumber()
{
for(int w=0; w<elementN; w++)
for(int q=0; q<elementN;q++)
if(magicObject[w][q] < 1 || magicObject[w][q] > elementN*elementN) return false;
return true;
}
boolean validMagicSquare()
{
if(correctnumber() && Sumrow()==Sumcolumn() && Sumcolumn()== leftRightDiagonalSum() && leftRightDiagonalSum()== SumrightLeftDiagonal() ) return true;
else return false;
}
public String toString()
{
String stringContent="";
for(int w=0; w<elementN; w++)
{
for(int q=0; q<elementN; q++)
stringContent=stringContent+ magicObject[w][q]+" ";
stringContent=stringContent+"\elementN";
}
return stringContent;
}
}
public class MagicSquaresMatrix
{
public static void main(String elementAObject[])
{
SquaresMatrixClass matrixObject;
Scanner scannerObject= new Scanner(System.in);
int characterFlag=0;
while(characterFlag!=3)
{
System.out.println("1. Create Magic square from file ");
System.out.println("2. Create Magic square from input ");
System.out.println("3. Exit the program ");
characterFlag=scannerObject.nextInt();
if(characterFlag==1)
{
matrixObject= new SquaresMatrixClass();
System.out.println(matrixObject);
if(matrixObject.validMagicSquare())System.out.println("It is elementAObject magicObject Square ");
else System.out.println("It is not elementAObject magicObject Square ");
}
else if(characterFlag==2)
{
int elementN;
System.out.println("Enter size of square matrix");
elementN=scannerObject.nextInt();
int objectArray[] = new int[elementN*elementN];
System.out.println("Enter "+elementN*elementN+" elements ");
for(int w=0;w<elementN*elementN; w++)
objectArray[w]=scannerObject.nextInt();
matrixObject= new SquaresMatrixClass(objectArray);
System.out.println(matrixObject);
if(matrixObject.validMagicSquare())System.out.println("It is elementAObject magicObject Square ");
else System.out.println("It is not elementAObject magicObject Square ");
}
else if(characterFlag==3)
break;
}
}
}
The following gives the backtracking algorithm in pseudo-code for a constraint satisfaction problem, where U is...
PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...
Write an algorithm in to solve a problem of your interest and evaluate your algorithm's goodness. (Python Language) 1. Clearly state what is the problem of your interest 2. Represent your algorithm in pseudo code. Note that each step in the algorithm must be a primitive with a clear definition without ambiguity for a computer (machine or human) to execute. For example, you can assume the step to \click on a html hyperlink" a primitive in your algorithm. You can...
Need help in c++ programming to output the lines in my code: if word if found: cout << "'" << word << "' was found in the grid" << endl; cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...
In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...
i
need the solution in pseudo code please.
4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...
in c++
Purpose: This lab will give you experience
harnessing an existing backtracking algorithm for the eight queens
problem, and seeing its results displayed on your console window
(that is, the location of standard output).
Lab
A mostly complete version of the eight queens problem has been
provided for you to download. This version has the class Queens
nearly completed.
You are to provide missing logic for the class Queens that will
enable it to create a two-dimensional array that...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
I need help as quick as possible, thanks beforehand. Please
provide the test output
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...
In terms of the programming language required, the algorithm
needs to be written in pseudocode
Dynamic Programming Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be 0-red. 1-yellow, 2-blue, 3-pink. , c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4...
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) { p = new node; p->key=x; p->left=NULL; p->right=NULL; } else { //Cheak if the element to be inserted is smaller than the root. if (x < p->key) { //call the function itself with new parameters. insert(x,p->left); } //cheak if the alement to be inserted...