JAVA Programming Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code
ID: 3930922 • Letter: J
Question
JAVA Programming Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code that will copy the non-zero elements of array "a" into an array "b", "squeezing out the zeroes", and setting nB equal to the number of non-zero elements in b. That is, if nA=13 and the contents of a[i], where i = 0 to nA - 1 are initially 0.0, 1.2, 0.0, 0.0, 0.0, 2.3, 0.0, 9.7, 5.6, 0.0, 3.6, 0.0, 0.0 then after execution of the code the contents should be nB=5, b[0]=1.2, b[1]=2.3, b[2]=9.7, b[3]=5.6, and b[4]=3.6. Note: You may assume both arrays are of type double.
Explanation / Answer
Please find the required program along with its output. Please see the comments against each line to understand the step.
-------------------------------------------------------------------------------------
OUTPUT:
Contents of b : [1.2, 2.3, 9.7, 5.6, 3.6]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.