Question

In: Computer Science

Add each element in origList with the corresponding value in offsetAmount. Print each sum followed by...

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("");
}
}

Solutions

Expert Solution

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....


Related Solutions

Print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem....
Print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem. Print "Unknown" (followed by a newline) if the value of userItem does not match any of the defined options. For example, if userItem is GR_APPLES, output should be: Fruit #include <stdio.h> int main(void) { enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER}; enum GroceryItem userItem = GR_APPLES; /* Your solution goes here */ return 0; } USE C PLEASE
Print either "Fruit", "Drink", or "Unknown" (followed by anewline) depending on the value of userItem....
Print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem. Print "Unknown" (followed by a newline) if the value of userItem does not match any of the defined options. For example, if userItem = GR_APPLES, output should be: Fruit Sample program: #include using namespace std; int main() { enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER}; GroceryItem userItem = GR_APPLES; return 0; } Below, do not type an entire program. Only type the portion indicated by...
Print either "Fruit", "Drink", or "Unknown" (followed by anewline) depending on the value of userItem....
Print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem. Print "Unknown" (followed by a newline) if the value of userItem does not match any of the defined options. For example, if userItem is GR_APPLES, output should be: Fruit FOR JAVA import java.util.Scanner; public class GrocerySorter { public enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER}; public static void main (String [] args) { GroceryItem userItem = GroceryItem.GR_APPLES; /* Your solution goes here */ return; }...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Find the p-value and corresponding statistical conclusion for each of these situations. Be sure to take...
Find the p-value and corresponding statistical conclusion for each of these situations. Be sure to take into account directionality of the test. A) Ho: ? ? 8     Ha: ? > 8     n = 24     ? = 0.01     t-statistic = 1.65     p-value =       Note:Round the answer to four decimal places     The statistical decision is:  ---Select--- Reject Ho Fail to reject Ho B) Ho: ? ? 59     Ha: ? < 59     n = 19     ? = 0.02...
goal of this function will be to add an element to an already existing array in...
goal of this function will be to add an element to an already existing array in C/C++. bool add(structure dir[], int& size, const char nm[], const char ph[]){ //code here } add = function name structure = structure with two fields { char name[20]; char phone[13]; } int& size = # of entries in dir nm = name that's going to be added ph = phone that's going to be added. return true if entry was successfully added, false if...
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
how to set the print area, insert page breaks, add print titles in excel. Explain how...
how to set the print area, insert page breaks, add print titles in excel. Explain how to do these three features and why they are important to a file
Answer Question 1, each part of the question has a corresponding point value seen in ()...
Answer Question 1, each part of the question has a corresponding point value seen in () 1A company that manufactures military hats reports that the mean head circumference of all men in the military is 22.05 inches with a standard deviation of 0.68 inches. Suppose the distribution of head circumference is approximately normal, and one man in the military is selected at random. a.Find the probability that the man’s head circumference is less than 21 inches (2 pts) b.Find the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT