Question

In Java, declare a 2-dim array called matrix which holds up to 35 values (of type...

In Java, declare a 2-dim array called matrix which holds up to 35 values (of type int) 5 rows and 7 columns each.

Using sub-array processing and modularity, create methods to do the following:

1. Fill the array matrix with random numbers between 1-100

2. Compute the cumulative sum of the array matrix

3. Using column-wise processing compute, and print the cumulative sum of all the elements in each column

4. Compute the min value of the array matrix

5. Using column-wise processing, compute and print the minimum value in each column

6. Create and copy the transpose of the array matrix into a new 2-dim array m_transpose

Generate output similar to the sample output below:

Initial Output:

0 0 0 0 0 0 0

0 0 0 0 0 0 0

0 0 0 0 0 0 0

0 0 0 0 0 0 0

0 0 0 0 0 0 0

Matrix array populated with random values:

49 22 7 100 86 47 13

56 71 24 59 94 30 45

12 78 98 43 32 67 66

40 76 41 89 54 93 23

74 68 44 91 38 39 82

The sum of the elements of the array is: 1951

The sum of the elements per column are: [231, 315, 214, 382, 304, 276, 229]

The min value in the array Matrix is: 7

The min values per column are: [12, 22, 7, 43, 32, 30, 13]

49 56 12 40 74

22 71 78 76 68

7 24 98 41 44

100 59 43 89 91

86 94 32 54 38

47 30 67 93 39

13 45 66 23 82

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;
public class MyClass {
public static void fill(int matrix[][])
{
System.out.println("Initial Output:");
for(int i=0;i<5;i++)
{
for(int j=0;j<7;j++)
{
System.out.print(matrix[i][j]);
System.out.print(" ");
}
System.out.println();
}
Random rand = new Random();
for(int i=0;i<5;i++)
{
for(int j=0;j<7;j++)
{
matrix[i][j]=rand.nextInt(101)+1; //it produces random numbers between 1 and 100
}
}
System.out.println("Matrix array populated with random values:");
for(int i=0;i<5;i++)
{
for(int j=0;j<7;j++)
{
System.out.print(matrix[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public static void cummulative(int matrix[][])
{
int sum=0;
for(int i=0;i<5;i++)
{
for(int j=0;j<7;j++)
{
sum+=matrix[i][j];
}
}
System.out.println("The sum of the elements of the array is: "+sum);
}
public static void columnsum(int matrix[][])
{
int sum=0;
System.out.print("The sum of the elements per column are: [");
for(int i=0;i<7;i++)
{
for(int j=0;j<5;j++)
{
sum+=matrix[j][i];
}
System.out.print(sum);
if(i<6)
System.out.print(", ");
sum=0; //after finding the sum of each column re-initialize sum to 0
}
System.out.println("]");
}
public static void minval(int matrix[][])
{
int mini=matrix[0][0];
for(int i=0;i<5;i++)
{
for(int j=0;j<7;j++)
{
if(matrix[i][j]<mini)
mini=matrix[i][j];
}
}
System.out.println("The min value in the array Matrix is: "+mini);
}
public static void mincol(int matrix[][])
{
int mini=1000;
System.out.print("The min values per column are: [");
for(int i=0;i<7;i++)
{
for(int j=0;j<5;j++)
{
if(matrix[j][i]<mini)
mini=matrix[j][i];
}
System.out.print(mini);
if(i<6)
System.out.print(", ");
mini=1000; //after finding the minimum value of each column re-initialize mini to 1000
}
System.out.println("]");
}
public static void transpose(int matrix[][])
{
int [][]m_transpose = new int[7][5];
for(int i=0;i<5;i++) //store the transpose of matrix in m_transpose
{
for(int j=0;j<7;j++)
{
m_transpose[j][i]=matrix[i][j];
}
}
for(int i=0;i<7;i++)   
{
for(int j=0;j<5;j++)
{
System.out.print(m_transpose[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String args[])
{
int [][]matrix= new int[5][7];
fill(matrix); //method to fill the array with random values
cummulative(matrix); //method to Compute the cumulative sum of the array matrix
columnsum(matrix); //method to print the cumulative sum of all the elements in each column
minval(matrix); //method to Compute the min value of the array matrix
mincol(matrix); //method to compute and print the minimum value in each column
transpose(matrix); //method to Create and copy the transpose of the array matrix into a new 2-dim array
}
  
}

output:

Add a comment
Know the answer?
Add Answer to:
In Java, declare a 2-dim array called matrix which holds up to 35 values (of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array....

    Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array. Display all the values in the array on the screen. For each number in the array, determine if the number contains digit 7 or is divisible by 7. Display all those numbers on the screen. Original array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • An article of the American Journal of Clinical Pathology, by Metz et al. (A-4) published the...

    An article of the American Journal of Clinical Pathology, by Metz et al. (A-4) published the comparison of three methods to determine the percentage of dysmorphic erythrocytes in the urine. They obtained the following results when using the A (X) and B (Y) methods in 75 urine samples. Draw a scatter diagram and obtain the regression equation and graph it on the diagram. X            Y            X            Y            X            Y            X            Y 0            0            20          16          65          55          89          81 0           ...

  • Python 3 Create a function that takes an array and an integer v, and returns the...

    Python 3 Create a function that takes an array and an integer v, and returns the number of v occurrences in the array. Add a variable Nsteps to count how many times the loops have executed and display that information in a message The main program must take an array / list 2D, call the function, and display the result. >>>l3 = [52, 14, 14, 8, 85, 69, 1, 77, 94, 96, 51, 65, 35, 32, 87, 92, 74, 47,...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • I need to develop the 7 functions below into the main program that's given on top...

    I need to develop the 7 functions below into the main program that's given on top Write a C program to create array, with random numbers and perform operations show below. Il Project 3 (53-20). 1 Projects CS5,00 This file contains the function Programcution Dagine and one include <timo // Defining some constants definer_SIZE 20 define LOVE LIMIT 1 eine UPR UNIT define TALSE eine Tut 1 wold randomizery (int[], int, Int, int) wold pinay in. St, Int); En find...

  • Please show how you did this in excel. :13-19 Every home football game for the past...

    Please show how you did this in excel. :13-19 Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contrib- uted greatly to the overall profitability of the football program. One particular souvenir is the football pro- gram for each game. The number of programs sold at each game is described by the following probabil- ity distribution:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT