Question: Arrays - Java is the code being used. Exercise 1: (2 points) Display values above the average Add code to the Inequality.java program to display
Arrays - Java is the code being used.
Exercise 1: (2 points) Display values above the average
Add code to the Inequality.java program to display all numbers above the average. Paste your program into your solutions document. No output is required
Exercise 2: (2 points) Display total # of values above the average
Modify your program from above to also compute the total number of values above the average. Display the total in a new line after display each of the individual values above the average. Copy your program and the output from the Console into your solutions document.
Exercise3: (2 points) Magic Columns!
A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an nxn matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum must be n(n^2+1)/2. Magic squares have been around since 650 BC (China) ! For those who are interested, this Wikipedia article provides more background. You should add code to Magic.java to check that each column equals the n(n^2+1)/2. Currently, the Magic.java program does an incomplete job of determining all the necessary checks for satisfying the conditions of a Magic Square. In particular, it does not check that the sum of each column equals n(n^2+1)/2 You may find it useful to look at how the row sums were computed. Copy only the code that you added into your solutions document.
Exercise 4: (2 points) Magic Diagonals!
You should add code to Magic.java to check that the sum of numbers along the diagonal from upper left to bottom right equals the n(n^2+1)/2. Currently, the Magic.java program does an incomplete job of determining all the necessary checks for satisfying the conditions of a Magic Square. In particular, it does not check that this diagonal equals n(n^2+1)/2 You may find it useful to look at how the sum of the diagonal from upper right to lower left was computed. Copy only the code that you added the into your solutions document.
Exercise 5: (2 points) New Magic
Is the follow two dimensional array Magic ? Use your program to test.
| 1 | 15 | 14 | 4 |
| 12 | 6 | 7 | 9 |
| 8 | 10 | 11 | 5 |
| 13 | 3 | 2 | 16 |
What about this one ? Again, use your program
| 1 | 15 | 14 | 4 |
| 12 | 7 | 6 | 9 |
| 10 | 8 | 11 | 5 |
| 13 | 3 | 2 | 16 |
Here is the code being used which has to be inserted.
The first one Inequality
public class Inequality { public static void main(String[] args) { // list of incomes in thousands int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21}; int sum = 0; int average; for (int i = 0;i< income.length;i++) { sum = sum + income[i]; } average = sum/income.length; System.out.println(" Average of income array : " + average); }
}
The second one, Magic
import java.util.Arrays;
public class Magic { public static void main(String []args) { int[][] a ={{4,9,2}, {3,5,7}, {8,1,6}}; final int n=a.length; final int nSquare=n*n; final int targetSum=n*(n*n+1)/2; boolean[] flag= new boolean[n*n]; int row, col, sum;
// Calculate the sum of each row ... if magic, then equal to targetSum for(row=0; row
// TODO : Add a check for the sums of columns
// Calculate the sum of the diagonal from Lower Left to Upper Right sum=0; System.out.print("diagonal: "); for(int pos=0; pos
// TODO : Add check for the sum of primary diagonal (from upper left to bottom right)
// Lastly, we check that every number from 1 to n is represented for(row=0; row
System.out.println(" The following two dimensional array is Magic !"); for (int i = 0;i< a.length;i++) { System.out.println(Arrays.toString(a[i])); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
