public static double [] copyArray (float [][] m);
Test this method in main().
public static double [][] generateIdentityMatrix (int n);
Test this method in main().
the array with numbers from 1 to m × n in the way as below: first row, initialize the elements from left to right; next row, initialize from right to left; then switch order. So if m=5; and n = 4; the array should be initialized to:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
17 18 19 20
Display returned array in main().
public static double [][] addMatrices(double [][] m1, double [][] m2);
m1 and m2 have the same size. Test method in main().
A is a matrix, and x is vector. The method is declared as below:
public static double [] productOfMatrixAndVector (double [][] A, double [] x);
Test method in main().
// ArrayOperations.java
import java.util.Arrays;
public class ArrayOperations {
// method to find the maximum value in this two dimensional array;
public double maximum(double arr[][])
{
double max = arr[0][0];
for(int i=0;i<arr.length;i++)
for(int j=0;j<arr[i].length;j++)
if(arr[i][j] > max)
max = arr[i][j];
return max;
}
// method to find the average value in the array;
public double average(double arr[][])
{
double total = 0;
for(int i=0;i<arr.length;i++)
for(int j=0;j<arr[i].length;j++)
total += arr[i][j];
return(total/(arr.length*arr[0].length));
}
// method to count how many elements are smaller than average;
public int smallerThanAverage(double arr[][])
{
double avg = average(arr);
int count = 0;
for(int i=0;i<arr.length;i++)
for(int j=0;j<arr[0].length;j++)
if(arr[i][j] < avg)
count++;
return count;
}
// method to copy contents in a two-dimensional array to a one-dimensional array.
public static double [] copyArray (float [][] m)
{
double singM[] = new double[0];
for(int i=0;i<m.length;i++)
for(int j=0;j<m[i].length;j++)
{
singM = Arrays.copyOf(singM, singM.length+1);
singM[singM.length-1] = m[i][j];
}
return singM;
}
// method to generate an identity matrix of size n
public static double [][] generateIdentityMatrix (int n)
{
double identity[][] = new double[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i==j)
identity[i][j] = 1;
else
identity[i][j] = 0;
return identity;
}
// method to get the sum of the major diagonal of the matrix
public double sumMajorDiagonal(float m[][])
{
double total = 0;
for(int i=0;i<m.length;i++)
for(int j=0;j<m[0].length;j++)
if(i == j)
total += m[i][j];
return total;
}
// method to generate an array given its rows and columns
public int[][] generateArray(int m, int n)
{
int arr[][] = new int[m][n];
int count=1;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
arr[i][j] = count;
count++;
}
return arr;
}
// method to add 2 matrices and return its resultant array
public static double [][] addMatrices(double [][] m1, double [][] m2)
{
if((m1.length == m2.length ) && (m1[0].length == m2[0].length))
{
double result[][] = new double[m1.length][m1[0].length];
for(int i=0;i<result.length;i++)
{
for(int j=0;j<result[i].length;j++)
result[i][j] = m1[i][j] + m2[i][j];
}
return result;
}
return null;
}
// method to multiply a matrix and a vector and return its resultant vector
public static double [] productOfMatrixAndVector (double [][] A, double [] x)
{
if(A[0].length == x.length)
{
double result[] = new double[A.length];
for(int i=0;i<A.length;i++)
{
result[i] = 0;
for(int k=0;k<x.length;k++)
result[i] += A[i][k]*x[k];
}
return result;
}
return null;
}
}
//end of ArrayOperations.java
Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...
(10 pts) Define a two-dimensional array named temp with three rows and four columns of type int such that the first row is initialized to 6, 8, 12, 9; the second row is initialized to 10, 13, 6, 16; and the third row is initialized to 27, 5, 10, 20. (10 pts) Consider the following declarations, and see p.566: enum brands = { GM, FORD, TOYOTA, BMW, KIA }; const int N_BRANDS = 5; const int COLOR_TYPES = 6; double...
Hand-write code to declare a two dimensional array of 3 columns and 4 rows. The Array will hold doubles. What is the array initialized to by default?
Write a java code that Declares and initialize a two-dimensional int array named grades. It should have 10 rows and 6 columns where it stores the values and then calculates the average of all the elements in the grades array that you have declared
1) Define a 2 dimensional arrays of doubles (3 rows by 3 columns) and 2) Read values from the user. 3) Print the numbers entered by the user in row major order 4) Print the numbers entered by the user in column major order import java.util.*; public class XXX_Chapter83 { public static void main(String[] args) { //creates array and stores values with input from user printArrayRowMajor (board); printArrayColumnMajor (board); } public static void printArrayRowMajor (int [] [] board) { //prints...
c++ 48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total 48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total
ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...
a) Declare and instantiate an array named scores of twenty-five elements of type int. (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...
Q is a TWO-DIMENSIONAL array of 3 rows and 3 columns. Write the statement to DISPLAY the value of the middle element of array Q. NOTE: Do not skip lines. Do not print an output label. ------------------------------- (T/F) The following statements store 75 into the first array element. int Score[10]; Score [1] = 75; ------------------------------- kindly help urgently
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...