In: Computer Science
Add each element in origList with the corresponding value in
offsetAmount. Print each sum followed by a space.
Ex: If origList = {4, 5, 10, 12} and offsetAmount = {2, 4, 7, 3},
print:
6 9 17 15
import java.util.Scanner;
public class VectorElementOperations {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] origList = new int[NUM_VALS];
int[] offsetAmount = new int[NUM_VALS];
int i;
origList[0] = 20;
origList[1] = 30;
origList[2] = 40;
origList[3] = 50;
offsetAmount[0] = 4;
offsetAmount[1] = 6;
offsetAmount[2] = 2;
offsetAmount[3] = 8;
*insert code*
System.out.println("");
}
}
CODE:
import java.util.Scanner;
public class VectorElementOperations {
public static void main (String [] args) {
final int NUM_VALS = 4;
/* created a two Integer arrays */
int[] origList = new int[NUM_VALS]; // allocating memory for 4
integers.
int[] offsetAmount = new int[NUM_VALS]; // allocating memory for 4
integers.
int i;
// initialize the elements of the first origList array
origList[0] = 20;
origList[1] = 30;
origList[2] = 40;
origList[3] = 50;
// initialize the elements of the second offsetAmount
array
offsetAmount[0] = 4;
offsetAmount[1] = 6;
offsetAmount[2] = 2;
offsetAmount[3] = 8;
/* created and allocating memory for 4 Integer to array sums to
hold the addition values of two arrays */
int[] sums = new int[NUM_VALS];
// accessing the elements of the specified array
for ( i = 0; i < NUM_VALS; i++) {
sums[i] = origList[i] + offsetAmount[i]; // Adding two arrays
System.out.print(sums[i] + " "); //Print the Sums array
elements
}
System.out.println("");
}
}
Output:
24 36 42 58
Output
Snapshots:
Please give Thumbs Up....