Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

Magic Square:

a. Write a method "createArithmeticSeq" that prompts the user toinput two numbers, "first" and "diff". The method thencreates a one-dimensional array of 16 elements ordered in anarithmetic sequence. It also outputs the arithmeticsequence. For example, if "first = 21" and "diff = 5", thearithmetic sequence is: 21 26 31 36 41 46 51 56 61 66 71 7681 86 91 96

b. Write a method "matricize" that takes a one-dimensional array of16 elements and a two-dimensional array of 4 rows and 4 columns asparameters. This method puts the elements of theone-dimensional array into the two-dimensional array. Forexample, if "A" is the one-dimensional array created in part(a) and"B" is a two dimensional array, then after putting the elements of"A" into "B", the array "B" is:

21 26 31 36
41 46 51 56
61 66 71 76
81 86 91 96

c. Write a method "reverseDiagonal" that reverses both diagonals ofa two-dimensional array. For example, if the two dimensionalarray is as shown in part(b), after reversing the diagonals, thetwo-dimensional array is:

96 26 31 81
41 71 66 56
61 51 46 76
36 86 91 21

d. Write a method "magicCheck" that takes a one-dimensional arrayof size 16, a two dimensional array of 4 rows and 4 columns, andthe sizes of the arrays as parameters. By adding all of theelements of the one-dimensional array and dividing by 4, thismethod determines the "magicNumber". The method then addseach row, each column, and each diagonal of the two-dimensionalarray and compares each sum with the magic number. If the sumof each row, each column, and each diagonal is equal to the"magicNumber", the method outputs "It is a magic square";otherwise, it outputs "It is not a magic square". Do notprint the sum of each row, each column, and the diagonals.

e. Write a method "printMatrix" that ouputs the elements of atwo-dimensional array, one row per line. This output shouldbe as close to a square form as possible.

f. The methods written in parts (a) through (e) should be generalenough to apply to an array of any size. (The work should be doneby using loops.) The method "magicCheck" works better if itcalls two seperate methods: one to add the rows and columns, andone to add the diagonals. (In this case, the program comparesthe magic number with each sum [row, column, and diagonal] in therespective method.)

Test the methods you wrote in parts (a) through (f) using thefollowing class and the method main:

public class Ch9_PrExercise13
{
static final int rows = 4;
static final int columns = 4;

static final int listSize = 16;
...
public static void main (String[] args)
{
int[] list = new int[listSize];
int [][] matrix = new int[rows][columns];

createArithmeticSeq(list);
matricize(list, matrix);
printMatrix(matrix);
reverseDiagonal(matrix);
printMatrix(matrix);
magicCheck(list, matrix);
}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9649560

Have any Question?


Related Questions in Computer Engineering

What are some topics that must be covered in a business

What are some topics that must be covered in a business case presented to management?

You are given a test to enter graduate school you must

You are given a test to enter graduate school. You must select 10 of the 13 essay questions to answer to determine your writing skills. How many ways can you select those questions?

Display the manager of the employee with the oldest project

Display the manager of the employee with the oldest project start date (start_date). (This query requires 3 nested queries, start by finding the min start_date from project, then find the emp_id from project where start_ ...

Question suppose you have three non-empty stacks r s and t

Question : Suppose you have three non-empty stacks R, S, and T. Describe a sequence of Stack operations that results in S storing all elements originally in T below all of S's original element with both sets of those ele ...

Question research the options for creating a gui on a sun

Question : Research the options for creating a GUI on a Sun Solaris platform that can be duplicated on a Windows platform. Select a tool for creating a dual platform product and specify how much time would be needed for ...

Question suppose there are 150 users sharing a

Question : Suppose there are 150 users sharing a communication link. Suppose each user transmits data only 12% of the time. Give the (compact) formula for finding the probability that at most 20 users are transmitting si ...

Given an undirected graph with both positive and negative

Given an undirected graph with both positive and negative edge weights, design an algorithm to find a maximum spanning forest with the largest total edge weights.

Question reversing the elements of an array involves

Question : Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. G ...

Java program that does the following please use simple

Java program that does the following: Please use simple computer syntax. More easy to understand! Use a while loop to prompt the user to enter 10 rain falls. Calculate and output the total and the average rain fall. Find ...

Please explain to me the concept of policing with emphasis

Please explain to me the concept of policing with emphasis on the "leaky bucket" mechanism

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As