Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user for a size of a 2D matrix. Then it populat

ID: 3661805 • Letter: W

Question

Write a program that prompts the user for a size of a 2D matrix. Then it populates the matrix with random numbers from 0-9, and prints that out. Next create a new matrix that is twice the size of the old one and is populated with the numbers from the old one expanded out in 2x2 increments.

Example Dialog:

Enter the size of the matrix

3

The matrix is

0   5   2   

3   5   2   

9   7   1   

The doubled matrix is

0   0   5   5   2   2   

0   0   5   5   2   2   

3   3   5   5   2   2   

3   3   5   5   2   2   

9   9   7   7   1   1   

9   9   7   7   1   1

Another Example Dialog:

Enter the size of the matrix

2

The matrix is

6   5   

6   0   

The doubled matrix is

6   6   5   5   

6   6   5   5   

6   6   0   0   

6   6   0   0

Explanation / Answer

Program:

import java.util.Scanner;
import java.util.Random;
public class DoubleMatrix {
   private static Scanner sca;

   public static void main(String arg[])
   {
       sca = new Scanner(System.in);
   int n;
   System.out.println("Enter the size of the matrix");
   n=sca.nextInt();
   int arr[][]=new int[n][n];
   int arr2[][]=new int[2*n][2*n];
   Random rand = new Random();
   System.out.println("The matrix is ");
   for(int i=0;i<n;i++)
   {
   for(int j=0;j<n;j++){
   arr[i][j]=rand.nextInt(10);
   System.out.print(arr[i][j]+" ");
   }
   System.out.println("");
   }
   System.out.println("The doubled matrix is ");
   for(int i=0;i<n;i++)
   {
   for(int j=0;j<n;j++)
   arr2[i*2][j*2]=arr2[i*2+1][j*2]=arr2[i*2+1][j*2+1]=arr2[i*2][j*2+1]=arr[i][j];
   }
   for(int i=0;i<n*2;i++)
   {
   for(int j=0;j<n*2;j++)
   System.out.print(arr2[i][j]+" ");
   System.out.println("");
   }

   }
}

Result:

Enter the size of the matrix
4
The matrix is

8 7 0 9
9 9 8 7
1 9 4 8
1 8 3 9
The doubled matrix is

8 8 7 7 0 0 9 9
8 8 7 7 0 0 9 9
9 9 9 9 8 8 7 7
9 9 9 9 8 8 7 7
1 1 9 9 4 4 8 8
1 1 9 9 4 4 8 8
1 1 8 8 3 3 9 9
1 1 8 8 3 3 9 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote