> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Arrays 2

## Multidimensional Arrays

<Tabs>
  <Tab title="Ex-1">
    ```java theme={null}
    int[][] a = new int[3][4];
    ```

    ![1](https://i.ibb.co/HtzHWtC/Screenshot-2024-04-03-210547.png)
  </Tab>

  <Tab title="Ex-2">
    ```java theme={null}
    int n = 5;
    int[][] distance = new int[n][n];
    distance[0][1] = distance[1][0] = 10;
    distance[1][3] = distance[3][1] = 5;
    ```
  </Tab>

  <Tab title="Ex-3">
    ```java theme={null}
    int[][] a = {
        {1, 2, 3},
        {4, 5, 6, 9},
        {7},
    };
    ```

    ![2](https://i.ibb.co/T1rk18d/Screenshot-2024-04-03-210554.png)
  </Tab>

  <Tab title="Ex-4">
    ```java theme={null}
    int[][] data = new int[4][];
    data[0] = new int[3];
    data[1] = new int[10];
    data[2] = new int[2];
    data[3] = new int[6];
    ```
  </Tab>
</Tabs>

## Array Traversal

<CodeGroup>
  ```java Ex-1 theme={null}
  int[][] data = {
      { 1, 2, 3 },
      { 4, 5 },
      { 6, 7, 8, 9 }
  };

  for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[i].length; j++) {
          System.out.print(data[i][j] + "\t");
      }
      System.out.println();
  }
  ```

  ```java Ex-2 theme={null}
  int[][] array2D = {
      { 1, 2, 3 },
      { 4, 5 },
      { 6, 7, 8, 9 }
  };

  for (int[] array1D : array2D) {
      for (int elem : array1D) {
          System.out.print(elem + "\t");
      }
      System.out.println();
  }
  ```
</CodeGroup>

## Exercise

### Exercise 1

<Tabs>
  <Tab title="Question">
    * Create an array that can hold $n$ integers
    * Fill up the array with user inputs
    * Ask the user to enter a value that they want to find
    * Show if the value is in the array

    ![1](https://i.ibb.co/8cZ9TYR/Screenshot-2024-04-03-215551.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("Enter the length of array: ");
      		int n = sc.nextInt();
      		int[] arr = new int[n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < n; i++) {
      			arr[i] = sc.nextInt();
      		}
      		System.out.print("Enter the lookup value: ");
      		int lookup = sc.nextInt();
      		for (int i = 0; i < n; i++) {
      			if (arr[i] == lookup) {
      				System.out.println(lookup + " is in the array.");
      			}
      		}
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 2

<Tabs>
  <Tab title="Question">
    * Create an array that can hold $n$ integers
    * Fill up the array with user inputs
    * Ask the user to enter a value that they want to find
    * Show how many times the value appears in the array

    ![2](https://i.ibb.co/Jrp44Fz/Screenshot-2024-04-03-215557.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("Enter the length of array: ");
      		int n = sc.nextInt();
      		int[] arr = new int[n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < n; i++) {
      			arr[i] = sc.nextInt();
      		}
      		System.out.print("Enter the lookup value: ");
      		int lookup = sc.nextInt();
      		int count = 0;
      		for (int i = 0; i < n; i++) {
      			if (arr[i] == lookup) {
      				count++;
      			}
      		}
      		System.out.println(lookup + " appears " + count + " time(s) in the array.");
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 3

<Tabs>
  <Tab title="Question">
    * Create an array that can hold $n$ integers
    * Fill up the array with user inputs
    * Ask the user to enter a value that they want to find
    * Show whether the value is in the array or not

    ![3](https://i.ibb.co/JrjFGp5/Screenshot-2024-04-03-215602.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("Enter the length of array: ");
      		int n = sc.nextInt();
      		int[] arr = new int[n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < n; i++) {
      			arr[i] = sc.nextInt();
      		}
      		System.out.print("Enter the lookup value: ");
      		int lookup = sc.nextInt();
      		boolean found = false;
      		for (int i = 0; i < n; i++) {
      			if (arr[i] == lookup) {
      				found = true;
      				break;
      			}
      		}
      		if (found) {
      			System.out.println(lookup + " is in the array.");
      		} else {
      			System.out.println(lookup + " is not in the array.");
      		}
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 4

<Tabs>
  <Tab title="Question">
    * Create an array that can hold $n$ integers
    * Fill up the array with user inputs
    * Ask the user to enter a value that they want to find
    * Show on which index the value appears in the array

    ![4](https://i.ibb.co/Csf13Gs/Screenshot-2024-04-03-215608.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("Enter the length of array: ");
      		int n = sc.nextInt();
      		int[] arr = new int[n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < n; i++) {
      			arr[i] = sc.nextInt();
      		}
      		System.out.print("Enter the lookup value: ");
      		int lookup = sc.nextInt();
      		boolean found = false;
      		for (int i = 0; i < n; i++) {
      			if (arr[i] == lookup) {
      				found = true;
      				System.out.println(lookup + " is at index " + i + ".");
      			}
      		}
      		if (!found) {
      			System.out.println(lookup + " is not in the array.");
      		}
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 5

<Tabs>
  <Tab title="Question">
    * Create an array that can hold $n$ integers
    * Fill up the array with user inputs
    * Find the largest value in the array
    * Also display on which index the maximum value appears in the array

    ![5](https://i.ibb.co/S0k5562/Screenshot-2024-04-03-215613.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("Enter the length of array: ");
      		int n = sc.nextInt();
      		int[] arr = new int[n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < n; i++) {
      			arr[i] = sc.nextInt();
      		}
      		int max = arr[0];
      		int index = 0;
      		for (int i = 1; i < n; i++) {
      			if (arr[i] > max) {
      				max = arr[i];
      				index = i;
      			}
      		}
      		System.out.printf("Maximum value: %d", max);
      		System.out.printf("\nIndex: %d", index);
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 6

<Tabs>
  <Tab title="Question">
    * Create a 2-dimensional array that can hold m x n integers
    * Fill up the array with user inputs
    * Display the contents of the array

    ![6](https://i.ibb.co/b6xmh7B/Screenshot-2024-04-03-210601.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("m: ");
      		int m = sc.nextInt();
      		System.out.print("n: ");
      		int n = sc.nextInt();
      		int[][] arr = new int[m][n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				arr[i][j] = sc.nextInt();
      			}
      		}
      		System.out.println("Contents of the array:");
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				System.out.print(arr[i][j] + "\t");
      			}
      			System.out.println();
      		}
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 7

<Tabs>
  <Tab title="Question">
    * Create a 2-dimensional array that can hold m x n integers
    * Fill up the array with user inputs
    * Ask the user to enter a value that they want to find
    * Show how many times the value appears in the array

    ![7](https://i.ibb.co/vz4TYJM/Screenshot-2024-04-03-210607.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("m: ");
      		int m = sc.nextInt();
      		System.out.print("n: ");
      		int n = sc.nextInt();
      		int[][] arr = new int[m][n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				arr[i][j] = sc.nextInt();
      			}
      		}
      		System.out.print("Enter the lookup value: ");
      		int lookup = sc.nextInt();
      		int count = 0;
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				if (arr[i][j] == lookup) {
      					count++;
      				}
      			}
      		}
      		System.out.println(lookup + " appears " + count + " time(s) in the array.");
      	}
      }

    ```
  </Tab>
</Tabs>

### Exercise 8

<Tabs>
  <Tab title="Question">
    * Create a 2-dimensional array that can hold m x n integers
    * Fill up the array with user inputs
    * Find the largest value in the array
    * Also display on which index the maximum value appears in the array

    ![8](https://i.ibb.co/DRFGHt9/Screenshot-2024-04-03-210615.png)
  </Tab>

  <Tab title="Answer">
    ```java theme={null}
      import java.util.Scanner;
      public class Main {
      	public static void main(String[] args) {
      		Scanner sc = new Scanner(System.in);
      		System.out.print("m: ");
      		int m = sc.nextInt();
      		System.out.print("n: ");
      		int n = sc.nextInt();
      		int[][] arr = new int[m][n];
      		System.out.println("Enter the elements of the array: ");
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				arr[i][j] = sc.nextInt();
      			}
      		}
      		int max = arr[0][0];
      		int maxI = 0;
      		int maxJ = 0;
      		for (int i = 0; i < m; i++) {
      			for (int j = 0; j < n; j++) {
      				if (arr[i][j] > max) {
      					max = arr[i][j];
      					maxI = i;
      					maxJ = j;
      				}
      			}
      		}
      		System.out.printf("Maximum value: %d\n", max);
      		System.out.printf("Index: (%d, %d)\n", maxI, maxJ);
      	}
      }

    ```
  </Tab>
</Tabs>
