Question

Write a Java program to create a two-dimensional array, myFifthMatrix, with the following specifications:  Initialize...

Write a Java program to create a two-dimensional array, myFifthMatrix, with
the following specifications:
 Initialize myFifthMatrix with int type and with size [3][3].
 Use a for loop to fill the myFifthMatrix with random values between 0 and 99.
 Print the myFifthMatrix using a for loop.
 For each column, use a variable named total to store its sum. Add each element
in the column to total using a for loop

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

Program:

import java.util.*;

class MatrixColumnTotal
{
public static void main(String args[])
{
int r,c;
int myFifthMatrix[][]=new int[3][3];

for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
myFifthMatrix[r][c]=(int)(Math.random()*(99-0)+0); //(int)(Math.random()*(upper-lower)+lower)
}
}

System.out.println("\nValues of myFifthMatrix[3][3] array: \n");

for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
System.out.print(myFifthMatrix[r][c]+" ");
}
System.out.print("\n");
}

System.out.println("\n");
int total;
String s1="";
for(r=0;r<=2;r++)
{
total=0;
for(c=0;c<=2;c++)
{
total=total+myFifthMatrix[c][r];
}
System.out.println("Sum of the Column"+(r+1)+": "+total);
s1=s1+total+" "; //a space
}

System.out.println("\n");

for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
System.out.print(myFifthMatrix[r][c]+" "); //two spaces
}
System.out.print("\n");
}
System.out.println("----------------------------");
System.out.println(s1);
System.out.println("----------------------------\n");
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java program to create a two-dimensional array, myFifthMatrix, with the following specifications:  Initialize...
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
  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

  • Write a java program that create a 2 dimensional array of type double of size 10...

    Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance

  • Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize...

    Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize your array using a text file, user input or assign value while declaring the array, you can choose the method. Then, a. Write a loop to print first row of the array on one line, using cout. b. Write a loop to print first column of the array on one line, using cout. c. Write a loop to print first five rows of the...

  • Row and columns sum Create a java program that: Input: Receive as input in command line...

    Row and columns sum Create a java program that: Input: Receive as input in command line the sizes m and n of a two dimensional array Receive as input in command line the minValue and maxValue between which the random numbers will be generated for your two dimensional array Generate: Generate a two-dimensional with numbers between minValue and maxValue (inclusive) with the given size (m x n) Compute: Compute the sum for each row and store the results in a...

  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • Write a C++ program to scale and average the values in a two-dimensional array. Write the...

    Write a C++ program to scale and average the values in a two-dimensional array. Write the following functions:           void randomize2DArray(int arr[ROW_SIZE][COL_SIZE]) – places random values between 1 and 100 in a two-dimensional array .           void scale2DArray(int arr[ROW_SIZE][COL_SIZE], double scale) – multiplies every value in the two-dimensional array by scale.           double average2DArray(int arr[ROW_SIZE][COL_SIZE]) – calculates the average value over all elements in the two-dimensional array. Randomize and print the values in the two-dimensional array of size 7 x...

  • Write a java code that Declares and initialize a two-dimensional int array named grades. It should...

    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

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

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