-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_19.java
More file actions
72 lines (61 loc) · 1.98 KB
/
Copy pathlesson_19.java
File metadata and controls
72 lines (61 loc) · 1.98 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*_______________________________*/
//! Two Dimensional Arrays(Passing Arrays as a method Parameters)!!!!
package Notes;
import java.util.*;
public class lesson_19 {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
System.out.print("Enter the number of row: ");
int row = input.nextInt();
System.out.print("Enter the number of column: ");
int col = input.nextInt();
int[][] matrix = new int[row][col];
fillArray(matrix);
System.out.println("Array Filled! ");
System.out.println("Array will be printed! ");
printArray(matrix);
System.out.println("Array will be summed! ");
System.out.println("The sum of the array elements is equal "+sumArray(matrix));
System.out.print("Enter the number to search for: ");
int searchId=input.nextInt();
search(matrix, searchId);
}
public static void fillArray(int[][] list){
for(int i =0;i<list.length;i++){
for(int j=0;j<list[0].length;j++){
System.out.print("Enter data for row no. "+(i+1)+" and column no. "+(j+1)+" : ");
list[i][j]=input.nextInt();
}
}
}
public static void printArray(int[][] list){
for(int i =0;i<list.length;i++){
for(int j=0;j<list[0].length;j++){
System.out.print(list[i][j]+" ");
}
System.out.println();
}
}
public static int sumArray(int[][] list){
int sum=0;
for(int i =0;i<list.length;i++){
for(int j=0;j<list[0].length;j++){
sum +=list[i][j];
}
}
return sum;
}
public static void search (int[][] list, int target){
boolean findValue = false;
for(int i =0;i<list.length;i++){
for(int j=0;j<list[0].length;j++){
if(list[i][j]==target){
System.out.println("The index of "+target+" is in row no. "+(i+1)+" and column no. "+(j+1));
findValue = true;
}
}
}
if (!findValue) System.out.println("The item "+target+" isn't found");
}
}
/*_______________________________*/