728x90
public class VectorAddition {
static int[] addVectors(int[] u, int[] v) {
// Check if the lengths of the two arrays are the same
if (u.length != v.length) {
throw new IllegalArgumentException("Vectors must be of the same length");
}
int[] result = new int[u.length];
for (int i = 0; i < u.length; i++) {
result[i] = u[i] + v[i];
}
return result;
}
public static void main(String[] args) {
int [] u = new int[10];
int [] v = new int[10];
for (int i = 0; i < u.length; i++) {
u[i] = (int) (Math.random() * 100); // Generate a random number between 0 and 99
}
for (int i = 0; i < v.length; i++) {
v[i] = (int) (Math.random() * 100); // Generate a random number between 0 and 99
}
int[] sum = addVectors(u, v);
System.out.print("Resulting vector: ");
for (int value : sum) {
System.out.print(value + " ");
}
}
}
728x90
'호그와트' 카테고리의 다른 글
치킨버거 맛 좀 볼래요? (0) | 2023.11.09 |
---|---|
leetcode sumArray (0) | 2023.11.05 |
쥐를 잡자 냐옹아 (0) | 2023.11.01 |
영국 사진들 대공개 1 (0) | 2023.09.13 |
today my lunch hacking exercise (it is like my lunch snack) (0) | 2023.08.02 |