Analyse 2D Array

Problem:
Input a 2 D array from the user. You must ask the user about the number of rows and columns of the array before taking the array as input. Input the array in a user friendly method. The array will contain the numbers from 0 to 20 only. If the user gives a number beyond this range, ask for the input again.
After you have taken the array as input, check that array for the number of 0s, 1s, 2s, ……., 19s and 20s present in the elements of the array and display the result on the screen. If a number, suppose 15 is not present in the array then there is no need to display “The number of 15s in the array is 0” simply skip this in output.
Code:

 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
28
29
30
31
32
33
34
35
36
 /**
  * @author Hafiz Muhammad Umer
  */
 import java.util.Scanner;
 public class Analyse2DArray 
 {
     public static void main(String[] args) 
     {
         Scanner input=new Scanner(System.in);
         System.out.print("Enter the number of rows of array: ");
         int x=input.nextInt();
         System.out.print("Enter the number of columns of array: ");
         int y=input.nextInt();
         int[][] array2d=new int[y][x];
         int[] tester={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
         for (int t=0;t<y;t++)
         {
             for (int t2=0;t2<x;t2++)
             {
                 do
                 {
                     System.out.print("Enter the ("+(t+1)+","+(t2+1)+") element: ");
                     array2d[t][t2]=input.nextInt();
                 }
                 while(array2d[t][t2]>20 || array2d[t][t2]<0);
                 tester[(array2d[t][t2])]++;
             }  
         }
         System.out.println("nHere is the result:");
         for (int t=0;t<=20;t++)  
             {
                 if (tester[t]!=0)
                     System.out.println("Number of "+t+"s="+tester[t]);
             }
     }
 }

Output:

Enter the number of rows of array: 4
Enter the number of columns of array: 4
Enter the (1,1) element: 2
Enter the (1,2) element: 6
Enter the (1,3) element: 12
Enter the (1,4) element: 16
Enter the (2,1) element: 2
Enter the (2,2) element: 16
Enter the (2,3) element: 3
Enter the (2,4) element: 6
Enter the (3,1) element: 5
Enter the (3,2) element: 4
Enter the (3,3) element: 7
Enter the (3,4) element: 12
Enter the (4,1) element: 3
Enter the (4,2) element: 4
Enter the (4,3) element: 7
Enter the (4,4) element: 18

Here is the result:
Number of 2s=2
Number of 3s=2
Number of 4s=2
Number of 5s=1
Number of 6s=2
Number of 7s=2
Number of 12s=2
Number of 16s=2
Number of 18s=1

Enter the number of rows of array: 2
Enter the number of columns of array: 2
Enter the (1,1) element: 6
Enter the (1,2) element: -2
Enter the (1,2) element: 6
Enter the (2,1) element: 21
Enter the (2,1) element: 3
Enter the (2,2) element: 5

Here is the result:
Number of 3s=1
Number of 5s=1
Number of 6s=2

Explanation:

  • The program asks the user for the number of rows and columns of the array and defines the array at line 14
  • The program declares an array named tester at line15 with 21 elements, all zero which will be used to store the number of  0s, 1s, 2s, ……., 19s and 20s present in the elements of the array.
  • A nested for loop starts at line 16 and ends at line 28 which will be used to input and test each element of the array.
  • The purpose of do while loop from line 20 to 25 is to ask the user again for input if the number that user has input is not in the range of 0-20.
  • At line 26, that element of the array tester is incremented which is given by the user as input. For example, if the input was 3, then the element of the tester array tester[3] is incremented. This process runs for each element of the array and the final result is stored in the array tester.
  • From line 29 to 34, the result is displayed on the screen.
  • The if statement at line 32 is used to skip displaying that number which is not present even a single time in the 2 dimensional array.

Leave a Comment