-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArrays.java
46 lines (33 loc) · 849 Bytes
/
Arrays.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
// Find the maximum of the array values
double max = a[0];
for (int i = 1; i < a.length; i++)
if (a[i] > max) max = a[i];
// Compute the average of the array values
int N = a.length;
double sum = 0.0;
for (int i = 0; i < N; i++)
sum += a[i];
double average = sum / N;
// Copy to another array
int N = a.length;
double[] b = new double[N];
for (int i = 0; i < N; i++)
b[i] = a[i];
// Reverse the elements within an array
int N = a.length;
for (int i = 0; i < N/2; i++)
{
double temp = a[i];
a[i] = a[N-1-i];
a[N-i-1] = temp;
}
// Matrix-matrix multiplication (square matrices)
// a[][]*b[][] = c[][]
int N = a.length;
double[][] c = new double[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{ // Compute dot product of row i and column j.
for (int k = 0; k < N; k++)
c[i][j] += a[i][k]*b[k][j];
}