# COSC1046-Assignment_5

NOTE: You must include commenting in all of these assignments. The code must be neatly formatted  marks will be deducted.
Part A: This question is to be submitted to the instructor in the form of a Word (or Open Office) document containing the Java code and appropriate screen capture(s) of the output. The file name must be in the form ASSIGN5A_YourName where YourName is your last name followed by your first name with no space. Upload the file to CMS.
Problem 6.9, page 256
Version 1: Solve the problem as written in the textbook. Demonstrate that it works using 3 different arrays (1 screen shot for each).
Problem 6.7, page 256
You may find the ternary operator useful when you need to add the s to the word times. Example of pluralizing question would be S.O.P(question + (numQuestions>1?s:);
Run the program three times  providing screenshots for each run.
Problem 7.5, page 282
Test using the matrix in the chapter and another matrix of different size with double values. (A minimum of 2 screen captures required.)
Part B: Write code and test the solutions for the following problems from the textbook. Submit the .java files for each question to CMS.

Problem 6.5, page 261
Problem 6.27, page 261
Include the method in a test application which uses the method.
Problem 7.13, page 285
Problem 7.25, page 290
Be prepared to answer questions about how the program works. If you cant explain how it works, your grade will be impacted.

6.9 (Find the smallest element) Write a method that finds the smallest element in an

array of double values using the following header:

public static double min(double[] array)

Write a test program that prompts the user to enter ten numbers, invokes this

method to return the minimum value, and displays the minimum value. Here is a

sample run of the program:

2n

Enter ten numbers:

The minimum number is: 1.5

6.7 (Count single digits) Write a program that generates 100 random integers between 0

and 9 and displays the count for each number. (Hint: Use (int)(Math.random()

* 10) to generate a random integer between 0 and 9. Use an array of ten integers,

say counts, to store the counts for the number of 0s, 1s, …, 9s.)

7.5 (Algebra: add two matrices) Write a method to add two matrices. The header of

the method is as follows:

public static double[][] addMatrix(double[][] a, double[][] b)

In order to be added, the two matrices must have the same dimensions and the

same or compatible types of elements. Let c be the resulting matrix. Each element

cij aij + bij For example, for two 3 * 3 matrices a and b, c is

Part b

6.5

6.27 (Identical arrays) The arrays list1 and list2 are identical if they have the same

contents. Write a method that returns true if list1 and list2 are identical,

using the following header:

public static boolean equals(int[] list1, int[] list2)

Write a test program that prompts the user to enter two lists of integers and displays

whether the two are identical. Here are the sample runs. Note that the first

number in the input indicates the number of the elements in the list

7.13 (Locate the largest element) Write the following method that returns the location

of the largest element in a two-dimensional array.

public static int[] locateLargest(double[][] a)

The return value is a one-dimensional array that contains two elements. These

two elements indicate the row and column indices of the largest element in the

two-dimensional array. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the

array. Here is a sample run:

7.25 (Markov matrix) An matrix is called a positive Markov matrix if each element

is positive and the sum of the elements in each column is 1. Write the following

method to check whether a matrix is a Markov matrix.

public static boolean isMarkovMatrix(double[][] m)

Write a test program that prompts the user to enter a matrix of double values

and tests whether it is a Markov matrix. Here are sample runs: