-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort and Linear Search.java
68 lines (47 loc) · 1.31 KB
/
Sort and Linear Search.java
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
import java. util.*;
public class Nwazirik_Chapter8Lab1 {
//Kean Nwaziri
//Program creates and fills a 2 dim array and prints the sum of the rows
public static void main(String[] args) {
int [][] matrix = createAndFillArray();
printArray(matrix);
int [] sums = calcSums(matrix);
printSums(sums);
}
public static int [][] createAndFillArray() {
Scanner red=new Scanner(System.in);
int[][]matrix=new int[3][3];
for(int row=0;row<matrix.length;row++) {
System.out.println("Enter the 3 values for row: " );
for(int col=0;col<matrix[row].length;col++) {
matrix[row][col]=red.nextInt();
}
}
return matrix;
}
public static int [] calcSums (int [][] matrix) {
int sum=0;
int[] table=new int[matrix.length];
for(int row=0;row<matrix.length;row++) {
sum=0;
for(int col=0;col<matrix[row].length;col++) {
sum=sum+matrix[row][col];
}
table[row]=sum;
}
return table;
}
public static void printSums (int [] sums) {
for(int row=0;row<sums.length;row++) {
System.out.println("The sum of row " +row + " is " + sums[row]);
}
}
public static void printArray(int [][] matrix) {
for(int row=0;row<matrix.length;row++) {
for(int col=0;col<matrix[row].length;col++) {
System.out.print(matrix[row][col]+ " ");
}
System.out.println();
}
}
}