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

Write a program which asks the users for the dimensions of an n times n matrix.

ID: 3832574 • Letter: W

Question

Write a program which asks the users for the dimensions of an n times n matrix. The matrix should be randomly populated with the characters x and o. Display the matrix then report all rows, columns, and both diagonals that have all x's or all o's. Sample Output: Enter matrix size: 5 x o x x o o x o o o x x x x o o x xo o o o o x All x's in row 3. All x's in major diagonal. Enter matrix size: 6 o x o o o x o x o o o o x x x x o o o x x o o o x o x o o o x x x o o o x All x's in column 2. All o's in column 5.

Explanation / Answer

import java.util.Scanner;

import java.util.Random;

public class Randommatrix {

   public static void main(String[] args)

   {

       System.out.print("Enter matrix size:");

       Scanner scan = new Scanner(System.in);

       int n = scan.nextInt();

       int[][] arr = new int[n][n];

       int row[] = new int[n];

       int col[] = new int[n];

       Random rand = new Random();

       for(int i=0;i<n;i++)

       {

           row[i]=0;

           col[i]=0;

       }

       int main=0,sec=0;

       for(int i=0;i<n;i++)

       {

           for(int j=0;j<n;j++)

           {

               arr[i][j] = rand.nextInt(2);

               row[i]+=arr[i][j];

               col[j]+=arr[i][j];

               if(arr[i][j]==0)

               {

                   System.out.print("x ");

               }

               else

               {

                   System.out.print("o ");

               }

               if(i==j)

               {

                   main+=arr[i][j];

               }

               if(i+j==4)

               {

                   sec+=arr[i][j];

               }

           }

           System.out.println("");

       }

       for(int i=0;i<n;i++)

       {

           if(row[i]==5)

           {

               System.out.println("All o's in row "+(i+1));

           }

           if(row[i]==0)

           {

               System.out.println("All x's in row "+(i+1));

           }

       }

       for(int i=0;i<n;i++)

       {

           if(col[i]==5)

           {

               System.out.println("All o's in column "+(i+1));

           }

           if(col[i]==0)

           {

               System.out.println("All x's in column "+(i+1));

           }

       }

       if(main==0)

       {

           System.out.println("All x's in main diagnol");

       }

       if(main==5)

       {

           System.out.println("All o's in main diagnol");

       }

       if(sec==0)

       {

           System.out.println("All x's in secondary diagnol");

       }

       if(sec==5)

       {

           System.out.println("All o's in secondary diagnol");

       }

   }

}

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